Save button (1 Viewer)

NewfieSarah

Registered User.
Local time
Today, 15:28
Joined
Feb 11, 2005
Messages
193
Hey all I have added a yes no button to my save button, so users have the option to save the record or not. so the save yes works fine, but it is the save no I am having probelms with. I would like to click no, and have the changed not saved, clear that out and go back to the first record again kind of like undo. Thanks! Here is my current code:
On Error GoTo Err_savebtn_Click
PIN.SetFocus
'enable buttons
insbtn.Enabled = True
Command31.Enabled = True
Command63.Enabled = True

'Save the current record
Dim Answer As String
Let Answer = MsgBox("Would you like to save your changes?", vbYesNo, "Save record Confirmation")
If Answer = vbYes Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
exitedit
Else
exitedit
Call firstbtn_Click
End If
Exit_savebtn_Click:
Exit Sub

Err_savebtn_Click:
MsgBox Err.DESCRIPTION
Resume Exit_savebtn_Click
 

ghudson

Registered User.
Local time
Today, 13:58
Joined
Jun 8, 2002
Messages
6,195
Here is a more advanced Yes/No/Cancel example using a select case statement for a custom Save Record button...
Code:
Private Sub bSave_Click()
On Error GoTo Err_bSave_Click

    Beep
    Select Case MsgBox("Do you want to save your changes to the current record?" & vbCrLf & vbLf & "  Yes:         Saves Changes" & vbCrLf & "  No:          Does NOT Save Changes" & vbCrLf & "  Cancel:    Reset (Undo) Changes" & vbCrLf, vbYesNoCancel + vbQuestion, "Save Current Record?")
        Case vbYes: 'Save the changes
            DoCmd.RunCommand acCmdSaveRecord

        Case vbNo: 'Do not save or undo
            'Do nothing

        Case vbCancel: 'Undo the changes
            DoCmd.RunCommand acCmdUndo

        Case Else: 'Default case to trap any errors
            'Do nothing
    End Select

Exit_bSave_Click:
    Exit Sub

Err_bSave_Click:
    If Err = 2046 Then 'The command or action Undo is not available now
        Exit Sub
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_bSave_Click
    End If
    
End Sub
The above code should look very familular to you for I posted the same code in your Message Boxes, Yes/No-Ok/Cancel Buttons thread you started on 10/24/2005. :rolleyes:

Also, you need to ditch the built-in wizard code for the old DoCmd.DoMenuItem commands are out of date. Use the DoCmd.RunCommand commands instead.
 

NewfieSarah

Registered User.
Local time
Today, 15:28
Joined
Feb 11, 2005
Messages
193
Okay so, I am haivng a bit of trouble with my undo as well. In my add I call my undo incase I didnt mean to add I am undo and go back to the original position I was. But it still saves the record. Why is that?? So Why would there be no code for the No button??
UNDO BUTTON CODE
On Error GoTo Err_undobtn_Click
DoCmd.RunCommand acCmdUndo
exitedit
Call firstbtn_Click
'disable buttons
insbtn.Enabled = True
Command31.Enabled = True
Command63.Enabled = True

Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone
If rst.EOF <> False And rst.BOF <> False Then
firstbtn.Enabled = False
previousbtn.Enabled = False
nextbtn.Enabled = False
lastbtn.Enabled = False
editbtn.Enabled = False
deletebtn.Enabled = False
Else
firstbtn.Enabled = True
previousbtn.Enabled = True
nextbtn.Enabled = True
lastbtn.Enabled = True
editbtn.Enabled = True
deletebtn.Enabled = True
End If

Exit_undobtn_Click:
Exit Sub

Err_undobtn_Click:
MsgBox Err.DESCRIPTION
Resume Exit_undobtn_Click

End Sub
 

ghudson

Registered User.
Local time
Today, 13:58
Joined
Jun 8, 2002
Messages
6,195
Using the forums code tags makes it a lot easier to read a posters code. ;)

The current record will be saved if the record is dirty [modified] and you move to another record. You would have to trap for the dirty record and not allow the user to move to another record until the dirty record is either saved or the user undo's the changes to the current record.

I use the method posted in my A Better Mouse Trap? sample to prevent that problem.

Check these two links out for a couple of working sample files and ideas...

A Better Mouse Trap?

Enable/Disable The Control Box X Button
 

NewfieSarah

Registered User.
Local time
Today, 15:28
Joined
Feb 11, 2005
Messages
193
okay back to this again... the code that you posted in the first message looks great, however I save and it saves and cancel and it undos and if I say no not to save, then it still saves the record!! So what is on the go with that? There must be some kind of code out there that doesnt save the record. that is what I was trying to say, I havent looked at the code for the mouse thing yet, so I will in a few, although it sounds very confusing!!! okay thanks
 

NewfieSarah

Registered User.
Local time
Today, 15:28
Joined
Feb 11, 2005
Messages
193
that is a great save, however I just have one question what is the form before update() doing?? What is that code for? and the form current what is that code doing?? Thanks
 

NewfieSarah

Registered User.
Local time
Today, 15:28
Joined
Feb 11, 2005
Messages
193
I Am Still Haveing The Same Problem With Me Add Button, No Matter What I Do The Record Still Saves Even Though I Have Said No And I Cant Cancel And Undo Cause I Am Still In Edit Mode, Andi Cant Edit When I Am In Edit Mode!! Any Ideas??
Thanks
 

ghudson

Registered User.
Local time
Today, 13:58
Joined
Jun 8, 2002
Messages
6,195
I can not guess what is going on. You will have to post your db. Leave a few dummy records to play with and explain what you want each button [in question] to do and also explain what each button [in question] is actually doing.

If you are correctly testing and trapping if the record is dirty then you will not be able to add a new record if the current record is dirty. My mouse trap sample is very easy to follow if you take the time to read the code for each command button.
 
Last edited:

Users who are viewing this thread

Top Bottom