PeterRollinson
asked on
Before Update Routine Not working as expected MS-Access 2003
Routine to ask user if they want to save amendments.
Yes = Save
No= Don't Save and Close Form
Cancel=Don't save at all and leave form open with amendments intact
But it does not work as it should. I get Error 2101 but this should be handled by error routine.
Problem is the cmdClose has to be clicked twice when No is selected
Selecting Yes or Cancel works as expected!
Any ideas?
Regards
Peter
Option Compare Database
Option Explicit
__________________________ __________ ________
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbNo 'Undo update and close the form.
Cancel = True
Me.Undo
Case vbCancel ‘ Ignore update keeping edits, but don’t close form
Cancel = True
End Select
Exit_Proc_Form_BeforeUpdat e:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " / " & Err.Description
Resume Exit_Proc
End Sub
__________________________ __________ ________-
Private Sub cmdClose_Click()
On Error GoTo Err_Handler
If Me.Dirty Then Me.Dirty = False
DoCmd.Close acForm, "frmSMSMessagesPredifined"
Exit_Proc:
Exit Sub
Err_Handler:
Select Case Err
Case 3314, 2101, 2115 ‘Ignore as they are all can’t save errors.
Case Else
MsgBox "Error " & Err.Number & " / " & Err.Description
End Select
End Sub
__________________________ __________ __________ ________-
I can make it work by using the following:
Option Compare Database
Option Explicit
Dim fOKToClose As Boolean
__________________________ __________ ______
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbNo 'Undo and close.
Cancel = True
Me.Undo
fOKToClose = True
Case vbCancel 'Don't close, and don't save.
Cancel = True
fOKToClose = False
End Select
Exit_Proc_Form_BeforeUpdat e:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " / " & Err.Description
Resume Exit_Proc
End Sub
__________________________ __________ __________ _
Private Sub cmdClose_Click()
On Error GoTo Err_Handler
If Me.Dirty Then Me.Dirty = False
DoCmd.Close acForm, "frmSMSMessagesPredifined"
Exit_Proc:
Exit Sub
Err_Handler:
Select Case Err
Case 3314, 2101, 2115 ‘Ignore as they are all can’t save errors.
If fOKToClose Then Resume Next Else Resume Exit_Proc
Case Else
MsgBox "Error " & Err.Number & " / " & Err.Description
End Select
End Sub
Yes = Save
No= Don't Save and Close Form
Cancel=Don't save at all and leave form open with amendments intact
But it does not work as it should. I get Error 2101 but this should be handled by error routine.
Problem is the cmdClose has to be clicked twice when No is selected
Selecting Yes or Cancel works as expected!
Any ideas?
Regards
Peter
Option Compare Database
Option Explicit
__________________________
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbNo 'Undo update and close the form.
Cancel = True
Me.Undo
Case vbCancel ‘ Ignore update keeping edits, but don’t close form
Cancel = True
End Select
Exit_Proc_Form_BeforeUpdat
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " / " & Err.Description
Resume Exit_Proc
End Sub
__________________________
Private Sub cmdClose_Click()
On Error GoTo Err_Handler
If Me.Dirty Then Me.Dirty = False
DoCmd.Close acForm, "frmSMSMessagesPredifined"
Exit_Proc:
Exit Sub
Err_Handler:
Select Case Err
Case 3314, 2101, 2115 ‘Ignore as they are all can’t save errors.
Case Else
MsgBox "Error " & Err.Number & " / " & Err.Description
End Select
End Sub
__________________________
I can make it work by using the following:
Option Compare Database
Option Explicit
Dim fOKToClose As Boolean
__________________________
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Select Case MsgBox("Save changes?", vbYesNoCancel)
Case vbNo 'Undo and close.
Cancel = True
Me.Undo
fOKToClose = True
Case vbCancel 'Don't close, and don't save.
Cancel = True
fOKToClose = False
End Select
Exit_Proc_Form_BeforeUpdat
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " / " & Err.Description
Resume Exit_Proc
End Sub
__________________________
Private Sub cmdClose_Click()
On Error GoTo Err_Handler
If Me.Dirty Then Me.Dirty = False
DoCmd.Close acForm, "frmSMSMessagesPredifined"
Exit_Proc:
Exit Sub
Err_Handler:
Select Case Err
Case 3314, 2101, 2115 ‘Ignore as they are all can’t save errors.
If fOKToClose Then Resume Next Else Resume Exit_Proc
Case Else
MsgBox "Error " & Err.Number & " / " & Err.Description
End Select
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are right of course :)
Thanks for the quick response.
Peter
Thanks for the quick response.
Peter
No worries, glad to have helped
Cancel = True
it forces u to stay on the form, that could be the reason why
thats what the use of Cancel is for, it doesnt do the update and leaves focus back on the form