Don't Save unless "OK" (1 Viewer)

kbreiss

Registered User.
Local time
Today, 20:00
Joined
Oct 1, 2002
Messages
228
Don't Save unless "OK"

I have a form that I would like for the only way for a record to be updated is when "OK" is clicked. The reason for this is because the "OK" button runs an insert statement that includes the username from their Novell account. My problem is if someone changes the record....then closes the form or previews the report the record gets updated, but the "lastupdatedby" field doesn't get updated because the insert statement didn't run. I guess what I'm asking is if there is a way to not have Access NOT to save a record upon closing or moving to print preview. Or any other suggestions are appreciated.

Thanks in advance,

Kacy
________
Kissing advice forums
 
Last edited:

Mile-O

Back once again...
Local time
Today, 20:00
Joined
Dec 10, 2002
Messages
11,316
Use the form's BeforeUpdate() event.

i.e.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If MsgBox("Do you want to save the changes?", vbQuestion + vbYesNo, "Save?") = vbYes Then
        DoCmd.RunCommand acCmdSaveRecord
    Else
        Me.Undo
    End If
End Sub
 

kbreiss

Registered User.
Local time
Today, 20:00
Joined
Oct 1, 2002
Messages
228
Thank you! That exactly what I was looking for.
________
VIDEO REVIEWS
 
Last edited:

kbreiss

Registered User.
Local time
Today, 20:00
Joined
Oct 1, 2002
Messages
228
Having Trouble

The code below is what I'm trying to use. I have to do an Insert statement instead of the DoCmd.Save because I have an unbound text box that contains the userid. The code runs through, but I get a message.......
"The record has been changed by another user since you started editing it.............." Gives me three options. 1.)Save Changes, 2.) Copy to Clipboard, 3.) Drop Changes. If I select "Save Changes" all fields get updated except the unbound textbox that contains the username. Is there away to turn this pop up message off so the program only runs my update query. Or any other suggestions are appreciated.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Save Changes to " & txtFacilityName & " ?", vbQuestion + vbYesNo, "Save?") = vbYes Then
DoCmd.SetWarnings (False)
strDocName = "qryUpdateInfo"
DoCmd.OpenQuery strDocName, acNormal, acEdit
Else
Me.Undo
End If

End Sub


Thanks again,
Kacy
________
Website Host
 
Last edited:

kbreiss

Registered User.
Local time
Today, 20:00
Joined
Oct 1, 2002
Messages
228
Working....is this OK to do

It seems like I've got it working with the code below. The only difference if that I put me.undo after the insert statement and I'm not getting the pop up message I explained in the previous post.

I just wanted to check to see if this is ok to do or am I going to get burned with it.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Save Changes to " & txtFacilityName & " ?", vbQuestion + vbYesNo, "Save?") = vbYes Then
DoCmd.SetWarnings (False)
strDocName = "qryUpdateInfo"
DoCmd.OpenQuery strDocName, acNormal, acEdit
Me.Undo
Else
Me.Undo
End If

Thanks,
Kacy
________
Justin bieber
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:00
Joined
Feb 19, 2002
Messages
43,274
Mile, this code puts Access into a tight loop -
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If MsgBox("Do you want to save the changes?", vbQuestion + vbYesNo, "Save?") = vbYes Then
        DoCmd.RunCommand acCmdSaveRecord
    Else
        Me.Undo
    End If
End Sub
- the reason being that Access is already in the process of saving the current record (that's why it is in the BeforeUpdate event code to begin with) and so attempting to save the current record from within this procedure causes Access to call the procedure recursively. Luckily it is smart enough to not do what you are telling it to do and it gives an error message instead. Earlier versions actually went into a loop.

kbreiss, stick to a single post on a topic to get the best advice. See one of your other posts on the subject for my solution to your problem.
 

Mile-O

Back once again...
Local time
Today, 20:00
Joined
Dec 10, 2002
Messages
11,316
Thanks Pat,

So, should it be more like this:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If MsgBox("Do you want to save the changes?", vbQuestion + vbYesNo, "Save?") = vbNo Then Cancel = True
End Sub

Oh! I love these developments on the CODE tags where it puts the code in a scrollable window...but that's an aside. :cool:
 

Users who are viewing this thread

Top Bottom