|
-
Nov 26th, 2011, 10:25 PM
#1
Thread Starter
Lively Member
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?
-
Nov 26th, 2011, 10:41 PM
#2
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"
-
Nov 26th, 2011, 10:52 PM
#3
Thread Starter
Lively Member
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..
-
Nov 26th, 2011, 10:58 PM
#4
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
-
Nov 26th, 2011, 11:46 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|