Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Controls bound to bit columns crash workaround - avoid gray hair! 1

Status
Not open for further replies.

ESquared

Programmer
Dec 23, 2003
6,129
US
You may not have run into this little gem of a bug yet, so if you haven't, thank your lucky stars. Be sure to remember this so if you DO run into it, you don't have one of those I-want-to-tear-out-my-hair days.

The problem is that an Access ADP crashes in certain circumstances (which no one exactly knows) when trying to update a checkbox (or presumably other control) bound to a bit column in SQL Server. The following helpful message appears:

Stupid Error Message said:
Microsoft Access has encountered a problem and needs to close. We are sorry for the inconvenience.

Microsoft is sorry all right. I've never seen such a sorry excuse for a major bug! Anyway, I'll explain the workaround I found--after probably a month of dancing around the problem on form after form.

The crash always happens after the form's BeforeUpdate event, but before the control's BeforeUpdate event. If two Form_BeforeUpdate events happen, it will occur after either of them (such as if you cancel the first one).

(Access for some reason sometimes (always?) fires two Form_BeforeUpdate events. And it also (sometimes?) does some weird things with autonumber primary key fields in the meantime. A coworker tells me that during the first one, records have sequential primary key values starting with 1, and during the second one, they again have their correct server-side values.)

Surprisingly, cancelling just these events seems to allow the application to not only avoid crashing but also to work normally as a user would expect. Since the control's GotFocus event fires before the form's BeforeUpdate event, we can use it to tell us when to cancel those updates:

Code:
Private gfPreventUpdate as Boolean

Private Sub Checkbox1_GotFocus
   gfPreventUpdate = True
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
   Cancel = gfPreventUpdate
End Sub

Private Sub Checkbox1_BeforeUpdate(Cancel As Integer)
   gfPreventUpdate = False
End Sub

This surprisingly small amount of code seems to do the trick. If you are already doing something in the control's Click or AfterUpdate events, you can optionally set the flag back to false there also instead of in the BeforeUpdate event.

I hope this helps someone avoid supreme frustration... and I again call down a pox on Microsoft for this bug.

Supposedly, Access 2002 is the latest version to have this bug, but I saw some reports of people managing to get it in Access 2003 as well.

If I sound grumpy it's because I AM grumpy. Sorry about that! It would be nice to know if this ever actually helps anyone, sparing them from suffering the same grumpiness! :)
 
Oh, you also need:

Code:
Private Sub chbInactive_LostFocus()
   gfPreventUpdate = False
End Sub

In case someone navigates to the control using the keyboard, firing GotFocus, but never changes the value of the control.
 
I had this problem too, and your code sorts it out perfectly. Thanks very much!

Leanne
 
Just for anyone else who may have this problem...

I can't use the fix above for every checkbox that is causing my application to crash. The way I'm getting around it is this.

I have a disconnected recordset feeding my form, so it's not linking directly to my table. I have created another checkbox that is unbound. When the user clicks it it fires off an on_click event that sets the disconnected recordset's value to true or false depending on what they've done. Then the user has to click the save button in order to save these changes.

I've found that if your on_click event sets the original field to true or false then you still crash, which is why I get it to set the value of the disconnected recordset instead. So it would appear this is a fault with FORMS in ADPs rather than the link to the data.

If anyone needs more help on this, or code examples, then post a reply and I'll give more detail.

Leanne
 
So my fix works sometimes but not other times? Do you have any clarity on when it doesn't work and how that's different from when it does work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top