|
-
Oct 28th, 2009, 11:23 PM
#1
Thread Starter
New Member
need help checking for number in vb6
Hi guys i'm working on some code whereby the user inputs a distance and time they took spent running for days 1 to 6 and it works fine however I can't get it to display a message box if the user inputs letters instead of numbers. I've had a go at trying to do it and you can see my attempt in the code below and btw this is a module in case it helps.
vb Code:
Sub Main()
'Declaring variables used in the program
Dim distance As Integer
Dim time, totaldistance, totaltime, average As Single
'Loop that calls the following functions to display the message box 6 times and change the day
For i = 1 To 6
distance = Val(InputBox("Enter the km travelled on day" + Str$(i)))
If IsNumeric(distance) = False Then
MsgBox ("please enter a number")
End If
time = Val(InputBox("Enter the minutes taken on day" + Str$(i)))
If IsNumeric(time) = False Then
MsgBox ("please enter a number")
End If
totaldistance = totaldistance + dist
totaltime = totaltime + time
Next i
'Calculates and displays the total time, distance and kmph
MsgBox ("The total distance travelled over the week =" + Str$(totaldistance) + "kms")
MsgBox ("The total time taken running over the week =" + Str$(totaltime) + " minutes")
average = totaldistance / totaltime * 60
MsgBox ("Your average speed is =" + Str$(average) + " kmph")
End Sub
Last edited by Hack; Oct 29th, 2009 at 12:35 PM.
Reason: Added Highlight Tags
-
Oct 29th, 2009, 12:28 AM
#2
Re: need help checking for number in vb6
u can avoid the non numeric values by keypress event also, but user can paste the nonnumeric value into the textbox, so u can restrict nonnumeric value by keypress event and by change event u can give a msgbox if the pasted value is nonnumeric, hope u understand, if any doubt i wil giv u some sample code later
-
Oct 29th, 2009, 03:36 AM
#3
Re: need help checking for number in vb6
Code:
Sub Main()
'Declaring variables used in the program
Dim distance As Integer
Dim time, totaldistance, totaltime, average As Single
'Loop that calls the following functions to display the message box 6 times and change the day
For i = 1 To 6
aa:
distance = Val(InputBox("Enter the km travelled on day" + Str$(i)))
If IsNumeric(distance) = False Then
MsgBox ("please enter a number")
goto aa
End If
bb:
time = Val(InputBox("Enter the minutes taken on day" + Str$(i)))
If IsNumeric(time) = False Then
MsgBox ("please enter a number")
goto bb
End If
totaldistance = totaldistance + dist
totaltime = totaltime + time
Next i
'Calculates and displays the total time, distance and kmph
MsgBox ("The total distance travelled over the week =" & Str$(totaldistance) + "kms")
MsgBox ("The total time taken running over the week =" & Str$(totaltime) + " minutes")
average = totaldistance / totaltime * 60
MsgBox ("Your average speed is =" & Str$(average) & " kmph")
End Sub
The above code uses two labels... It may work... 
For concatenation, use & rather than +
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 29th, 2009, 12:36 PM
#4
Re: need help checking for number in vb6
Also, in this line
Code:
Dim time, totaldistance, totaltime, average As Single
only the variable "average" is a Single. All the rest a Variants. If you need all of them to be a Single, then you need to change that line of code to
Code:
Dim time As Single, totaldistance As Single, totaltime As Single, average As Single
Also also, you should consider something other than the word "time" as a Variable name. Time is a reserved word in just about every language I've ever encountered. Using reserved words for variables is a great way to cause yourself endless problems.
-
Oct 29th, 2009, 02:09 PM
#5
Re: need help checking for number in vb6
You can use my NumberBox ActiveX control. When you use it it will not only just allow numbers but you can also say if negatives are allowed and how many decimal places can be entered.
-
Oct 29th, 2009, 08:30 PM
#6
Re: need help checking for number in vb6
only the variable "average" is a Single. All the rest a Variants. If you need all of them to be a Single, then you need to change that line of code to
Code:
Dim time As Single, totaldistance As Single, totaltime As Single, average As Single
Thanks Hack... This is a new knowledge to me...
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|