Solved Input box exiting function when cancel is not selected! (1 Viewer)

slharman1

Member
Local time
Today, 09:21
Joined
Mar 8, 2021
Messages
476
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:21
Joined
May 7, 2009
Messages
19,230
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
 

slharman1

Member
Local time
Today, 09:21
Joined
Mar 8, 2021
Messages
476
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?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:21
Joined
May 7, 2009
Messages
19,230
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.
 

slharman1

Member
Local time
Today, 09:21
Joined
Mar 8, 2021
Messages
476
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:21
Joined
Sep 21, 2011
Messages
14,257
PMFJI, but arnel has to sleep sometime? :)

Look at the Val() function perhaps?
 

slharman1

Member
Local time
Today, 09:21
Joined
Mar 8, 2021
Messages
476
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:21
Joined
Sep 21, 2011
Messages
14,257
Ok you could just say

Not IsNumeric(VarInput) ?

just another way.
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:21
Joined
Sep 21, 2011
Messages
14,257
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.
 

slharman1

Member
Local time
Today, 09:21
Joined
Mar 8, 2021
Messages
476
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

Top Bottom