Reading module variables in Form_Load which are initialized in Form_Open

  • Thread starter Thread starter Bob Hairgrove
  • Start date Start date
B

Bob Hairgrove

Using Access 97 SP-2, German edition:

I'm having some problems here ... believe it or not, the Open event
doesn't seem to fire for one of my forms until the Load event has run.

In the form's Open event, I check the OpenArgs which is set by another
form using the DoCmd.OpenForm method. Depending on the value of this
data, I set the form's Recordsource property in code. Also, I set some
module-scope variables using the OpenArgs as a key to look up some
other values in different tables.

Depending on the values I looked up in the form's Open event, I need
to set up some controls which I have to do in the Load event. However,
the value of those module-scoped variables (type Long) has
mysteriously disappeared (i.e. all become 0)!

I put MsgBox's in both the Load event, right before I read the
variables, and in the Open event, right after I set them. Lo and
behold, I see the message box from the Load event BEFORE the message
box from the Open event.

Has anyone else seen this strange behavior, and if so, what can I do
to ensure that the events fire in the proper order?

Thanks.
 
Hi Bob.

I can't be sure of the details, but I do know that Access is not consistent
with the published order of the form events (Open, Load, Current, Resize,
Activate.) The event order does seem to change, depending on what else the
code is doing.

Having said that, I don't think I have ever seen Form_Load before Form_Open.
Since Form_Open is the first event, it is hard to imagine why it could be
sent to execute something else before it starts on the Open event.

How are you opening this form? OpenForm? New?
Are you setting the form's Visible property?
What else could be contributing to this?

I am not familiar with the German version if that ends up being a factor.
 
. However,
the value of those module-scoped variables (type Long) has
mysteriously disappeared (i.e. all become 0)!

You sure they are module defined...and not local to the sub code?

Further, any un-handled errors will cause a re-set of all variable (global
vars, local vars..and module level vars are all re-set if a un-trapped
errors occurs).

So, check your error handling (or..start adding some error handling to each
routine in question...).
 
the value of those module-scoped variables (type Long) has
mysteriously disappeared (i.e. all become 0)!

You sure they are module defined...and not local to the sub code?

Yes. They are defined right after "Option Explicit" and before any
other code lines.
Further, any un-handled errors will cause a re-set of all variable (global
vars, local vars..and module level vars are all re-set if a un-trapped
errors occurs).

This I didn't know about ... good thing to know! However, there
haven't been any such errors except that the order of events seems to
have changed.
So, check your error handling (or..start adding some error handling to each
routine in question...).

In general, I leave error traps out until I have more or less finished
developing something. That way, if an unforeseen error occurs, the IDE
will throw me directly to the offending line. Once I get most of the
bugs ironed out, I put in error handling code as well as line numbers
for logging purposes. I have my own class modules for error logging
which always go into the production code.

I decided to change the form to an unbound form in every case and do
all the updates and inserts in code instead of setting the form's
recordsource property. It's not a whole lot of fields, and it seems to
be working OK now. Still, it would be nice to know what caused this in
the first place.

Thanks for helping!
 
Further, any un-handled errors will cause a re-set of all variable

I should point out that a mde file will NOT re-set variables. So, as a
"final" deployment option, you distribute a mde to your end users, and this
results is a MUCH more reliable application..since un-handled errors don't
reset anything!!

(of course...using a mde thus assumes you split your database).

I talk about using a mde file in the following article about splitting....
http://www.members.shaw.ca/AlbertKallal/Articles/split/index.htm
 
Back
Top