dcsimg
Results 1 to 5 of 5

Thread: Input Box Error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    76

    Unhappy Input Box Error

    Hello, I have this minor problem in my Inputbox,

    here's my code

    Code:
    Dim price As Double
    price = 40
    
    Dim qty As Integer
    If chkIcedTea = vbChecked Then
        lstItem.AddItem chkIcedTea.Caption
        qty = InputBox("Enter Quantity:", "Input Quantity")
            
        lstQty.AddItem qty
        Call multiplier(price, qty)
        lstPrice.AddItem sum
       
    End If

    the error is "type mismatch" everytime I click the cancel and when the inputbox in empty. How can I avoid this error?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,993

    Re: Input Box Error

    qty is a numeric variable, InputBox returns a string value. If you try to set a numeric variable to a string that cannot be converted to some numeric value, type mismatch error.

    Suggest: qty = Val(InputBox("Enter Quantity:", "Input Quantity"))
    That still isn't perfect as it can still generate errors if someone provides an extremely long number. You may want to include error checking. If an error occurs, display a message something like: Invalid value, please try again"
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    76

    Re: Input Box Error

    How can I check if the Input contains a letter, that's one of my problem, anyway, thanks for the code it works..

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,993

    Re: Input Box Error

    Couple things:

    1) If InputBox returns "", then either user hit OK with no input entered or hit the Cancel button
    2) IsNumeric() may be helpful
    3) Sill nothing but error checking will help if someone adds an extremely long number in the box

    Code:
    Dim strInPut As String
    ...
        strInput = InputBox("Enter Quantity:", "Input Quantity")
        If strInput <> "" Then
            If IsNumeric(strInput) = False Then
                  ' add a msgbox & invalid entry warning
            Else
                  On Error Resume Next
                  qty = CInt(strInput)
                  If Err Then
                       ' add a msgbox & invalid entry warning
                  Else
                       On Error GoTo 0 ' turn off error checking
                       ... rest of your code
                  End If
            End If
        End If
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    76

    Re: Input Box Error

    thank you, It works!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width