Hi all,

What code can I use whereby if the user times in anything other than a number an error message will prompt them.

[U]If NOT val(Textbox1.value) then[/U]
    Msgbox("Sorry you must only put numbers")
else
end if

The line underlined, does not work. Please can someone tell me what the right code is? Thank you very much for your help.

Ini

Recommended Answers

All 5 Replies

If NOT [B]isNumberic[/B](Textbox1.value) then
    Msgbox("Sorry you must only put numbers")
else
end if
If NOT [B]isNumberic[/B](Textbox1.value) then
    Msgbox("Sorry you must only put numbers")
else
end if

It's a cleaner code method to not include an else unless it's being used. Also note that VB6's textbox's do NOT support the .value property, instead, use the .text property. So, the above code example:

if not isnumeric(text1.text) then
     msgbox("Sorry you must only put numbers")
     exit sub
end if

It's a cleaner code method to not include an else unless it's being used.

True, but for the question asked the else was unnessesary to expand because this is an example of how to use isNumeric, not an example of how to design an IF. And all I did was answer the question
asked, I didn't correct his value/text mistake because he would find it quickly enough, and the focus was on the question asked.

True, but for the question asked the else was unnessesary to expand because this is an example of how to use isNumeric, not an example of how to design an IF. And all I did was answer the question
asked, I didn't correct his value/text mistake because he would find it quickly enough, and the focus was on the question asked.

Ok Waltp,

I understand where you are coming from, and that's ok, but understand this also: the forum is not simply meant to answer questions, it's meant to help people become better programmers in the VB Language. Some people who come to this site, have a difficult enough time, trying to differentiate something as simple as the differences between certain data types. As people who help others, it's a responsibility of ours to not only answer the question, but to answer the question correctly and precisely. Programming is a practice of precision, and so should our answers be.... if we encourage poor programming practices (such as using else's in an if block when it's not necessary) we our only making things more difficult for ourselves, when the user posts his nearly completely project with a bunch of empty else's in it. I'm sorry If I came off harsh in my reply by constructively criticizing your code, but I just want to make sure that INI is informed to the very best that we can make him informed....

That said, if we take a closer look at INI's code, it would appear that it's most likely being done in VB.NET, or VB Express (or even VBA) in which case .value would be accurate.... he uses the name textbox1, which indicates that this code is Not being doing in Legacy VB, since by default the name of a textbox in legacy VB is text1, not textbox1. If the problem is solved, however, we should mark this thread as solved, and move on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.