First of all, I just want to warn you that I know very little when it comes to Access AND VBA. However, I've been looking through the forums and FAQs trying to find the answer to my question and I think I am almost there. I've just got one problem that I can't seem to fix on my own. Any help is greatly appreciated.
I work for a power company and I am creating a database in Access for all of the motors in the plant. I have a form that you can open to enter a new motor. On that form, I put a command button that is labeled "Save/Exit". When you click the button, a message box appears asking if the user wants to save their entry. I did this to keep from autosaving records that are incomplete. It has three options: Yes, No, and Cancel. If you click Cancel, it puts the cursor in the Motor Name field. If you click No, it doesn't save and closes the form. If you click Yes, it saves the record and closes the form. However, if you do not enter anything and then click the Save/Exit button to exit the form and then click No, because obviously we do not want to save a blank record, it gives an error. Please help. Here is the code that I have in the OnClick Event of the Command Button:
Private Sub Command42_Click()
Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte
strMessage = "Do you want to save this entry?"
intOptions = vbQuestion + vbYesNoCancel
bytChoice = MsgBox(strMessage, intOptions)
If bytChoice = vbCancel Then
Me![Motor_Name].SetFocus
End If
If bytChoice = vbNo Then
DoCmd.DoMenuItem acTableBar, acEditMenu, acDeleteRecord, , acMenuVer70
DoCmd.Close
No = True
End If
If bytChoice = vbYes Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close
End If
End Sub
Again, I am very unfamiliar with this. I got a version of this code from another thread and modified it a good bit to do what I needed it to do. Thank you for any help you can offer.
Veronica
I work for a power company and I am creating a database in Access for all of the motors in the plant. I have a form that you can open to enter a new motor. On that form, I put a command button that is labeled "Save/Exit". When you click the button, a message box appears asking if the user wants to save their entry. I did this to keep from autosaving records that are incomplete. It has three options: Yes, No, and Cancel. If you click Cancel, it puts the cursor in the Motor Name field. If you click No, it doesn't save and closes the form. If you click Yes, it saves the record and closes the form. However, if you do not enter anything and then click the Save/Exit button to exit the form and then click No, because obviously we do not want to save a blank record, it gives an error. Please help. Here is the code that I have in the OnClick Event of the Command Button:
Private Sub Command42_Click()
Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte
strMessage = "Do you want to save this entry?"
intOptions = vbQuestion + vbYesNoCancel
bytChoice = MsgBox(strMessage, intOptions)
If bytChoice = vbCancel Then
Me![Motor_Name].SetFocus
End If
If bytChoice = vbNo Then
DoCmd.DoMenuItem acTableBar, acEditMenu, acDeleteRecord, , acMenuVer70
DoCmd.Close
No = True
End If
If bytChoice = vbYes Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close
End If
End Sub
Again, I am very unfamiliar with this. I got a version of this code from another thread and modified it a good bit to do what I needed it to do. Thank you for any help you can offer.
Veronica