Checking Data Change on Close

  • Thread starter Thread starter Travis (New User)
  • Start date Start date
T

Travis (New User)

I want a data editing form to check for any changes when an exit button is
clicked. So here is the following code that I have done. (I always get Run
Time Error 13. Type Mismatch) Any help would be greatly appreciated!

Private Sub Form_Close()

If Me.DataChange Then
Dim strMsg As String
Dim Cancel As Integer
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes
Then
'do nothing
Else
Me.Undo
Cancel = True
End If
Else
'do nothing
End If
End Sub
 
I want a data editing form to check for any changes when an exit button is
clicked. So here is the following code that I have done. (I always get Run
Time Error 13. Type Mismatch) Any help would be greatly appreciated!

Private Sub Form_Close()

If Me.DataChange Then
Dim strMsg As String
Dim Cancel As Integer
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes
Then
'do nothing
Else
Me.Undo
Cancel = True
End If
Else
'do nothing
End If
End Sub

DataChange doesn't work this way - use Me.Dirty instead. I haven't run
the code but that should solve your problem (help has a good example
on 'Dirty', so to speak).

-- James
 
As Minton said, DataChange doesn't work like this.

You also can't use Form_Close for this; the record's already been saved by
the time this event fires.

You also can't use Dim Cancel As Integer in a native Access sub! If it
appears in the sub declaration, you can use it; otherwise you can't, Dimming
it or not!

If you need to check to see if the user wants to save changed data, you need
to check for this anytime a record will be saved (i.e. when moving to a
different record) not just if the form's being closed. To check for any
change in either of these circumstances, you need to place code in the
Form_BeforeUpdate event.

I think this will do the job:

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim strMsg As String

strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
'do nothing
Else
Me.Undo
End If
End Sub
 
Forgot to add, you don't need to check to see if the data's changed with this
code! If the data's changed, Sub Form_BeforeUpdate(Cancel As Integer) will
fire; if the data hasn't changed, the sub won't fire!
 
Back
Top