Multiple Apps

OK or Cancel InputBox

Ease of Use

Intermediate

Version tested with

97 and 2000 

Submitted by:

Steiner

Description:

Differentiate between the OK and the Cancel Button on a regular input box. 

Discussion:

Sometimes you need to differentiate whether the user entered no text and pressed Ok, entered some text and pressed OK, or chose Cancel. This can't be done with just 'If MyString = "" ' because this will be true in two of the cases. There is an undocumented function to see whether any text has been entered into the InputBox. This is just a working example, you'll have to modify the three possible outcomes to your own needs. 

Code:

instructions for use

			

Sub InputBoxNew() Dim Temp$ Temp = InputBox("Enter something here:", "Inputbox") If StrPtr(Temp) = 0 Then MsgBox "You pressed Cancel!" 'Option 1 Else If Temp = "" Then MsgBox "You entered nothing and pressed OK" ' Option 2 Else MsgBox Temp 'Option 3 End If End If End Sub

How to use:

  1. Copy the code above.
  2. Open Word, and hit Alt+F11 to open the Visual Basic Editor (VBE).
  3. Double-click Normal on the left-hand side.
  4. Choose Insert-Module and paste the code into the window that appears at right.
  5. Add your own code depending upon the returned value.
  6. Hit the Save diskette to save the macro in your normal.dot template.
  7. Close the VBE.
 

Test the code:

  1. Alt+F8 and select InputBoxNew from the list of Macros; click on Run
  2. Try it with entering some text and clicking OK, entering nothing and clicking OK or just selecting Cancel (no matter whether you've entered text or not), The code as above will confirm your action.
 

Sample File:

No Attachment 

Approved by Anne Troy


This entry has been viewed 148 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express