Solved Input box exiting function when cancel is not selected!

slharman1

Member
Local time
Yesterday, 23:40
Joined
Mar 8, 2021
Messages
483
I have a function that does a multitude of things, it has an Input Box that returns a value, but if the user selects cancel for the input, the function should exit, if the input box returns a value, the function should continue to run.
In the code below my function keeps exiting at this line:
Code:
    If (varInput = InputBox("Enter quote number for new order:")) = False Then
        'Debug.Print
        Exit Function
    End If
 
when you press Cancel, the Return of the Inputbox is zero length string not False.
Code:
    varInput = InputBox("Enter quote number for new order:")
    If Len(varInput) < 1 Then
        'Debug.Print
        Exit Function
    End If
 
when you press Cancel, the Return of the Inputbox is zero length string not False.
Code:
    varInput = InputBox("Enter quote number for new order:")
    If Len(varInput) < 1 Then
        'Debug.Print
        Exit Function
    End If
Thanks Arnel - question, would isnull work for the test?
 
the return of Inputbox is a String, so you cannot use IsNull() since
if you Cancel the return is Not Null but a zero length string.
so you need to test it's Length.
 
when you press Cancel, the Return of the Inputbox is zero length string not False.
Code:
    varInput = InputBox("Enter quote number for new order:")
    If Len(varInput) < 1 Then
        'Debug.Print
        Exit Function
    End If
Arnel, help again please :)
I need some error handling fir when the user types text instead of a number.
 
PMFJI, but arnel has to sleep sometime? :)

Look at the Val() function perhaps?
 
Arnel, help again please :)
I need some error handling fir when the user types text instead of a number.
Arnel & Gasman
Never mind, I figured it out. You guys must be teaching me something!

This works:
Code:
    ElseIf IsNumeric(varInput) = False Then
        MsgBox "Enter Numbers Only", vbCritical, "Try again" 'Debug.Print
        Exit Function
 
Ok you could just say

Not IsNumeric(VarInput) ?

just another way.
 
It’s in a If......Then loop
Yes I know. :(
All I was trying to say, was that instead of checking for False, you could also check for True?, in which case there is no need for = True or False?

If IsNumeric(variable) = True is the same as If Isnumeric(variable)
If Not IsNumeric(variable) = True is the same as If Not Isnumeric(variable)

You would still use your If/Else construct, it is just the test I am talking about.
 
Yes I know. :(
All I was trying to say, was that instead of checking for False, you could also check for True?, in which case there is no need for = True or False?

If IsNumeric(variable) = True is the same as If Isnumeric(variable)
If Not IsNumeric(variable) = True is the same as If Not Isnumeric(variable)

You would still use your If/Else construct, it is just the test I am talking about.
Ahh... you teach me well master :) I get it now. Thanks
 

Users who are viewing this thread

Back
Top Bottom