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
 
You could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,215,109
Messages
6,123,137
Members
449,098
Latest member
Doanvanhieu

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