PDA

View Full Version : Solved: Input check



Kaizer
02-20-2007, 09:25 AM
guys, I need some help.

I have two txtBoxes on a form for users to add the data. One field is a Description and the other one is Amount.

Before submission I would like to make sure that the first field doesn't contain the "/" symbol and the second field is actually the numeric value.

Can you help with the code, please?

Bob Phillips
02-20-2007, 09:32 AM
This checks input as entered



Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If

End Sub

You can test for / with



If InStr("TextBox1.Text"."/") > 0 Then
MsgBox "Invalid input"
End If

dansam
02-20-2007, 09:49 AM
Hi
Can we use this to make sure that textbox has an integer value :?

Private Sub CommandButton1_Click()
On Error GoTo err:
Dim a As Long
a = textbox1
If a = textbox1 Then
MsgBox "ya, that's good"
End If

err:
If Err.Number <> 0 Then
MsgBox "Input Integer only", vbCritical, "Invalid data"
End If
End Sub
Regards,
Dan

Kaizer
02-20-2007, 10:27 AM
Hi
Can we use this to make sure that textbox has an integer value :?

Private Sub CommandButton1_Click()
On Error GoTo err:
Dim a As Long
a = textbox1
If a = textbox1 Then
MsgBox "ya, that's good"
End If

err:
If Err.Number <> 0 Then
MsgBox "Input Integer only", vbCritical, "Invalid data"
End If
End Sub
Regards,
Dan

I thought of using the code that looks like this:
Private Sub txtAddAmnt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not IsNumeric(txtAddAmnt) Then KeyAscii = 0
End Sub
For some reason it's not working. You, guys, know why?

Bob Phillips
02-21-2007, 02:17 AM
That is because Keypress is a before event, it is run before the value is accepted, so on first run the textbox is always blank, so the test always fails, so the input always gets cancelled.

You would use the technique I showed earlier



Private Sub txtAddAmnt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case 32 ' space
Case Else
KeyAscii = 0
Beep
End Select

End Sub

Kaizer
02-21-2007, 03:59 AM
Great, it works. Thaks a lot, XLD

dansam
02-21-2007, 09:50 AM
Hi,
xld's code works fine.
thanks
dan