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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Button VBA (AutoSave) 1

Status
Not open for further replies.

vbridge

Technical User
Jun 27, 2005
15
US
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
 
vbridge,

Try replacing [tt]DoCmd.DoMenuItem acTableBar, acEditMenu, acDeleteRecord, , acMenuVer70[/tt] with [tt]Me.Undo[/tt].
Code:
...
If bytChoice = vbNo Then
'[s]DoCmd.DoMenuItem acTableBar, acEditMenu, acDeleteRecord, , acMenuVer70[/s]
[b]Me.Undo[/b]
DoCmd.Close
No = True
End If
...

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Worked like a charm. Thank you sooo much!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top