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 + vbYesNoIf vbYes Then Dim i As Integer For i = 1 To LstNewAct.ListCount 'Remove an item from the ListBox. LstNewAct.RemoveItem 0 Next iDoCmd.CloseEnd IfIf vbNo ThenEnd If
DatabaseMX (Joe Anderson - Microsoft Access MVP)Connect With a MentorDatabase ArchitectCommented:
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
MsgBox "Your Weekly Report Has Been Updated. Thank you!", vbInformation + vbOKOnly, "Update Complete"result = MsgBox "Close Form?", vbInformation + vbYesNoIf 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.CloseEnd If
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
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.'
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.NameEnd If
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.NameEnd If
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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~
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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
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!
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"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.
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.
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