Help with cancel on inputbox (1 Viewer)

Gint32

Registered User.
Local time
Today, 11:41
Joined
Jan 21, 2018
Messages
39
I am struggling to catch the error when a user presses cancel with an Inputbox, so far I have tried to catch if a user presses cancel using
Code:
 If Sinput < 1 Or Sinput > 4 Or Sinput = "" Or Sinput = False Or Sinput = vbCancel Then
but none of the above seems to work, as the vba just skips out of the sub routine and gets on with the rest of the code. Can anyone show me how to work around this within VBA.
Incidentally, I have had a search for solutions and if from what I understand I could have used
Code:
application.inputbox
, then I would not be having this issue! but I don't think this is an option in VBA Access. Any input appreciated..:)
 

Ranman256

Well-known member
Local time
Today, 14:41
Joined
Apr 9, 2015
Messages
4,337
input box produces a value
val = inputbox("prompt","caption")

if user presses cancel, val = ""
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 19:41
Joined
Sep 12, 2006
Messages
15,661
The inputbox is a fairly low level tool, A lot of us use a purpose built
form to capture inputs. You can make it look like an input box, but behave rather better.
 

isladogs

MVP / VIP
Local time
Today, 19:41
Joined
Jan 14, 2017
Messages
18,247
Adding to Ranman's answer:

Code:
Dim strResponse As String
strResponse = inputbox("prompt","caption")

If strResponse="" Then Exit Sub

'otherwise continue with remaining code
 

Gint32

Registered User.
Local time
Today, 11:41
Joined
Jan 21, 2018
Messages
39
Adding to Ranman's answer:

Code:
Dim strResponse As String
strResponse = inputbox("prompt","caption")

If strResponse="" Then Exit Sub

'otherwise continue with remaining code

Many Thanks for your input, As I said in my OP. My VBA has your suggestion built in (and some), the issue that I am having, regardless if I use yours or mine, the VBA will still skip over and out of the rest of the code, so I really can't use the
Code:
vba if "" then
As below "Sinput in my variable for the inputbox, where yours in "VaL", but as you know there is no difference. When a user presses cancel the code doesn't get to the Then part of the VBA(as it jumps out of the routine completely)
Code:
If Sinput < 1 Or Sinput > 4 Or Sinput = "" Or Sinput = False Or Sinput = vbCancel Then
 

Gint32

Registered User.
Local time
Today, 11:41
Joined
Jan 21, 2018
Messages
39
The inputbox is a fairly low level tool, A lot of us use a purpose built
form to capture inputs. You can make it look like an input box, but behave rather better.

Thanks, for your response do you have any examples of what you are suggesting!
 

isladogs

MVP / VIP
Local time
Today, 19:41
Joined
Jan 14, 2017
Messages
18,247
Here's an example I use for entering passwords to restricted areas of databases



As Dave said, creating your own form can look better and do more things
Such forms can also be reusable in different contexts

Another example is a database login form
 

Attachments

  • Capture.PNG
    Capture.PNG
    7 KB · Views: 244

isladogs

MVP / VIP
Local time
Today, 19:41
Joined
Jan 14, 2017
Messages
18,247
thanks for that, so back to my OP does this mean its impossible to trap the error when a user presses cancel instead of imputing some data, and therefore the only way forward is as you suggested via the link

See post 4. Its easy!
 

Gint32

Registered User.
Local time
Today, 11:41
Joined
Jan 21, 2018
Messages
39
input box produces a value
val = inputbox("prompt","caption")

if user presses cancel, val = ""
Hi, If val = "" did actually ="" I 'd be able to error trap the code, but as is the case you actually can't as far I know so far!
the only way I can get val = "" is to not enter anything and press ok
But thanks anyway for replying
 

isladogs

MVP / VIP
Local time
Today, 19:41
Joined
Jan 14, 2017
Messages
18,247
That wasn't post 4!
Here's a modified version to check for null or ZLS

Code:
Dim strResponse As String
strResponse = Nz(Inputbox("prompt","caption"),"")

If strResponse="" Then Exit Sub

'otherwise continue with remaining code

Its always worked fine for me
 

Gint32

Registered User.
Local time
Today, 11:41
Joined
Jan 21, 2018
Messages
39
See post 4. Its easy!

thanks once again Ridders, I just found out that the issue I have been having is in this line of code .. although I no not why, reasons being I changed it to
Code:
 If sinput = "" Then
and it runs as it should. Any ideas what is wrong with the following line of code?
Code:
If Sinput < 1 Or Sinput > 4 Or Sinput = "" Or Sinput = False Or Sinput = vbCancel Then
 

isladogs

MVP / VIP
Local time
Today, 19:41
Joined
Jan 14, 2017
Messages
18,247
thanks once again Ridders, I just found out that the issue I have been having is in this line of code .. although I no not why, reasons being I changed it to
Code:
 If sinput = "" Then
and it runs as it should. Any ideas what is wrong with the following line of code?
Code:
If Sinput < 1 Or Sinput > 4 Or Sinput = "" Or Sinput = False Or Sinput = vbCancel Then

Sinput is a text string so it cannot equal any of the other items unless that was the exact text entered... and to detect you would need to enclose in quotes
"1", "4", "false" etc. I'm sure that wasn't what you thought you were doing
 

Gint32

Registered User.
Local time
Today, 11:41
Joined
Jan 21, 2018
Messages
39
Sinput is a text string so it cannot equal any of the other items unless that was the exact text entered... and to detect you would need to enclose in quotes
"1", "4", "false" etc. I'm sure that wasn't what you thought you were doing

makes sense now many Thanks,
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 28, 2001
Messages
27,226
The one gap in this logic is if someone hit space and then said, "Oh, the heck with it."

Code:
If Trim$( Nz( sInput, "" ) ) = "" Then ....
 

Gint32

Registered User.
Local time
Today, 11:41
Joined
Jan 21, 2018
Messages
39
The one gap in this logic is if someone hit space and then said, "Oh, the heck with it."

Code:
If Trim$( Nz( sInput, "" ) ) = "" Then ....
thanks for the input I'll amend to reflect!
 

Users who are viewing this thread

Top Bottom