Form Closing Regardless of B4Update Response

S

Sprinks

For a timesheet application, I have a command button on a continuous form for
each project record. If the button is pressed, I present a Popup/Modal form
for the user to enter a note related to the project. A button on the 2nd
form closes the form.

Unfortunately, although the BeforeUpdate code below generates the proper
message, the form closes regardless of whether the user selects OK or Cancel.
Can anyone tell me what I'm doing wrong?

Thank you.

Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click

DoCmd.Close

Exit_cmdExit_Click:
Exit Sub

Err_cmdExit_Click:
MsgBox Err.Description
Resume Exit_cmdExit_Click

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler

Dim ctl As Control
Dim intResponse As Integer

For Each ctl In Me.Controls
If ctl.Tag = "R" Then
If Nz(ctl.Value) = 0 Then
intResponse = MsgBox(ctl.name & " is a required field. Please
enter a value or press Cancel to exit without saving record.", vbOKCancel,
"Required Field")
If intResponse = vbOK Then
Cancel = True
ctl.SetFocus
Else
End If
End If
End If
Next ctl

Err_Exit:
Exit Sub

Err_Handler:
MsgBox "The following error has occurred. Please contact the system
administrator." & _
vbCrLf & vbCrLf & Err.Number & vbCrLf & Err.Description, vbOKOnly,
"Runtime Error"
Resume Err_Exit

End Sub
 
K

Klatuu

You are canceling the update, not the close. You need something in the
form's Unload event to keep it from closing.
 
S

Sprinks

Klatuu,

Thanks for your response; that makes sense, however, can you suggest what
might keep the form from closing?

Thank you.
 
S

Sprinks

Klatuu,

Thanks for your response; that makes sense, however, can you suggest what
might keep the form from closing?

Sprinks
 
K

Klatuu

Use the form's Unload event. It can be canceled which will cause the form
not to close.
 
S

Sprinks

Klatuu,

I've moved the error-checking code to the Unload event, and it works as I
intend, however, even with setting the warnings to False, I'm getting an
Access window saying that the Close action was cancelled.

Do you know how to get rid of this message?

Thank you.

Sprinks
 
K

Klatuu

Put error handling in the unload event and trap for the error number.

If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure PrintReport of VBA Document Form_frmUPOLabor"
End If
GoTo PrintReport_Exit

The error number should be 2501 (I think). Notice in the code above it
ignores this particular error number.
 

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