Message Box Commands?

Good Afternoon!

I have another quick question about the code I am writing. This time it is for a message box.


When my user completes the form he fills out, I have programed a message box to come up and tell the user that the updates are complete.

The user clicks "OK" and then another box comes up asking the user if he wants to close the form. This is a yes/no box.

When the user clicks "yes," The form clears and then closes.

Now, when the user clicks "No" I want the message boxes to close and for nothing else to happen. However, what is actually happening is that the user clicks "No" and the form still closes.

Below is the code. I am wondering if I am using the DoCmd.Close command wrong.

Also, how do you tell the form to just close the message box?

Thank you for any help you can give me!

MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"
MsgBox "Close Form?", vbInformation + vbYesNo
If vbYes Then
    
    Dim i As Integer

           For i = 1 To LstNewAct.ListCount

               'Remove an item from the ListBox.
               LstNewAct.RemoveItem 0

           Next i
DoCmd.Close
    
End If

If vbNo Then

    
End If

Open in new window

MeginAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony BerenguelCommented:
Megin,

try this...
MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"
result = MsgBox "Close Form?", vbInformation + vbYesNo
If result =  vbYes Then
    
    Dim i As Integer

           For i = 1 To LstNewAct.ListCount

               'Remove an item from the ListBox.
               LstNewAct.RemoveItem 0

           Next i
           
           DoCmd.Close
    
End If

Open in new window

0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Try this:

MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"
If MsgBox ("Close Form?", vbInformation)  + vbYesNo) =vbYes Then
           Dim i As Integer
           For i = 1 To LstNewAct.ListCount
               'Remove an item from the ListBox.
               LstNewAct.RemoveItem 0

           Next i
          DoCmd.Close, acForm, Me.Name
End If

mx
0
MeginAuthor Commented:
I used that code, but immediatly the line that says "result v= MsgBox "Close Form?", vbInformation + vbYesNo" turned red as an error. It is saying that it is a syntax error.'

How should I change that?

Also, thank you for taking the time to help me!
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
No needs to use 'result' ... it was not Defined anyway ... try my approach.

mx
0
Anthony BerenguelCommented:
Hi Megin,

The line should read...
result = MsgBox "Close Form?", vbInformation + vbYesNo

Open in new window

instead of...
result v= MsgBox "Close Form?", vbInformation + vbYesNo

Open in new window

0
MeginAuthor Commented:
DatabaseMX: I had to make a small change because I was getting a syntax error message, but then it did work, except that the close box, instead of being a "Yes/No" box is not an "Ok" box. How to I make it into a yes/no box?

Here is what the code looks like now:

MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"
If MsgBox("Close Form?", vbInformation) + vbYesNo = vbYes Then
           Dim i As Integer
           For i = 1 To LstNewAct.ListCount
               'Remove an item from the ListBox.
               LstNewAct.RemoveItem 0

           Next i
          DoCmd.Close , acForm, Me.Name
End If

Open in new window

0
Anthony BerenguelCommented:
MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"
If MsgBox("Close Form?", vbYesNo) = vbYes Then
           Dim i As Integer
           For i = 1 To LstNewAct.ListCount
               'Remove an item from the ListBox.
               LstNewAct.RemoveItem 0

           Next i
          DoCmd.Close , acForm, Me.Name
End If

Open in new window

0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"
If MsgBox ("Do you want to close this form?", 36,"Close Form?") = vbYes Then

           Dim i As Integer
           For i = 1 To LstNewAct.ListCount
               'Remove an item from the ListBox.
               LstNewAct.RemoveItem 0

           Next i
          DoCmd.Close , acForm, Me.Name
End If
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
aebea - you copied the same code I posted and changed it ...  Please use your own code ...

mx
0
MeginAuthor Commented:
Aebea,  
That was almost perfect! I just ran into an type mismatch error when I click the "Yes" button. It is highlighting the DoCmd.Close  , acForm,  Me.Name

Should I just take off the acForm and Me.Name part?

Honstly, I am so ignorant of how all of this works. I am not sure why they are there. They might be super important to what I am doing. ~heavy sigh~
0
Anthony BerenguelCommented:
I have never used the Me.Name part of the DoCmd.Close. Personally, I tend to just use DoCmd.close. :-)
0
MeginAuthor Commented:
DatabaseMX

Same thing. Type mismatch. Error 13.
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Sorry ... on more time:


MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"
If MsgBox ("Do you want to close this form?", 36,"Close Form?") = vbYes Then

           Dim i As Integer
           For i = 1 To LstNewAct.ListCount
               'Remove an item from the ListBox.
               LstNewAct.RemoveItem 0

           Next i
         DoCmd.Close acForm, Me.Name
End If
0
Anthony BerenguelCommented:
DatabaseMX, I really didn't mean to step on your toes there. I'm just trying to help out, and do so as quickly as possible. But since you're complaining,  I will add my two cents as well. There's nothing wrong with my initial solution so I didn't see any point of you posting your solution after mine. But I guess we're all just trying to help and get points.
0
MeginAuthor Commented:
Thank you so much! I really appretiate the quick help! The solution worked once I took the bits off of the end of the DoCmd.Close statement. I hope that is okay, that I took the other stuff off. <br /><br />I swear, if it weren't for you I would be beating my head on my desk for the next two days!
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
"so I didn't see any point of you posting your solution after mine"
Look at the time stamp ... there was no posting when I hit Submit ...

And even if there was, I posted a *different* solution ... removing code that is really not necessary. Plus, you did not Dim result ... so, and module with Option Explicit set is going to generate a compile error.

mx
0
Anthony BerenguelCommented:
You're right. Sorry, MX. Won't happen again. Honestly, I was just trying to help out. I wasn't trying to rip you off.
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
no big deal ... sorry.
0
Anthony BerenguelCommented:
No worries on my end. And again, I'm sorry. I realize you're a veteran expert and I don't want cause any tension between any experts here, especially the veterans.
0
MeginAuthor Commented:
I think you are both AWESOME!!!!!!

Again, THANK YOU!
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
aebea:
I was just being a dick.  Sorry.  Feel free to post whatever you want at anytime.  Believe, points are not an issue.

mx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.