Jump to content
  • Advertisement
Sign in to follow this  
Clotty

Visual Basic 6 Question

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've made a few applications, but I just want to spruce them up a bit and be more professional. If for an integer value someone decides it would be funny to enter a string, how can I create a msgbox or inputbox that will pop-up if someone enters the wrong data type? For now, I can control it if someone enters a bad value such as a number less than 1,000 or more than 10,000 (supposing the input needed in the text box is an integer), but if someone enters a string, the application breaks down. I tried using the NOT statement but it still didn't work for me when a string was entered when an integer was need. How can I fix this? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Wow, even thinking about this took me back 5 years to high school [grin]

My VB is a bit rusty, so take everything below with a pinch of salt, and double-check it on MSDN...

Use the IsNumeric function on the string. That will return a Boolean value telling you whether the value is a number or not. IsNumeric("42") and IsNumeric("99.9") will return True, IsNumeric("57 varieties") will return False. In the case of someone entering 99.9, VB should cast it to 99 if you store the String in an Integer.

Share this post


Link to post
Share on other sites
Quote:
Original post by iNsAn1tY
Wow, even thinking about this took me back 5 years to high school [grin]

My VB is a bit rusty, so take everything below with a pinch of salt, and double-check it on MSDN...

Use the IsNumeric function on the string. That will return a Boolean value telling you whether the value is a number or not. IsNumeric("42") and IsNumeric("99.9") will return True, IsNumeric("57 varieties") will return False. In the case of someone entering 99.9, VB should cast it to 99 if you store the String in an Integer.



I haven't learnt all of the functions on VB6 yet, but I'm assuming you mean I do something like this.

If (intvariable <> IsNumeric()) Then
response = msgbox() ...

and so on.

Am I right?

Also, do you have to define IsNumeric by putting the kind of acceptable numbers in, or is it already built in with the function.

Finally, what if I wanted to do things the other way round with a string being the only acceptable input.

Thanks in advance.

Share this post


Link to post
Share on other sites

on error goto errh
dim x as integer
sof:
x = inputbox("inty please")
' stuff
exit sub
errh:
msgbox "You moron! i asked for an integer!"
goto sof
end sub


and if you don't understand that..... you've come to the wrong language...

from,
Nice coder

Share this post


Link to post
Share on other sites
Thanks for trying to help I suppose, but that really doesn't tell me much... at all really ("'STUFF" for example). In fact it looks nothing like VB6.

Is it just me? Any other way I could go about this?

*is confused*

Share this post


Link to post
Share on other sites
It is VB6.

Stuff was a comment - that's where you would do whatever it is you want to do with the inputted data.


Private Sub Command1_Click()
On Error GoTo errh:

Dim X As Integer

sof:
X = InputBox("Enter an integer")

'Do whatever it is you want to do

Exit Sub 'This is so the error handling (stuff following the errh: marker) won't run if it doesn't need to

errh: 'This is your error handling
MsgBox "You moron! I asked for an Integer!"
GoTo sof: 'Ask the user for input again

End Sub


Try putting that into a form with a command button called Command1 to see how it works. Do you understand it yet?

Share this post


Link to post
Share on other sites
Oh, I see. When it's written that way it's mich ore easier to understand. I hadn't seen the On Error command either, but I've got the hang of it now. Thanks ... all of you.

Share this post


Link to post
Share on other sites
Quote:
Original post by iNsAn1tY
Use the IsNumeric function on the string. That will return a Boolean value telling you whether the value is a number or not. IsNumeric("42") and IsNumeric("99.9") will return True, IsNumeric("57 varieties") will return False. In the case of someone entering 99.9, VB should cast it to 99 if you store the String in an Integer.

I would recommend the above solution over the use of trapping an error. The above way, you know for sure that the value entered is not an integer, whereas in the error trapping example, you do not know for sure that the reason an error occurred is because text was entered instead of a number. (You could of course check the value of Err.Number, but that's a lot more work.) I would recommend doing the following:

Private Sub Command1_Click()
Dim strInput As String
Dim dblValue As Double

' Accept a string value, and continue to do so until a numeric-like string is provided.
Do
strInput = InputBox("Please enter an integer.")
Loop Until IsNumeric(strInput)

' Convert the string input to a double (numeric) value.
dblValue = CDbl(strInput)
End Sub

Share this post


Link to post
Share on other sites
Private Sub Command1_Click()
Dim strInput As String
Dim dblValue As Double

' Accept a string value, and continue to do so until a numeric-like string is provided.
Do
strInput = InputBox("Please enter an integer.")
Loop Until IsNumeric(strInput)

' Convert the string input to a double (numeric) value.
dblValue = CDbl(strInput)

End Sub


Thanks for the help, but why would I want to use these two lines?

' Convert the string input to a double (numeric) value.
dblValue = CDbl(strInput).

Also is there something like an IsString() function or something if I wanted to do things the other way round?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!