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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap vbOkay or vbCancel from InputBox 3

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
0
0
US
How do I use the values from Okay or Cancel from a MessageBox or InputBox? Not the text, but the Okay or Cancel button.. What I want to do is this:
Code:
Dim strComment as String
strComment = InputBox("Enter a word here")

If InputBox.vbOkay Then
     {code for this option}
End If
If InputBox.vbCancel Then
     {code for this option}
End If
Any help would be greatly appreciated. X-)

 
If the user clicks "Cancel" in an inputbox, a zero-length string is returned. So you can test your variable, like so:
Code:
Dim strComment as String
strComment = InputBox("Enter a word here")
If Len(strComment) = 0 Then ' user clicked cancel
     {code for this option}
Else ' user submitted string
     {code for this option}
End If

VBAjedi [swords]
 
Hi there,

this should work for you:

Sub ActOnInputbox()
Dim sAnswer As String

sAnswer = InputBox("Input text", "Title")

If Trim(sAnswer) <> "" Then
'user filled out the inputbox and clicked OK
Else
'user did not fill out the inputbox or clicked cancel
End If

End Sub

Sub ActOnMessagebox()
Dim iAnswer As Integer

iAnswer = MsgBox("Message to react on", vbOKCancel, "Title")

If iAnswer = vbOK Then
'code here
Else
'code here
End If

End Sub

Regards,
Unica
 
Thanks VBAJedi, that was extremely helpful. Unica, thanks as well, I think that would work, but it entails much more coding, it seems. Thanks to you both.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top