Undo Doesn't (1 Viewer)

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 09:04
Joined
Oct 17, 2012
Messages
3,276
Here's an aggravating situation in Access 2007:

I'm trying to set up a check box on an unbound form (basically a search screen) to, when updated, check to see if a specific text box has data entered, and if it doesn't, cancel the update and revert the change.

Here is the initial code:

Code:
Private Sub chkWorkComp_BeforeUpdate(Cancel As Integer)

    If IsNull(Me.txtInjuryDate.Value) Then
        Beep
        MsgBox "Worker's Comp may not be selected unless an injury date has been entered!", vbInformation, PROJECT_NAME
        Me.chkWorkComp.Undo
        DoCmd.CancelEvent
        Me.txtInjuryDate.SetFocus
    End If
    
End Sub
The error I receive is:
Run-time error '2108':

You must save the field before you execute the GoToControl action, the GoToControl method, or the SetFocus method.
Additionally, the change to the value of the checkbox is NOT reverted. In fact, as far as I can tell, neither the CancelEvent method nor setting Cancel to True do a damned thing, and none of (Undo, Me.Undo, Me.chkWorkComp.Undo) do a bloody thing either - they certainly do NOT revert the checkbox to its prior value, and the control is left unsaved with the focus due to the error, or if I comment out the SetFocus, the control updates as normal despite the Cancel.

Setting the value to .OldValue (or setting it directly to FALSE) unsurprisingly causes the following error:
Run-time error '2115':

The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing SRS from saving the data in the field.
The only surprise there is that it actually DOES revert the change anyway, but will not pass that line in the code.

Changing the order of the Undo and the Cancel changes nothing.

The idea here is to cancel the update, revert the change to the value of the control (which will, incidentally, revert it to FALSE in all cases), and move the focus to txtInjuryDate. I just can't seem to get those last three lines to actually WORK, and would be grateful for any suggestions received. Hell, for all I know, it's something simple I'm missing just because I've been beating my head against the wall so long!
 

Minty

AWF VIP
Local time
Today, 13:04
Joined
Jul 26, 2013
Messages
10,355
Have you tried Cancel = True.
I think your use of DoCmd.CancelEvent is actually the issue, and that Cancel = True would work?

Possibly?
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 09:04
Joined
Oct 17, 2012
Messages
3,276
Nope, tried that too. (It's in the wall of text under the first error message - easy to miss.)

For some reason, with A2007, I rarely see Cancel = True actually cancel the event. Typically, only the DoCmd.CancelEvent actually did its job, but I did try both here.

Thanks, though!
 

Minty

AWF VIP
Local time
Today, 13:04
Joined
Jul 26, 2013
Messages
10,355
Nope, tried that too. (It's in the wall of text under the first error message - easy to miss.)

For some reason, with A2007, I rarely see Cancel = True actually cancel the event. Typically, only the DoCmd.CancelEvent actually did its job, but I did try both here.

Oops - I did miss that. I blame US time lag. (In the USA working so my body clock and brain are not in sync. At all!)

Wait a moment. It's an unbound check box. Doesn't that mean the before update event is sort of unconnected? I thought before update only worked properly on bound controls ?
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 09:04
Joined
Oct 17, 2012
Messages
3,276
Oops - I did miss that. I blame US time lag. (In the USA working so my body clock and brain are not in sync. At all!)

Wait a moment. It's an unbound check box. Doesn't that mean the before update event is sort of unconnected? I thought before update only worked properly on bound controls ?

Well #@(&#$(@!, I think you're right. Time for a work-around.

Thanks!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:04
Joined
Feb 28, 2001
Messages
27,001
Another side-effect... if it is unbound, then it has no .OldValue property (I think) so you can't revert to the old value.
 

Users who are viewing this thread

Top Bottom