473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

can i get a hand seeing why this wont work?

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?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdT4e_Click()
  2.  
  3. Dim dblSqrNum As Double
  4. Dim dblSqrRes As Double
  5. Dim blnNumChk As Boolean
  6. Dim strFinRes As String
  7.  
  8. dblSqrNum = InputBox("please enter a number to square root", "input", 1)
  9. On Error GoTo ErrHandler
  10.  
  11. blnNumChk = IsNumeric(dblSqrNum)
  12.  
  13. If blnNumChk = False Then
  14.     MsgBox ("you cannot square root a letter. please enter a positive number")
  15.     Exit Sub
  16. Else
  17. End If
  18.  
  19. Select Case dblSqrNum
  20.  
  21. Case Is < 0
  22.     MsgBox ("invalid entry. number must be positive")
  23.     Exit Sub
  24.  
  25. Case Is = 0
  26.     MsgBox ("invalid entry. number must be greater than zero")
  27.     Exit Sub
  28. End Select
  29.  
  30. ErrHandler: MsgBox ("exited")
  31.  
  32. dblSqrRes = Sqr(dblSqrNum)
  33.  
  34. strFinRes = "the square root of " & dblSqrNum & " = " & dblSqrRes
  35.  
  36. txtT4e.Text = strFinRes
  37.  
  38. End Sub
Oct 24 '07 #1
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.
Oct 24 '07 #2
kadghar
1,295 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. sub test()
  2. dim dou1 as double
  3. on error goto errlabel
  4. dou1 = inputbox("write a number")
  5. 'Write here your code
  6. exit sub 'will terminate the sub when your code is done, but its not the end of the sub.
  7. errlabel:'if you had an error, it will only run the lines after this one.
  8. msgbox("you had an error")
  9. 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
Oct 24 '07 #3
Killer42
8,435 Expert 8TB
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.
Oct 25 '07 #4
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?
Oct 25 '07 #5
kadghar
1,295 Expert 1GB
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.
Oct 25 '07 #6
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
Oct 25 '07 #7
Killer42
8,435 Expert 8TB
... 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.
Oct 25 '07 #8
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
Oct 31 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

19
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,...
6
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...
4
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...
2
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...
5
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...
19
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...
17
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...
3
Frinavale
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...
0
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"...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
1
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)...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.