melissa_may1
Registered User.
- Local time
- Today, 08:07
- Joined
- Nov 29, 2011
- Messages
- 41
Hi All,
I'm using Access 2007, and have created a database with some forms and reports that are working fine.
The user is having some trouble, due to accidental changes on the forms.
I want to alert the user when a change has been made, and give her the option of saving or discarding the changes.
I've added code to the form's BeforeUpdate event (see below), and the code seems to do its job. If a change has been made, the messagebox pops up with "Do you wish to save the changes?" and the user can choose "Yes" or "No."
When the user selects "Yes" everything works as expected.
However, when the user chooses "No." there is an undesired error message that pops up: "You can't go to the specified record."
I've done everything I can think of to trap this error, but nothing seems to work.
Since the query for the form has been entered through the form's properties, I'm thinking that the error is being generated by the query, that VBA doesn't have control over the query (since it's in the form's properties and not in a code module), and thus can't trap the error.
So, what do I do?
Do I move the query into VBA? (And, I'm guessing that I'll have to move all of the First/Next/Previous/Last logic into VBA as well?)
Or is there a way to eliminate or trap this error?
Thanks!
I'm using Access 2007, and have created a database with some forms and reports that are working fine.
The user is having some trouble, due to accidental changes on the forms.
I want to alert the user when a change has been made, and give her the option of saving or discarding the changes.
I've added code to the form's BeforeUpdate event (see below), and the code seems to do its job. If a change has been made, the messagebox pops up with "Do you wish to save the changes?" and the user can choose "Yes" or "No."
When the user selects "Yes" everything works as expected.
However, when the user chooses "No." there is an undesired error message that pops up: "You can't go to the specified record."
I've done everything I can think of to trap this error, but nothing seems to work.
Since the query for the form has been entered through the form's properties, I'm thinking that the error is being generated by the query, that VBA doesn't have control over the query (since it's in the form's properties and not in a code module), and thus can't trap the error.
So, what do I do?
Do I move the query into VBA? (And, I'm guessing that I'll have to move all of the First/Next/Previous/Last logic into VBA as well?)
Or is there a way to eliminate or trap this error?
Thanks!
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer
' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
' Check the user's response.
If iResponse = vbNo Then
' This doesn't stop the error.
'On Error Resume Next
' Undo the changes to record
' DoCmd.RunCommand acCmdUndo
' Undo the changes to record
Me.Undo
' Requery to avoid error
' DOESN'T WORK
'Me.Requery
' Cancel the update.
Cancel = True
End If
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
' This messagebox never fires!
MsgBox DataErr
' None of this code seems to work!
Select Case DataErr
Case 2169
Response = acDataErrContinue
Case 2105
Response = acDataErrContinue
Case Else
Response = acDataErrDisplay
End Select
End Sub