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!

Cancel MsgBox

Status
Not open for further replies.
Sep 27, 2002
14
0
0
US
I need to know how to cancel a msgbox if the user chooses "No" in the following code:

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to continue with the IMPORT?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Import Verification" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic
' context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
MyString = "Yes" ' Perform some action.
Else ' User chose No.
MyString = "No" ' Perform some action.
'these next two lines don't work. How do I get
'out of this??????
'DoCmd.Close acForm, "frmMain Menu"
'DoCmd.OpenForm "frmMain Menu"
End If
 
If this is a sub routine then Exit Sub, or if it's a function then Exit Function will stop any further code from running after the user selects No.

So:

Else ' User chose No.
MyString = "No" ' Perform some action.
Exit Sub 'or Exit Function
End If Anthony J. DeSalvo
President - ScottTech Software
"Integrating Technology with Business"
 
Robert:

Why not just use vbOKCancel instead of vbYesNo? That way you only have to provide code for the OK button.

HTH,

Vic
 
VicM,

No, it will still return to the code after the bit where the msgbox is called or evaluated.

Robert,

You may also want to tweak your code some. If I were to write that code it would look like this:
If vbYes msgbox("Do you want to continue with the IMPORT?",vbYesNo + vbCritical + vbDefaultButton2, "Import Verification", "DEMO.HLP",1000 )Then ' User chose Yes.
MyString = "Yes" ' Perform some action.
Else ' User chose No.
MyString = "No" ' Perform some action.
Goto ExitPoint
End If

ExitPoint would be a label ("ExitPoint:") that brought me to a spot in the code just before the exit happened. I do this because I often use object variables in my code, and cleaning them up always happens after that label. By being consistent about how I exit a routine I know I won't run into situations where I forget to clean up those variables.

The reason I prefer to do the msgbox without variables is that there is a lot less overhead, there are several fewer lines of code, and I find it easier to read. It is true that using variables as you have makes it explicit what each of the parameters is, but it's just so many lines that it bogs me down. Also, when declaring variables it is best to explicity type them, as in "Dim Msg as String".

I also use a bit of a naming convention, so that I know what type of variable I'm using by that variables names. String variables, for me, always start with "str". You can do some searching for Hungarian Naming Convention or check out the RVBA naming convention on line. They're not necessary, and very few people use a complete convention, but they are helpful in keeping things straight.

Man, this is a lot to type, especially considering that the first response you got took care of the problem 100%!! Sorry. Sometimes I can't control myself.

Jeremy =============
Jeremy Wallace AlphaBet City Dataworks See the Developers' section for some helpful fundamentals.

See thread181-473997 for information on how to get the best responses possible here at Tek-Tips.
 
thanks for all the suggestions.
Consider me 'schooled'<grin>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top