Connecting Tech Pros Worldwide Forums | Help | Site Map

How to capture Input Box Button

Familiar Sight
 
Join Date: Apr 2007
Posts: 132
#1: Jul 12 '07
Hi All,

How can I determine if the OK button or the CANCEL button was clicked in an INPUT BOX??

II have the following code and it does not appear to be working correctly.

SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
If SSNreplace = "" Then
MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"
Else
If InputBox("No Number Entered. Please Try Again", vbCritical, "Update a SSN") = vbCancel ThenEnd If
End If

The bolded code does not seem to do anything......

JKing's Avatar
Moderator
 
Join Date: Jun 2007
Location: Niagara Falls, Ontario
Posts: 557
#2: Jul 13 '07

re: How to capture Input Box Button


As far as I know a zero length string is returned if the user clicks cancel. However the same is returned if they click OK and type nothing.

An example would be:
Expand|Select|Wrap|Line Numbers
  1. Dim strTest As String
  2.  
  3. strTest = InputBox("Enter value:")
  4.  
  5. if strTest = "" Then
  6.      MsgBox "User clicked cancel or entered no data."
  7. Else
  8.      MsgBox "You entered: " & strTest
  9. End If
  10.  
Familiar Sight
 
Join Date: Apr 2007
Posts: 132
#3: Jul 13 '07

re: How to capture Input Box Button


Quote:

Originally Posted by JKing

As far as I know a zero length string is returned if the user clicks cancel. However the same is returned if they click OK and type nothing.

An example would be:

Expand|Select|Wrap|Line Numbers
  1. Dim strTest As String
  2.  
  3. strTest = InputBox("Enter value:")
  4.  
  5. if strTest = "" Then
  6.      MsgBox "User clicked cancel or entered no data."
  7. Else
  8.      MsgBox "You entered: " & strTest
  9. End If
  10.  


With that in mind, how could I make the input box appear as long as no data is entered? Some kind of loop or test for data?? The way you have it now, the routine does a test then ends....I'm trying to make it so that if the user types nothing and clicks OK, the code would check for input, if it finds none, it would show a msg box stating no data entered and then show the input box again...If the user click Cancel, the routine ends.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,216
#4: Jul 13 '07

re: How to capture Input Box Button


Quote:

Originally Posted by Proaccesspro

Hi All,

How can I determine if the OK button or the CANCEL button was clicked in an INPUT BOX??

II have the following code and it does not appear to be working correctly.

SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
If SSNreplace = "" Then
MsgBox "No Number Entered. Please Try Again", vbCritical, "Update a SSN"
Else
If InputBox("No Number Entered. Please Try Again", vbCritical, "Update a SSN") = vbCancel ThenEnd If
End If

The bolded code does not seem to do anything......

The bold code will never work since vbCancel is an Intrinsic Constant with a value of 2, and the return value of the InputBox() Function if the Cancel button is pressed is a Zero Length String (""). Try this code and see how you like it, it has been tested and is fully operational:
Expand|Select|Wrap|Line Numbers
  1. Dim SSNreplace, strMsg As String, intResponse As Integer, blnTryAgain As Boolean
  2.  
  3. strMsg = "You either Cancelled the previous operation or clicked on "
  4. strMsg = strMsg & "the OK button with no value entered in the Text Field."
  5. strMsg = strMsg & vbCrLf & vbCrLf & "Do you wish to try again?"
  6.  
  7.  
  8. Do
  9.   SSNreplace = InputBox("Please enter the Member's SSN", "Update a SSN")
  10.  
  11.   If SSNreplace = "" Then
  12.     intResponse = MsgBox(strMsg, vbQuestion + vbYesNo + vbDefaultButton2, "Update a SSN")
  13.       If intResponse = vbYes Then
  14.         blnTryAgain = True
  15.       Else
  16.         blnTryAgain = False
  17.       End If
  18.   Else
  19.     MsgBox "You have entered " & SSNreplace & " as your SSAN"
  20.     blnTryAgain = False
  21.   End If
  22. Loop While blnTryAgain
Reply