ERROR 2108 is driving me batty! (1 Viewer)

knegrotto

New member
Local time
Today, 05:30
Joined
Oct 22, 2007
Messages
3
confused:Does any one have a solution for the problem with a setfocus following an undo and cancel event raising the following error?

Run-time error 2108: You must save the field before you execute the
SetFocus method.

I am trying to a "simple" data validation. Based on a choice in an option group (yes/no), certain data is required.

For example, if the client selects "yes", then 3 bond controls must be populated. If they are not I want to 1) "deselect option 3" 2) send a message 3) move the focus to the first missing control

Here is my code for the first required control:

Private Sub GroupClearance_BeforeUpdate(Cancel As Integer)

If Me.GroupClearance And Nz(Me.WorkRequirements, "") = "" Then
MsgBox "For Group Clearance, you must enter Work Requirements!"
Cancel = True
Me.GroupClearance.Undo
Me.WorkRequirements.SetFocus
Exit Sub
End If

No matter what I try, I get error 2108. I have tried every we I know how to force a save (Docmd.Runcommand acCmdSaveRecord, setting Me.Dirty = true).

The problem seems to arise from the record being left "dirty" from the undo and cancel event. How do I "undirty" it so I can proceed?

Thanks for your help. I have spent many hours already resaerching this and trying everything I know to no avail.:eek:
 

KeithG

AWF VIP
Local time
Today, 05:30
Joined
Mar 23, 2006
Messages
2,592
I would remove the Undo method because the Cancel=True should cancel the update to the control.
 

knegrotto

New member
Local time
Today, 05:30
Joined
Oct 22, 2007
Messages
3
Hi KeithG,

Thanks for your reply.

That is one option that I tried initially. It does cancel the update event but leaves the "new" value in the control. To remove it and restore the initial value, the user would hit the escape key. I am trying to "do the work for them" and prevent errors.

Any other ideas?
 

WayneRyan

AWF VIP
Local time
Today, 13:30
Joined
Nov 19, 2002
Messages
7,122
K,

Look at the .OldValue property.

Me.YourControl = Me.YourControl.OldValue

Wayne
 

knegrotto

New member
Local time
Today, 05:30
Joined
Oct 22, 2007
Messages
3
Thanks Wayne...

I put the following code in the AFTER update event and it works beautifully!

Me.ClearanceHolder = Me.ClearanceHolder.OldValue
Me.WorkRequirements.SetFocus
Exit Sub
End If

Thank you all for your help!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:30
Joined
Feb 19, 2002
Messages
43,233
The problem with putting code in control event when the edit involves multiple controls is that if the user doesn't ever enter the field, the edit won't run. I find that the best solution is to put all the edit code into the Form's BeforeUpdate event. In your case, you can display an informative message regarding the three additonal fields, but I would consolidate ALL the value edits in the Form's BeforeUpdate event. You can then cancel the control and place the insertion point in the first missing field. I would not reset the values of any fields at this point. I leave them as the user entered them so he can see what he did. I only undo field values if I am prompting for a "save changes" and the user chooses to discard the changes.

If you change the option, you will also need to lock the required fields so you can get back to ground zero.
 

Users who are viewing this thread

Top Bottom