Text box Exit event fails

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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