Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Make Cancel Button Work In InputBox 2

Status
Not open for further replies.

Buddy008

Programmer
Sep 29, 2003
11
US
How do you make the cancel button actually work in an inputbox??
 
can you be a bit more precise?

What does it do,
What do you want it to do?

It normally should just close the inputbox.
 
The InputBox function returns an empty string when Cancel is pressed. Note it will also return an empty string if OK is pressed and there is nothing in the textbox.

Paul Bent
Northwind IT Systems
 
Buddy008,
If u are assining value of the Input Box to a numeric variable , and u press cancel or ok without entering anything in the text box , then only it raises an error . Otherwise I never really needed any code for Cancel button

Good luck

Vikram
 
I have the Inputbox as a search, and I have it protected so that if you accidentally don't type any text an click ok a msgbox pops up and it allows you to try again. However if someone clicks on the search accidentally cancel won't work to exit the sub. so basically right now you must enter some sort of text to get out of the inputbox. I tried cancelerror = true and then an error handler to exit the sub, but no luck... any other suggestions??

thanks,
Josh
 
You need to use the Msgbox function, ask the user if they wish to try again or cancel and provide two buttons. Check the MsgBox function return and proceed accordingly. Something like:

Dim intMR As Integer 'Msgbox function return
Dim strInput As String

Retry:
strInput = InputBox$("Enter search phrase", , _
"Text Search", 180, 180)
If strInput = vbNullString Then
'Get confirmation to cancel
intMR = Msgbox("Do you want to cancel the search?", _
vbYesNo, "Confirm Cancel")
If intMR = vbNo Then
GoTo Retry
Else
Exit Sub
End If
End If

Paul Bent
Northwind IT Systems
 
Buddy008,

If you thought there was no way to tell the difference if the user pressed Cancel or OK on an InputBox, you're wrong.

You see, if the user presses cancel, vbNullString is returned. However if they press OK, the empty string ("") is sent back.

But in Visual Basic, vbNullString equates to an empty string, so you can't compare the two - even though in reality, they're completely different.

However you can use StrPtr to evaluate the difference between the two.

If the user had pressed Cancel StrPtr of the returned string is 0, else it is some value > 0.
 
Razvi, the information you provided about the return value of the InputBox function was worth knowing. I am sure most of the people like me were not aware of this... How to detemine whether the user entered a blank value or pressed the Cancel button...?

Have a star for providing this great piece of information. You really deserve it. (and many more from other members as well!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top