Hey,
Real sorry if this is stupidly obvious, but what is the validation I can use in a command box to ensure some text boxes I have contain a number, so if not it displays an error message.
Would really appreciate your help.
Thanks.
Printable View
Hey,
Real sorry if this is stupidly obvious, but what is the validation I can use in a command box to ensure some text boxes I have contain a number, so if not it displays an error message.
Would really appreciate your help.
Thanks.
command box?? u mean command button?
VB Code:
If Not IsNumeric(Text1) Then MsgBox "Error! Text1 is not a number" Text1.SetFocus End If
Hey, thanks for the reply.Quote:
Originally Posted by Static
Is it possible that I can use this for more than one text field in one code, e.g.:
If Not IsNumeric(Text1, Text2, Text3) Then
etc.
I tried that, but it returned an error.
If you want to use a control that only allows numbers the follow then NumberBox ActiveX control link in my signature.
use marty's control... or
u can code it like this..
If Not IsNumeric(Text1) or Not IsNumeric(Text2) or Not IsNumeric(Text3) Then
Here's some coding I did today that will only allow a user to key in numbers and nothing else. The second set of coding will display an error message if the user tries to copy/paste letters and other non-numeric data into the box. The second set of coding is the same as what the others said. BUT, with the first set of coding it just flat out prevents the user from using the keyboard to type in non-numeric data into the textbox and they won't get annoyed by error messages constantly popping up unless they try to copy/paste data into the textbox.Quote:
Originally Posted by sugardaddy
VB Code:
Private Sub txtEmployee_KeyPress(KeyAscii As Integer) 'Only allows the user to use numbers and backspace on the keyboard for 'the textbox txtEmployee If (KeyAscii >= 48 And KeyAscii) <= 57 Or KeyAscii = 8 Then Exit Sub Else KeyAscii = 0 End If End Sub Private Sub txtEmployee_LostFocus() 'Checks for non-numeric characters if user tries to copy/pastes in data If Not IsNumeric(txtEmployee(Index)) And txtEmployee(Index) <> "" Then MsgBox ("Please use numbers only!") txtEmployee(Index).SetFocus End If End If
dont use lost focus.. use validate
then cancel = true when errors...
Yea, you can use Validate instead. I just thought it only activated when you tried to save/delete/etc data. Didn't know it activated also when the control lost focus.Quote:
Originally Posted by Static
If the textbox loses focus to a control that has CausesValidation = True (the default) then it will fire, otherwise it won't.