-->>Hi,I have developed a Program that accepts the inputs from an Input Box of Interger datatype,but whenever I try to Cancel the Input Box (That means if I decided not to provide any value and Cancel it) I get an Error saying "Type Mismatch" My Variable assigned to It's declared: Dim LQ_Value As Integer and the line that generates an erro is:LQ_Value = InputBox("Plaese Enter The Number of LQ Forms Entered:", "Number of LQ Required.", "Example: 25", 7500, 4000).
-->>So I was wondering if I can have a way to omit this error by validating it...

4 Years
Discussion Span
Last Post by jhai_salvador

First off inputbox returns a string and your trying to put it into an integer, which might work if the string represents an integer. When it doesn't you get a mismatch. Changing LQ_Value to a string then converting its value to an integer later would work. A simple conditional statment to check if the string begins with an "E" would allow you to catch cancellation of the input.

Edited by tinstaafl


Nope, inpitbox can accept any value given to it. You have to state that cancel can be accepted by doing it as such -

''In your code BEFORE you open the input box...

On Error GoTo Err

Inputbox ... ''Rest of your inputbox code here...

Exit sub

''User pressed cancel button...
Msgbox "No value entered."
''Do some more stuff here if you like.

Inputbox is accepting the input. It's when you try and assign a string that doesn't represent an integer to an integer variable that you get the type mismatch error. Which is what's happenning here because the default value of the inputbox is "Example: 25"(a non-integer string). When the inputbox is cancelled that's the value passed to LQ_Value(an integer), which creates a 'type mismatch' error. As per my original message changing LQ_Value to a string fixes that and allows the programmer to check for a non-integer string and handle it however he likes. It's a simple matter to use the Val function to convert it to an integer.


Follow what AndreRet said. Error handler will do the trick.

Another method would be doing it like this (like what tinstaafl is saying):

Dim x As String
x = InputBox("Input a number")
MsgBox Val(x)

variable x is string since the return of InputBox is also a string.


-->>Thank you tinstaafl,AndreRet and jhai_salvador...
-->>I hope that the Error Handler is somewhat owesome...
-->>Though I thought that there may be a way round out of Error Handler to Validate this..
-->>I mean like may be a function or something like that...
-->>Thak you...


Only a pleasure. The error trapping ios still the best functionality here. :)

Please mark this as solved, thanx.


Well the snippet I posted does not need an error handler since inputbox will return a string so we better put it to string first then after that you can do your things on the return value.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.