G Guest Dec 20, 2004 #1 How can I make a text box to accept only numbers or text in a specific format (eg. for Post Codes)
G Guest Dec 20, 2004 #2 Assuming the textbox is in a form then in the code behind the form you can write a short procedure. Assuming the TextBox is called MyTextBox then Sub MyTextBox_Change() if not MyTextBox.Value like "##" then MyTextBox.Value = "" MsgBox "This is not a number", vbOKOnly, "Dont type text" end if end sub This will be triggered as the user types. If you prefer to wait until the user has completed his/her mistake then use _afterUpdate() instead Hope this helps Nick Shinkins
Assuming the textbox is in a form then in the code behind the form you can write a short procedure. Assuming the TextBox is called MyTextBox then Sub MyTextBox_Change() if not MyTextBox.Value like "##" then MyTextBox.Value = "" MsgBox "This is not a number", vbOKOnly, "Dont type text" end if end sub This will be triggered as the user types. If you prefer to wait until the user has completed his/her mistake then use _afterUpdate() instead Hope this helps Nick Shinkins
B Bob Phillips Dec 20, 2004 #3 Why not trap them as input Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii Case 48 To 57 'Nos 0 - 9 Exit Sub Case Else Application.EnableEvents = False KeyAscii = 0 Application.EnableEvents = True Beep End Select End Sub -- HTH RP (remove nothere from the email address if mailing direct)
Why not trap them as input Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii Case 48 To 57 'Nos 0 - 9 Exit Sub Case Else Application.EnableEvents = False KeyAscii = 0 Application.EnableEvents = True Beep End Select End Sub -- HTH RP (remove nothere from the email address if mailing direct)