Hey there. I'm a mechanical engineer and we're learning a bit of VB for computer control and instrumentation. Our lecturer is always going on about how if he is presented with a text box or input box asking for a number, he'll go and put in a letter or some such just to see if we've covered all the bases.
Now he hasn't actually told us how this should be done, so I've been searching and found the IsNumeric thing and it still doesn't work. If I put a letter in, I get an error saying type mismatch!
Also, when the cancel button is pressed, that just errors out as well! How do I stop that?
Can anyone help? Am I doing it right? - Private Sub cmdT4e_Click()
-
-
Dim dblSqrNum As Double
-
Dim dblSqrRes As Double
-
Dim blnNumChk As Boolean
-
Dim strFinRes As String
-
-
dblSqrNum = InputBox("please enter a number to square root", "input", 1)
-
On Error GoTo ErrHandler
-
-
blnNumChk = IsNumeric(dblSqrNum)
-
-
If blnNumChk = False Then
-
MsgBox ("you cannot square root a letter. please enter a positive number")
-
Exit Sub
-
Else
-
End If
-
-
Select Case dblSqrNum
-
-
Case Is < 0
-
MsgBox ("invalid entry. number must be positive")
-
Exit Sub
-
-
Case Is = 0
-
MsgBox ("invalid entry. number must be greater than zero")
-
Exit Sub
-
End Select
-
-
ErrHandler: MsgBox ("exited")
-
-
dblSqrRes = Sqr(dblSqrNum)
-
-
strFinRes = "the square root of " & dblSqrNum & " = " & dblSqrRes
-
-
txtT4e.Text = strFinRes
-
-
End Sub
8 1221
ha sods law
the moment i post this, i figure it out
Private Sub cmdT4e_Click() Dim strInput As String
Dim dblSqrNum As Double
Dim dblSqrRes As Double
Dim blnNumChk As Boolean
Dim strFinRes As String strInput = InputBox("please enter a number to square root", "input", 1)
blnNumChk = IsNumeric(strInput)
If blnNumChk = False Then
MsgBox ("you cannot square root a letter. please enter a positive number")
Exit Sub
Else
blSqrNum = CDbl(strInput)
End If
Select Case dblSqrNum
Case Is < 0
MsgBox ("invalid entry. number must be positive")
Exit Sub
Case Is = 0
MsgBox ("invalid entry. number must be greater than zero")
Exit Sub
End Select
dblSqrRes = Sqr(dblSqrNum)
strFinRes = "the square root of " & dblSqrNum & " = " & dblSqrRes
txtT4e.Text = strFinRes
End Sub
added the bits in bold in and it works, wouldnt mind a confirmation that i've done it correctly though. im not sure if the number im eventually getting out the check is a double, but it seems to give correct answers.
also still stuck on what to do if the cancel button is pressed.
ha sods law
the moment i post this, i figure it out
added the bits in bold in and it works, wouldnt mind a confirmation that i've done it correctly though. im not sure if the number im eventually getting out the check is a double, but it seems to give correct answers.
also still stuck on what to do if the cancel button is pressed.
it's better if you keep what you had, read numeric values into a numeric variable, strings might give you some troubles later, since if a =1 and b=2
a+b = 3 (if they're numeric)
a+b= 12 (if they're strings)
"On error goto label " is an statement that will determine what to do in case of further errors, so all you have to do is put that line before the inputbox, this way, when the inputbox has an error will know what to do.
now, when you send to a label, it will start running from it to the end, so what i also recomend you is to have something like: - sub test()
-
dim dou1 as double
-
on error goto errlabel
-
dou1 = inputbox("write a number")
-
'Write here your code
-
exit sub 'will terminate the sub when your code is done, but its not the end of the sub.
-
errlabel:'if you had an error, it will only run the lines after this one.
-
msgbox("you had an error")
-
end sub 'this is the end
Oh, and when they hit "cancel" if you had a string it will be "" (an empty one)
if you had a numeric value it will be set to 0 (zero)
if you dont want that to happen you can always put something like
if dou1=0 then goto errlabel
HTH
Your lecturer makes a good point. In programming, you really have to assume that the end-user will do any stupid thing that is possible, so you need to handle it gracefully, without "crashing".
That said, there's usually more than one way to go about it. In this case, you have come up with the idea of using a string to accept the input, and checking it for validity before continuing. kadghar, on the other hand, prefers to use a numeric field, and use an error handler to capture the error that occurs when a non-numeric value is entered.
Both methods are valid, and each have their own good and bad points. For instance, using the On Error technique means that you cannot (as far as I know) access the actual value that was entered. Normally this wouldn't matter. But it might be an issue if, for example, you wanted to tell the user that "ABC is not a valid entry".
You just need to decide which is the more appropriate method for your circumstances. From the sound of it, the error handler will certainly be needed in any case, to deal with the Cancel button.
Personally, I would lean more toward using a string to accept the input, then validating it, then moving it to a numeric field. As kadghar pointed out, you don't want to keep it in a string variable, as this can confuse things when you try to use the value later.
cheers lads
so can i just ask then
will doing this
dblSqrNum = CDbl(intInput)
does that give me a double ? or is it still a string?
cheers lads
so can i just ask then
will doing this
dblSqrNum = CDbl(intInput)
does that give me a double ? or is it still a string?
well that depends on what you have in the string, if its a number in any format and it doesnt exced de Double's intervals, I'd say you'll succed. but if you write "hello", it'll show you an error.
well that depends on what you have in the string, if its a number in any format and it doesnt exced de Double's intervals, I'd say you'll succed. but if you write "hello", it'll show you an error.
thanks man, thats all i need to know,
it wont get to that stage because any letters or non-numbers will of thrown an error message and exited the sub because of my check.
gonna try the error handler way, that seems to be more useful
... gonna try the error handler way, that seems to be more useful
Well, like I said, you really need to have an error handler in any case, to deal with the "Cancel" button. So I suppose you might as well use it for the type checking too.
Let us know how it turns out.
works well i think, kept the boolean check thingy for the checking what has been entered, and used the error handler for the cancel button
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Allen Thompson |
last post by:
sorry for the simple question, haven't done this in a while. when I use the
following script it keeps displaying the value of "x" like a string. for
example, if I type the number 7 in the prompt,...
|
by: lostinspace |
last post by:
After four+ years of using FrontPage in a limited capacity and having
created over 600 pages on my sites, I've finally (at least for the most
part) abandoned FP, to begin the process of converting...
|
by: jake |
last post by:
Maybe some kind person can help with a suggestion or two ;-)
I want to construct a banner on a number of pages in the format:
LHS: Text (variable content) RHS. Logo
..... each with a...
|
by: Justin Lazanowski |
last post by:
I have a form that programmatticly creates 5 labels. Then programatticly
should dispose them, when this happens when it gets down to two lables left
it says that I get a null ref exception.. and I...
|
by: Mr Newbie |
last post by:
Debug.Assert( False, "Why wont I display ??")
I am trying to use this in my code but it wont display. The app is running
on my local machine and the above code under a button click event.
What...
|
by: CES |
last post by:
All,
I don't know why I always get stuck on the stupid questions...
This is pretty straight forward... I want to change the cursor to the hand on a mouse event. This works just fine, if I use...
|
by: M.Siler |
last post by:
I'm trying to get my head around a conversation I had with a developer the
other day. We were talking about Codesmith vs. Hand coding. He's position is
Codesmith is for junior to mid level...
|
by: Frinavale |
last post by:
Hi there,
I am currently behind a firewall that wont let me send emails.
As a result my smtpClient.send(emailMessage) wont work on my IIS but this method does work through the VisualStudio...
|
by: watashi |
last post by:
Hello,
This is on socket. i m using tcpclient and tcplistener
task is when the "Active " button is pressed according to user choice tool should work as server or client.
when "Inactive"...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |