A question about Load event ordering in forms and subforms

C

Carl Rapson

I have a form that contains a subform (subform1), and I am wanting to
perform some actions in subform1's Load event, based on whether or not there
are any records:

Private Sub Form_Load()
If Me.Recordset.RecordCount > 0 Then
...<do some things here>
Else
...<do some different things here>
End If
End Sub

The problem is, the RecordCount is coming back zero, even when there are
records in the Recordset (I know this because the records display in the
subform after it opens). After some investigation, I found that subform1's
Load event is firing before the main form's Load event, when I assume means
that there are not yet any linked records to be displayed in the subform
(and thus the RecordCount is zero). Am I correct in this?

What's really odd is that subform1 itself contains another subform
(subform2); I am doing the exact same thing in subform2's Load event, and it
works properly (the RecordCount returns the proper number of records). I
checked, and the order of the Load events is: subform1, then main form, then
subform2. So by subform2's Load event, apparently the records have been
loaded and all is well. Is there any rhyme or reason to the order of the
Load events? Is there any way to "force" the main form's Load event to
execute before the subform? Otherwise, what would be a good way to
accomplish what I am wanting?

Thanks for any assistance,

Carl Rapson
 
S

Sam Hobbs

Carl Rapson said:
Is there any way to "force" the main form's Load event to execute before
the subform? Otherwise, what would be a good way to accomplish what I am
wanting?


One possibility is to call a function in the subform from the main form's
load event. Or maybe that should be to call a function in the main form from
the subform's load event; I might be confused.
 
A

Allen Browne

Assuming you are using the LinkMasterFields/LinkChildFields, the subform's
records are reloaded each time the Current event of the main form fires. It
follows that you cannot really know whether there are any matching records
in the subform when the main form initially loads until after the main
form's Current event fires for the first time.

As a workaround, could you use a DLookup() on the subform's table in the
main form's Load event? That would probably be better than saving the main
form without anything in the subform's SourceObject and forcing it to load
later.
 
C

Carl Rapson

I was able to call the subform's Load event from the main for's Load event,
but it doesn't seem to solve the problem (the RecordCount is still zero). I
will play with it for a while and see what I can figure out.

Thanks,

Carl
 
C

Carl Rapson

I'm experimenting with putting a call to the subform's Load event in the
CUrrent event of my main form, but I keep running into some problems. If I
simply call the subform's Load event, then when I first open the form I get
a runtime 2455 error (improper reference to the Form property). If I put in
a flag to bypass the very first Current event and call the subform's Load
event on subsequent Current events, then I still have the same problem --
the first time the subform loads, it's RecordCount is still zero, even at
the main form's Current event.

I will investigate the DLookup() suggestion and see if that will do what I
need.

Thanks,

Carl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top