Results 1 to 6 of 6

Thread: need help checking for number in vb6

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    8

    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:
    1. Sub Main()
    2. 'Declaring variables used in the program
    3. Dim distance As Integer
    4. Dim time, totaldistance, totaltime, average As Single
    5.  
    6. 'Loop that calls the following functions to display the message box 6 times and change the day
    7. For i = 1 To 6
    8. distance = Val(InputBox("Enter the km travelled on day" + Str$(i)))
    9. If IsNumeric(distance) = False Then
    10. MsgBox ("please enter a number")
    11. End If
    12.  
    13. time = Val(InputBox("Enter the minutes taken on day" + Str$(i)))
    14. If IsNumeric(time) = False Then
    15. MsgBox ("please enter a number")
    16. End If
    17.  
    18. totaldistance = totaldistance + dist
    19. totaltime = totaltime + time
    20. Next i
    21.  
    22. 'Calculates and displays the total time, distance and kmph
    23. MsgBox ("The total distance travelled over the week =" + Str$(totaldistance) + "kms")
    24. MsgBox ("The total time taken running over the week =" + Str$(totaltime) + " minutes")
    25. average = totaldistance / totaltime * 60
    26. MsgBox ("Your average speed is =" + Str$(average) + " kmph")
    27. End Sub
    Last edited by Hack; Oct 29th, 2009 at 12:35 PM. Reason: Added Highlight Tags

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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
  •  



Click Here to Expand Forum to Full Width