Call Form_BeforeUpdate

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

Hallo,

How can i adress form_BeforeUpdate in the below code? The below code gives
an error...

Private Sub CmdSave_Click()
On Error GoTo Err_CmdSave_Click

Call Form_BeforeUpdate

Exit_CmdSave_Click:
Exit Sub

Err_CmdSave_Click:
MsgBox Err.Description
Resume Exit_CmdSave_Click

End Sub

Help talks about setting something as optional. I do not understand what i
have to do.

Thanx in advance for any tips.


Regards,
Brian
 
Brian said:
Hallo,

How can i adress form_BeforeUpdate in the below code? The below code
gives an error...

Private Sub CmdSave_Click()
On Error GoTo Err_CmdSave_Click

Call Form_BeforeUpdate

Exit_CmdSave_Click:
Exit Sub

Err_CmdSave_Click:
MsgBox Err.Description
Resume Exit_CmdSave_Click

End Sub

Help talks about setting something as optional. I do not understand
what i have to do.

Thanx in advance for any tips.

You can call Form_BeforeUpdate, but the procedure requires a Cancel
argument, of type Integer. You could define a variable to use in the
call and pass it like this:

Dim intCancel As Integer

Call Form_BeforeUpdate(intCancel)

If intCancel <> 0 Then
' the code in the BeforeUpdate procedure
' issued a cancel request.
End If

But what are you trying to achieve by calling the BeforeUpdate
procedure? Your button is named "CmdSave", which rather implies that
your goal is to save the current record, but I hope you aren't under the
impression that simply calling Form_BeforeUpdate will accomplish that.
If what you want to do is save the record, which will *automatically*
call the Form_BeforeUpdate procedure, then you can use this instead:

If Me.Dirty Then
RunCommand acCmdSaveRecord
End If

or this:

If Me.Dirty Then
Me.Dirty = False
End If

(I rather like the latter, but if the record can't be saved it gives a
rather misleading error message.)
 
Hallo,

Thanx for the reply.

The form_beforeUpdate has the below code. I was thinking to refer to this
instead of 2 save actions. So a user can click the save button or if not
clicked save be reminded on leaving the record through form_
beforeupdate...

Private Sub Form_BeforeUpdate(cancel As Integer)

Msg = "Save changes?"
Style = vbYesNo + vbQuestion
Title = "Mydatabase"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
'do nothing
Else
DoCmd.RunCommand acCmdUndo
End If

End Sub


Regards,
Brian
 
Brian said:
Hallo,

Thanx for the reply.

The form_beforeUpdate has the below code. I was thinking to refer to
this instead of 2 save actions. So a user can click the save button
or if not clicked save be reminded on leaving the record through form_
beforeupdate...

Private Sub Form_BeforeUpdate(cancel As Integer)

Msg = "Save changes?"
Style = vbYesNo + vbQuestion
Title = "Mydatabase"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
'do nothing
Else
DoCmd.RunCommand acCmdUndo
End If

End Sub

If you want to display the "Save changes?" prompt only if the user
hasn't clicked the Save button, why not use a module-level flag variable
to tell the routine that the button was clicked. The relevant parts of
the form's code module might look something like this:

'----- start of code ------
Option Compare Database
Option Explicit

Dim mblnClickedSave As Boolean 'Set to True when user clicks "Save"

Private Sub CmdSave_Click()
On Error GoTo Err_CmdSave_Click

If Me.Dirty Then
mblnClickedSave = True
RunCommand acCmdSaveRecord
End If

Exit_CmdSave_Click:
Exit Sub

Err_CmdSave_Click:
MsgBox Err.Description
Resume Exit_CmdSave_Click

End Sub


Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim Msg As String
Dim Title As String
Dim Style As Integer

If mblnClickedSave Then
' Reset flag for next time
mblnClickedSave = False
Else
Msg = "Save changes?"
Style = vbYesNo + vbQuestion
Title = "Mydatabase"
Response = MsgBox(Msg, Style, Title)
If Response <> vbYes Then
Cancel = True
DoCmd.RunCommand acCmdUndo
End If
End If

End Sub
'----- end of code ------

I haven't tried that out, and I'm not sure whether setting Cancel = True
in the BeforeUpdate event procedure (combined with Undoing the form) is
something you should do or if it's going to cause a problem, but you get
the idea.
 
Hallo Dirk,

Thanx for the assistance!

On every save action i want the question "save changes?" asked. Can i place
the save code i a module and address it from within every form
(cmdSavebutton, form_before, dirty)? Any pointers on the code changes?

Thanx in advance.


Regards,
Brian

Private Sub Form_BeforeUpdate(cancel As Integer)

Msg = "Save changes?"
Style = vbYesNo + vbQuestion
Title = "Mydatabase"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
'do nothing
Else
DoCmd.RunCommand acCmdUndo
End If

End Sub
 
Harmannus said:
Hallo Dirk,

Thanx for the assistance!

On every save action i want the question "save changes?" asked.

That doesn't make sense to me for a Save button -- if I click a Save
button I expect the record to be saved, and not to be forced to repeat
my request. However, it's your application.
Can i
place the save code i a module and address it from within every form
(cmdSavebutton, form_before, dirty)? Any pointers on the code changes?

Sure. You could create a Boolean function in a standard module and call
it from wherever you want. The function would ask the question and
return either True (okay to save) or False (not okay). It might look
like this:

Public Function fOkayToSave() As Boolean

fOkayToSave = _
(MsgBox( _
"Save changes?", _
vbYesNo+vbQuestion, _
"My Database") _
= vbYes)

End Function

Then you need only call it from each form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

Cancel = Not fOkayToSave()

End Sub

If you have a "Save" command button on your form, all it needs to do is
tell Access to save the current record:

Private Sub cmdSave_Click()

On Error GoTo Err_cmdSave_Click

If Me.Dirty Then
RunCommand acCmdSaveRecord
End If

Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:
If Err.Number = 2501 Then
' Ignore - save cancelled by user
Else
MsgBox Err.Description, vbExclamation, _
"Unable to Save - Error " & Err.Number
End If
Resume Exit_cmdSave_Click

End Sub
 
Back
Top