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
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
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 +
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.
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.
Re: need help checking for number in vb6
Quote:
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... :)