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 <> ""
strResponse = InputBox("What is your Name"
If strResponse = "" Then
MsgBox "You Must Enter Text", vbOkOnly
End If
Loop Craig, mailto:sander@cogeco.ca
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("What is your Name"
If strResponse = "" 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("What is your Name"
If strResponse = "" Then
Exit Do ' This will exit the Loop
End If
Loop
[/tt]
Craig, mailto:sander@cogeco.ca
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).
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.