spikepl
Eledittingent Beliped
- Local time
- Today, 16:07
- Joined
- Nov 3, 2010
- Messages
- 6,142
Cancel Close after BeforeUpdate validation failure
I have been completely stumped by a common problem.
I have a record with a check box and a date. If the checkbox is checked, the date MUST be filled in. The form can be closed by the user clicking the CLose button, or the cross in the corner. An update can be triggered by a refresh-button which is not on the form for other reasons (not shown in my attached db).
I do a check in BeforeUpdate of the form, and cancel the update if my condition is not fulfilled. The BeforeUpdate can be triggered by user closing the form, so I also want to cancel the close. To this end, I have a grlobal variable, that gets set in the BeforeUpdate, to remember if my current state is Ok to save or not. If not, I cancel the unload. I get a plethora of error messages that do npot make sense ... No current record, Records cannot be saved at this time and otehrs ....I have spent many hours on this now, with bits of code form here and there. I tried to trap messages in the forms OnError, but they did not come from there either. I am mentally stuck, but need this solved.
I need the following:
If condition is not fulfilled, ie. checkbox checked and no date:
1. the user is not allowed to save the record
2. user must either provide a date or uncheck the checkbox ( a message to that effect will be displayed)
3. an Undo of the entire record is not OK, I have about 60 other fields that must be preserved
4. the checkbox should remain as it was, until the user changes its state, so no undo for the checkbox either
Don't bother with messages to the user - that I can deal with. The issue is to obtain the above functionality, without any additional messages from the system itself.
I have shaved my problem down to the attached DB, and the code is here:
The problem is simple and idiotic, but I just haven't found the right pieces of code. Can you point me to a working sample?
If you want to have a look at the attached 2007-db: just open frm1, tick the box, leave date blank, and play with closing the form.
I have been completely stumped by a common problem.
I have a record with a check box and a date. If the checkbox is checked, the date MUST be filled in. The form can be closed by the user clicking the CLose button, or the cross in the corner. An update can be triggered by a refresh-button which is not on the form for other reasons (not shown in my attached db).
I do a check in BeforeUpdate of the form, and cancel the update if my condition is not fulfilled. The BeforeUpdate can be triggered by user closing the form, so I also want to cancel the close. To this end, I have a grlobal variable, that gets set in the BeforeUpdate, to remember if my current state is Ok to save or not. If not, I cancel the unload. I get a plethora of error messages that do npot make sense ... No current record, Records cannot be saved at this time and otehrs ....I have spent many hours on this now, with bits of code form here and there. I tried to trap messages in the forms OnError, but they did not come from there either. I am mentally stuck, but need this solved.
I need the following:
If condition is not fulfilled, ie. checkbox checked and no date:
1. the user is not allowed to save the record
2. user must either provide a date or uncheck the checkbox ( a message to that effect will be displayed)
3. an Undo of the entire record is not OK, I have about 60 other fields that must be preserved
4. the checkbox should remain as it was, until the user changes its state, so no undo for the checkbox either
Don't bother with messages to the user - that I can deal with. The issue is to obtain the above functionality, without any additional messages from the system itself.
I have shaved my problem down to the attached DB, and the code is here:
Code:
Option Compare Database
Dim OKtoSave As Boolean
Private Sub cmdClose_Click()
On Error GoTo cmdClose_error
DoCmd.Close , ""
cmdClose_exit:
Exit Sub
cmdClose_error:
MsgBox Err.Number & " " & Err.Description
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
OKtoSave = True
If Me.MyCheckBox And IsNull(Me.MyDate) Then
Cancel = True
OKtoSave = False
End If
End Sub
Private Sub Form_Load()
OKtoSave = True 'temporary measure, for if I get errors the thing won't save next time
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not OKtoSave Then
Cancel = True
End If
End Sub
If you want to have a look at the attached 2007-db: just open frm1, tick the box, leave date blank, and play with closing the form.
Attachments
Last edited: