Hi Don,
Just a short aside. I would recommend that you name your various
controls something more meaningful than the generic, Accesss-supplied
Command5, Text1, etc. That makes it much easier to follow in code or
elsewhere what control is being used. So I might use cmdClose for the name
of the close button.
Anyway, your code might look something like this, which incorporates
Jim's thoughts about whether or not the close button was clicked. I think my
preference would be to use the On Unload event because you can cancel the
unload in the event of an error. Of course, if clicking on the close button
just runs the same code, you probably would want to remove it from that event
so it is only in the unload event. And in which case you would not need to
check to see if the close button was clicked.
================================================
Private m_boolCloseButtonClicked As Boolean
Private Sub cmdClose_Click()
m_boolCloseButtonClicked = True
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Private Sub Form_Open(Cancel As Integer)
m_boolCloseButtonClicked = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Handle_Error
If Not m_boolCloseButtonClicked Then
DoCmd.RunSQL "some SQL statement"
End If
Exit_Sub:
Exit Sub
Handle_Error:
Cancel = (MsgBox(Err.Number & ": " & Err.Description & vbCrLf & vbCrLf & _
"Do you wish to ignore and close anyway?", vbCritical + vbYesNo +
vbDefaultButton2) _
= vbNo)
Resume Exit_Sub
End Sub
================================================
Hope that helps,
Clifford Bass