Text box Exit event fails

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Having trouble getting my text box to behave.
I want to restore the original text (which always will exist), if the user exits entering nothing.
Also limit the input to numbers only. That bit works ok, as does the Change event.

The Exit event is the problem. Even though Len = 0 and tmpText contains the value, the text box does not show it.

I am a bit fuzzzy on the ".text' part , which I know has some relevance, but I've tried all combinations and no difference.





Code:
Private Sub TextBox1_Change()
    On Error GoTo quit
    Frame2.lblRed.BackColor = RGB(TextBox1, 0, 0)
    Frame2.btnSet.BackColor = RGB(TextBox1, TextBox2, TextBox3)
    ColourPicked = Frame2.btnSet.BackColor
quit:
End Sub

Private Sub TextBox1_Enter()
    tmpText = TextBox1.text
    'Debug.Print "Enter " & tmpText
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'Debug.Print "Length"; Len(TextBox1)
    If Len(TextBox1) = 0 Then
       TextBox1 = tmpText
    End If
    'Debug.Print "Exit" & tmpText
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = Restrict(KeyAscii)
End Sub

Private Function Restrict(KeyAscii)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Restrict = KeyAscii
        Case Else
            KeyAscii = 0
    End Select
End Function
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
.
I only concentrated on having the TextBox populated. Did not take action on the rest of your macro.

Try this :

Code:
Option Explicit


Private Sub TextBox1_Change()
Dim tmpText As String
Application.ScreenUpdating = False
   
    If TextBox1.Text <> tmpText Then
        tmpText = ""
        TextBox1.Text = TextBox1.Text
    Else
       TextBox1.Text = tmpText
    End If
    
  Application.ScreenUpdating = True
End Sub


Private Sub TextBox1_GotFocus()
Dim tmpText As String
Application.ScreenUpdating = False


    With TextBox1
        .SelStart = 0
        .SelLength = Len(.Text)
    End With


    TextBox1.Text = "Text"
    tmpText = TextBox1.Text
    
Application.ScreenUpdating = True


End Sub
 
Upvote 0
I did tests and I do not see what the problem is. you can put a sequence of the steps
 
Upvote 0
How do you declare “tmpText”?
You need to declare it in the top of the code module, so it will have a module level scope, like this:
Code:
Private tmpText as string
 
Upvote 0
Dante, if the text is deleted the intent is to restore back to what it was, not become empty.
I'm trying to work through what logit suggested.. won't tmpText always be "" as it's just been dimensioned ?
 
Upvote 0
I have Dim tmpText as String in Form General Declarations.
 
Upvote 0
Are you sure you have tmpText at the beginning of the form code?
I tried the code again and it works fine.

Code:
[COLOR=#0000ff]Dim tmpText[/COLOR]


Private Sub TextBox1_Change()
    On Error GoTo quit
    Frame2.lblRed.BackColor = RGB(TextBox1, 0, 0)
    Frame2.btnSet.BackColor = RGB(TextBox1, TextBox2, TextBox3)
    ColourPicked = Frame2.btnSet.BackColor
quit:
End Sub


Private Sub TextBox1_Enter()
    tmpText = TextBox1.Text
    'Debug.Print "Enter " & tmpText
End Sub


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'Debug.Print "Length"; Len(TextBox1)
    If Len(TextBox1) = 0 Then
       TextBox1 = tmpText
    End If
    'Debug.Print "Exit" & tmpText
End Sub


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = Restrict(KeyAscii)
End Sub


Private Function Restrict(KeyAscii)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Restrict = KeyAscii
        Case Else
            KeyAscii = 0
    End Select
End Function
 
Upvote 0
This is very odd. I can also get this working but under some circumstances it will not.
But I have another issue which may be a factor and I'll start a new thread for that and continue here
after/if that's resolved.
 
Upvote 0
If you find the sequence of steps when the error occurs I will try to replicate it to find a possible solution
 
Upvote 0
Thanks Dante, I can do this but how do i upload an attachment ?
 
Upvote 0

Forum statistics

Threads
1,215,119
Messages
6,123,172
Members
449,094
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top