Library code snippets
Validating textbox entries
When you retrieve a numeric value from a textbox, you may want to compare
it to some other preset value. For example, consider the following code:Select Case Text1.Text
Case 1 to 12
MsgBox "Acceptable Value"
Case Else
MsgBox "Unacceptable Value"
End Select
You might think that this code would compare a numeric value in Text1 and
return Acceptable Value for values 1 through 12, and Unacceptable Value
for all other numbers. Instead, this code snippet displays the Acceptable
Value message for only 1, 10, 11, and 12, but not 2, 3, 4, etc. That's
because a textbox's Text property returns the value as a string, and so
compares them as such in the Select Case statement.
To avoid this unexpected glitch, convert the numbers with the Val()
function. This function automatically converts a string into the
appropriate type: integer, long, single, or double. The modified code
would look as follows:Select Case Val(Text1.Text)
Case 1 to 12
MsgBox "Acceptable Value"
Case Else
MsgBox "Unacceptable Value"
End Select
Related articles
Related discussion
-
Key_Press() event for text box
by Aquila (1 replies)
-
Regarding Visual Basic Programme
by manjunathsl2007 (0 replies)
-
how do you hide all in VB6
by CapnJack (1 replies)
-
Problem with Input File
by novavb6 (3 replies)
-
How to produce a txt file with a table??
by novavb6 (1 replies)
This thread is for discussions of Validating textbox entries.