Testing Form Text Entries

When using VBA forms for gathering text inputs you may want to test the values. This is particularly true when your application is expecting numeric text inputs. The examples below show two TextBox events that can be used to validate numeric entries. The default Change event tests the input after every key press. This even can create problems when you want to allow for negative values, floating point numbers less than one, or simply clearing out an existing value. The single characters of “-“ and “.” along with a zero length text string are not recognized as numeric values. In this case it may be better to use the AfterUpdate TextBox event which tests the values only when focus is moved to another form control.


'
' Tests value after every character entry
'
Private Sub TextBox1_Change()
If Not IsNumeric(TextBox1.Text) Then
MsgBox "Text entry must be a number", vbExclamation, "TextBox1"
End If
End Sub

'
' Tests value after leaving the text box
'
Private Sub TextBox1_AfterUpdate()
If Not IsNumeric(TextBox1.Text) Then
MsgBox "Text entry must be a number", vbExclamation, "TextBox1"
End If
End Sub

EnvisionCAD

Since 1996, EnvisionCAD has been a nationally recognized leader in the configuration, customization, implementation, training & support for CAD software solutions. Our individualized approach has benefited private engineering firms and government agencies alike. Basic or advanced, we can help you get the most from your CAD technology.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *