InputBox Cancel

  • Thread starter Thread starter Wembly
  • Start date Start date
W

Wembly

Is it possible to trap the Cancel click event when the
user clicks on it from an InputBox?
 
Wembly,

You can take advantage of the fact that clicking Cancel returns a 0-length
string. Of course, if you click OK with NOTHING in the input line, you'll
also get a 0-length string; use a default value to make sure an empty input
box is a deliberate choice.

HTH,

Eric

-----------------------
Sub testinput()
Dim vInput As Variant

vInput = InputBox("Enter something and click", "InputBox Test",
"something")
Select Case Len(vInput)
Case 0 'Cancel clicked (or input area cleared)
Debug.Print "Cancel"
Case Else 'OK clicked
Debug.Print "OK"
End Select
End Sub
 
from Access Help Example:
'******************************************
Dim Message, Title, Default, MyValue
Message = "Enter a value between 1 and 3"
Title = "InputBox Demo" ' Set title.
Default = "1" ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)

The variable MyValue contains the value entered by the
user if the user clicks OK or presses the ENTER key . If
the user clicks Cancel, a zero-length string is returned.
'********************************************

So, the answer you seek is this:
Use a Dim statement to declare a Variant variable.
Use this variant to capture the value of the input.
Then check/trap on the result (looking for a zero-length
string):

MyValue = InputBox(Message, Title, Default)
If MyValue = "" Then 'Cancel was selected
 
I'd suggest you use an actual Form if you want unlimited
flexibility. However *unfriendly* the InputBox is, if you
MUST use it you can try this code. it will give you an
idea what to do.

Function InputBx()
Dim Msg As String
Msg = InputBox("", "?", "Enter your text here")
If Msg <> "" Then
MsgBox Msg
Else
MsgBox "You have Canceled the input"
End If
End Function
 
Dim retVal as String

retVal = InputBox("Prompt","Title","Default")

If retVal = "" Then
MsgBox "Cancel or '' (empty string)"
Else
MsgBox retVal
End If
 
None of the other replies are what you want.

This is what you want:

s = inputbox ("What's the skinny?")
if strptr (s) = 0 then
' he cancelled.
else
' he didn't cancel
endif

HTH,
TC
 
-----Original Message-----
Is it possible to trap the Cancel click event when the
user clicks on it from an InputBox?


.


There isn't an event as such, but you can trap for it. eg

strPassword = InputBox("Test")

If strPassword = "" Then
'User clicked cancel, or didn't enter anything
Else
'OK
End If


Hope that helps.


Damien
 
1) strptr is "undocumented" per MS Knowledge Base

2) strptr returns non-zero when presented a zero-length string (which is
what InputBox returns when Cancel is pressed). strptr only returns 0 when
presented with a null string (vbNullString) - as discussed in MVP article on
this group of functions.

3) You need to define the API reference to strptr before using it.

The best reason to use strptr in this case is if overhead is critical - it
seems unlikely you'd use an InputBox in a high-performance routine.
Otherwise, stick to the other responses; at least they work.

HTH,

Eric
 
Sorry, but most of that is wrong.

strptr() is the *only* reliable way to determine that the user clicked
Cancel. None of the other suggested methods work. "" is returned if the user
clicked Cancel *or* if they OK'd an empty string.

strptr() does not require an API reference. It is a VBA function.

TC
 
Back
Top