Skip MsgBox in BeforeUpdate if Save Button Click = True

O

owp^3

I have 3 buttons on my form Add Record, Save Changes, and Cancel/Refresh.
They work fine.

However, I put a confirmation msgbox in the BeforeUpdate event, because
users are able to use the record selectors to move to another record forcing
the beforeupdate/afterupdate events. From the msgbox the user can save
changes, cancel changes or return to the record they were editing. That
works fine too.

My issue is the user experience of having to press two buttons to save
changes first on the form and then the msgbox that fires in BeforeUpdate.
I'd like to have some sort of conditional check in the BeforeUpdate event to
skip the msgbox if the SaveChanges button was clicked. I just don't know the
best way to pass a parameter from the button click event to the beforeupdate
event.

Any help would be appreciated. Here is my code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Msg, Style, Title,Response
Msg = "Save Changes?"
Style = vbYesNoCancel + vbCritical + vbDefaultButton2
Title = "You are leaving this record"
'Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then
'Do Nothing - Go Straight to AfterUpdate
Else
If Response = vbNo Then Me.Undo Else Cancel = True
End If
End Sub

Private Sub SavePVRecord_Click()
On Error GoTo Err_SavePVRecord_Click
Me.SavePVRecord
Exit_SavePVRecord_Click:
Exit Sub
Err_SavePVRecord_Click:
MsgBox Err.Description
Resume Exit_SavePVRecord_Click
End Sub
 
G

geppo

Ciao "owp^3 said:
I have 3 buttons on my form Add Record, Save Changes, and Cancel/Refresh.
They work fine.

However, I put a confirmation msgbox in the BeforeUpdate event, because
users are able to use the record selectors to move to another record
forcing
the beforeupdate/afterupdate events. From the msgbox the user can save
changes, cancel changes or return to the record they were editing. That
works fine too.

My issue is the user experience of having to press two buttons to save
changes first on the form and then the msgbox that fires in BeforeUpdate.
I'd like to have some sort of conditional check in the BeforeUpdate event
to
skip the msgbox if the SaveChanges button was clicked. I just don't know
the
best way to pass a parameter from the button click event to the
beforeupdate
event.

Any help would be appreciated. Here is my code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Msg, Style, Title,Response
Msg = "Save Changes?"
Style = vbYesNoCancel + vbCritical + vbDefaultButton2
Title = "You are leaving this record"
'Display message.

if me.dirty = true then
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then
'Do Nothing - Go Straight to AfterUpdate
Else
If Response = vbNo Then Me.Undo Else Cancel = True
End If

end if
 
O

owp^3

Thanks Geppo, but both cases are dirty.

The user makes their changes and then either presses the Save Changes button
or moves to a new record with the record selectors. If they press Save
Changes I don't want the message box. If they change records I do want the
message box.
The message box sits in the BeforeUpdate Event. I think the record has to
be dirty for the event to fire at all, right?
 
D

Dale Fye

Try checking to see what the "Active Control" is on the form. Something like

Private Sub Form_BeforeUpdate(Cancel as integer)

'put this right after your dim statement
debug.print screen.activecontrol.name
if me.Dirty AND _
(screen.activecontrol.name = "cmd_Save_Changes" OR _
screen.Activecontrol.name = "cmd_Cancel") then exit sub

...

End Sub
 
G

geppo

Ciao "owp^3" <[email protected]> ha scritto nel messaggio

--- cut---

I don't understand well the English, however I try there.

I hope to have understood your demand


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
 
O

owp^3

Thanks Dale that was what I needed.

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Debug.Print Screen.ActiveControl.Name
ctlName = Screen.ActiveControl.Name
Msg = "Save Changes?"
Style = vbYesNoCancel + vbCritical + vbDefaultButton2
Title = "You are leaving this record"

If Not ctlName = "MySaveChangesButtonName" Then
'Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
'Evaluate Response
If Response = vbYes Then
' Do Nothing and wait for AfterUpdate Event to save changes
Else
If Response = vbNo Then Me.Undo Else Cancel = True
End If
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top