xyzzy
2012-08-09 08:49:34 UTC
Hi guys,
I wrote this standard module, NumericInputErrorCheck, used to validate that the input from a text box is a numeric integer between the values of MinVal & MaxVal:
Option Explicit
Dim Message As Integer, N As Integer
Public Function NumericInputErrorCheck(TextInput As TextBox, MinVal As Integer, MaxVal As Integer) As Boolean
NumericInputErrorCheck = True
For N = 1 To Len(TextInput)
If Asc(Mid(TextInput, N, 1)) < 48 Or Asc(Mid(TextInput, N, 1)) > 57 Then NumericInputErrorCheck = False: Exit For
Next N
If TextInput = "" Or Val(TextInput) < MinVal Or Val(TextInput) > MaxVal Or Val(TextInput) <> Int(Val(TextInput)) Or NumericInputErrorCheck = False Then
Message = MsgBox("Please enter an integer between " & MinVal & " and " & MaxVal & ".", vbExclamation, "Error")
NumericInputErrorCheck = False
TextInput.SetFocus
TextInput.Text = ""
End If
End Function
Which seems to work a treat when I call from a textbox subroutine:
Private Sub txtInput_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then If NumericInputErrorCheck(txtInput, 1, 10) Then Print "Correct!"
End Sub
BUT I have a few queries...
1) Do you think this is the best was of error checking for an integer value?
2) Why does my system beep when I hit return? Any way of stopping it?
3) In the standard module is it necessary to declare Option Explicit?
4) In the standard module should the variables Message & N be declared within the function itself?
5) Why does 'If KeyCode = vbKeyReturn Then If NumericInputErrorCheck....' work but 'If KeyCode = vbKeyReturn and NumericInputErrorCheck....' calls the function any time a key is pressed? I thought the whole point of AND was that both arguments had to be valid.
Your feedback would be much appreciated guys :)
I wrote this standard module, NumericInputErrorCheck, used to validate that the input from a text box is a numeric integer between the values of MinVal & MaxVal:
Option Explicit
Dim Message As Integer, N As Integer
Public Function NumericInputErrorCheck(TextInput As TextBox, MinVal As Integer, MaxVal As Integer) As Boolean
NumericInputErrorCheck = True
For N = 1 To Len(TextInput)
If Asc(Mid(TextInput, N, 1)) < 48 Or Asc(Mid(TextInput, N, 1)) > 57 Then NumericInputErrorCheck = False: Exit For
Next N
If TextInput = "" Or Val(TextInput) < MinVal Or Val(TextInput) > MaxVal Or Val(TextInput) <> Int(Val(TextInput)) Or NumericInputErrorCheck = False Then
Message = MsgBox("Please enter an integer between " & MinVal & " and " & MaxVal & ".", vbExclamation, "Error")
NumericInputErrorCheck = False
TextInput.SetFocus
TextInput.Text = ""
End If
End Function
Which seems to work a treat when I call from a textbox subroutine:
Private Sub txtInput_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then If NumericInputErrorCheck(txtInput, 1, 10) Then Print "Correct!"
End Sub
BUT I have a few queries...
1) Do you think this is the best was of error checking for an integer value?
2) Why does my system beep when I hit return? Any way of stopping it?
3) In the standard module is it necessary to declare Option Explicit?
4) In the standard module should the variables Message & N be declared within the function itself?
5) Why does 'If KeyCode = vbKeyReturn Then If NumericInputErrorCheck....' work but 'If KeyCode = vbKeyReturn and NumericInputErrorCheck....' calls the function any time a key is pressed? I thought the whole point of AND was that both arguments had to be valid.
Your feedback would be much appreciated guys :)