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

cancel button 3

Status
Not open for further replies.

sf123

Programmer
Oct 9, 2002
30
US
How do you program a cancel button of an input box?
-sf
 
The input box will return nothing if Cancel is pressed. That is the only thing you can tell. So you can relate your code to see if cancel was pressed, such as.

Dim strResponse As String
strResponse = InputBox("What is your Name")

If strResponse = "" Then
MsgBox "You Must Enter Text", vbOkOnly
End If

or if you want to loop until the user actually does enter something rather than jsut pressing cancel try this.

Dim strResponse As String

Do Until strResponse <> &quot;&quot;
strResponse = InputBox(&quot;What is your Name&quot;)

If strResponse = &quot;&quot; Then
MsgBox &quot;You Must Enter Text&quot;, vbOkOnly
End If
Loop Craig, mailto:sander@cogeco.ca

Si hoc legere scis, nimis eruditionis habes
 
That doesn't help me because if the user presses cancel, it keeps going in an endless loop. That loop works fine if they enter invalid input and press OK, then I want to force them to first enter valid input before pressing OK. If the user presses cancel, I want to exit out of the whole program but still have the form running so they press submit(thus starting the bulk of my code over again).
Am I being clear?
 
Right, then what you should do is this....
[tt]
Dim strResponse As String

Do Until strResponse ' Test for invalid text here
strResponse = InputBox(&quot;What is your Name&quot;)

If strResponse = &quot;&quot; Then
exit Sub ' This will just exit the current sub, but leave the program running
End If
Loop
[/tt]
Or If you want to continue with the code below but just exit the loop then try this.
[tt]
Dim strResponse As String

Do Until strResponse ' Test for invalid text here
strResponse = InputBox(&quot;What is your Name&quot;)

If strResponse = &quot;&quot; Then
Exit Do ' This will exit the Loop
End If
Loop
[/tt]
Craig, mailto:sander@cogeco.ca

Si hoc legere scis, nimis eruditionis habes
 

Then, where the msgbox is you could enter Exit Sub or Exit Function depending upon where the code is located.

This will allow your program to continue running and exit out of the running code.

Good Luck

 
This still leaves me with problems. Let me explain the flow of my code.
1. user enters criteria and presses submit
2. submit function calls a few subs
a.promp user to enter a sheet name (input box). create new sheet with sheet name
c. fill sheet
d. create chart based on info in sheet

My problem is in 2a. if I exit the sub because of user pressing cance, the next sub will be called (sub c).

How do I fix this mess?
 

Set a form level or public level boolean variable, and before you exit a sub due to bad user input, you would set it (True/False) then exit the sub. Then from the calling sub you could check the value of the variable and if set you could exit it also. Then from that calling sub you could....

That is one way you could do it. Another way would be to change the called subs into functions that return boolean variables and only at the end of that called function would you say...

CalledFunction = True

So from the calling sub you would do something like...

If CalledFunction(YourParameterIfAny) = False Then Exit Sub

Good Luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top