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
Validating textbox entries
By ElementK Journals, published on 14 Jul 2001
| Filed in
You might also like...
VB 6 forum discussion
-
CorelDRAW VBA: cdrTraceLineDrawing FAILS, producing single linear path instead of Centerline trace?
by dancemanj (0 replies)
-
client/server application using activex
by beautifulheart (0 replies)
-
System Error &H8007007E. The specifed module could not be found.
by swiftsafe (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
-
Send SMS with SMPP
by mmahmoud (0 replies)
VB 6 podcasts
-
Stack Overflow Podcast: Podcast #45 – Keeping it Sharp
Published 7 years ago, running time 0h54m
Our guest this week is Eric Lippert – language architect extraordinaire and famous for all his work at Microsoft in developing their languages Eric joined Microsoft right out of college and was originally working on VB It’s time for everyone’s favorite game: Name the Worst Feature of that Microso.
Comments