Cancel FormUpdate

Kenln

Registered User.
Local time
Today, 07:39
Joined
Oct 11, 2006
Messages
551
I have unbound check boxes on a form.

When someone checks any of these boxes I want to prevent them from going to the next (or previous, or anyother) record without a user prompt.

----

Basically, the form is based on a table. The check boxes are based on another can CANNOT be updated directly but rather through an MSSQL procedure.

So... because of this the check boxes are unbound, but are updated using form.current. If a user changes one of these boxes I will show a new commend button which will allow them to update the table.

However, I don't want them to navigate away from the record without making the decision to update or not.

Thanks,
 
One technique you could consider is to force the form to become dirty on the checkboxes' BeforeUpdate event:

Code:
Me.Dirty = True

But if you need a clearer indication, add a boolean flag as well- call it UnboundDirty for instance:

Code:
Private bUnBoundDirty As Boolean

Private Sub MyCheckbox_BeforeUpdate(Cancel As Integer)

bUnBoundDirty = True
Me.Dirty = True

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)

If bUnBoundDirty Then
   'Do your validate code/whatever you need to react to the checkbox
End If

'The rest of code..

End Sub

Private Sub Form_Current()

bUnBoundDirty = False 'Reset the flag

End Sub
 
I tried that. the code
Code:
 Private Sub Form_BeforeUpdate(Cancel As Integer)
never executes.

I think it is because the check boxes are unbound so the form does not update. I think. Why else could I check a check box and not call 'BeforeUpdate'???
 
I set up a little form to test it. This code works:

Code:
Option Compare Database
Option Explicit

Private bDirtied As Boolean

Private Sub Check0_AfterUpdate()

bDirtied = True
Me.Text1.SetFocus
Me.Dirty = True
Me.Check0.SetFocus

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)

If bDirtied Then
    MsgBox "The check was dirtied."
End If

End Sub

Private Sub Form_Current()

bDirtied = False

End Sub

Private Sub Form_Dirty(Cancel As Integer)

MsgBox "The form is now dirty."

End Sub

I discovered that you need to have focus on bound controls to force the form to become dirty, and that has to happen on checkbox's AfterUpdate event, but it will dirty the form and thus fire form's BeforeUpdate.
 
There inlies my problem.

When I use
Code:
Me.Dirty = True
I get:
In order to chage data through this form, the focus must be in a bound field that can be modified.


The data in the form cannot be updated directly.

Without data the is updatable I cannot set Me.Dirty, and without that I do not get the 'BeforeUpdate' event.
 
Maybe I'm not understanding your problem correctly.

You said you have a bound form that has a couple of unbound controls that you want to react to in form's beforeupdate event?

If so, then that code I showed you will do that for you. Note that I did a Setfocus to a bound control before settting Me = Dirty then moving focus back to the unbound to circumvent the error you just got.

If the form is wholly unbound, then we would need a different approach.
 
Sorry,

The check boxes are unbound.

And the form is based on a table. But the table is not updatable either.
 
Ah, I see.

If you don't need the table to be updatable, then I'd say we'll have to treat it as if it was wholly unbound (even if it's bound to the non-updateable table), in which case you would have to trap and simulate form's BeforeUpdate by:

1) Writing a generic sub to handle all data changes/validation. This will substitute the BeforeUpdate.

2) Call the same sub from Form's Unload event.

3) If necessary, provide your own navigation buttons so you can call the sub before moving to a new record on the form.
 
I was really afraid you were going to say something like that.

bummer...

Not the answer I was hoping for, hehe

Thank you,
 

Users who are viewing this thread

Back
Top Bottom