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. Thanks in advance ;)

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 [/vb6][code=vb6]
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 [/vb6]

Hi,
it doesn't work because the variables distance and time are declared as integer. So if you put down in your inputbox for the distance e.g. the word "apple", it will automatically convert to value 0... and 0 is numeric. The solution for your problem is in declaring those two variables as Variant or as String, and after IsNumeric test convert it by using CInt(for converting into integer) and CSng(for converting to sinlge) functions. For example:

'Declaring variables used in the program
Dim distance As String, time As String
Dim iDistance as Integer
Dim sngTime as single, totaldistance as single , totaltime as single, 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 = (InputBox("Enter the km travelled on day" + Str$(i)))
If IsNumeric(distance) = False Then
MsgBox ("please enter a number")
End If

iDistance = CInt(Distance)

'...................the code continues here

You should also notice I added "As Single" after every variable in declaration... If you use it your way, it means that only var "average" is declared as single, but "time", "totaldistance" and "totaltime" are declared as Variant. And don't use the Val function. It will also convert your "apple" to "0", even the sDistance is as string.

Hope this is helpful for you. Enjoy.

commented: very helpful, thanks +1

You can also eliminate the message box, and keep the focus on the textbox/Inputbox if any value other than an integer has been entered i.e. -

If Not IsNumeric(distance) Then
        SendKeys "{BackSpace}"
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.