tags:

views:

1168

answers:

3

I have a main form MYMAIN with two subforms in it MYSUBONE and MYSUBTWO.

I have "on current" events in each subform that update textbox in the other subform.

My problem arises when the forms are being loaded. The "on current" event is triggered when the subform "MYSUBONE" is loaded (BEFORE "MYSUBTWO" is loaded) and it tries to update a textbox in MYSUBTWO which is still not yet loaded. So error is triggered in the event procedure.

How do I check in my "on current" event procedure (in VBA?) for MYSUBONE to check if the MYSUBTWO subform is not yet loaded.

on-current-mysubone if mysubtwo is not loaded then update mysubtwo.textbox = ... end if

I tried the "Isloaded" function in the sample database "Northwind" but doesn't seem to work. how do i check if subform is not yet loaded?

Or could I just ignore error and use something like "if error, exit function"?

A: 

Subforms are actually opened before the main form is opened (including all their OnCurrent events). I don't think you can guarantee which order they are loaded in either.

I'd turn the problem around and do the update from the main form. If you really have to do the update from the Subform, move the update to a separate function in MySubOne. Then in the main form's OnCurrent, call the function in MySubOne. This will guarantee that both MySubOne and MySubTwo are already loaded.

CodeSlave
This prevents the subforms from updating the opposite subform if their data change instead of the main form.
Jeff O
That's easily solved by putting in a call to the same function on the AfterUpdate event for the subform .
CodeSlave
Also keep in mind that the OnCurrent event of a subform gets called twice when the parent form arrives on a new record. Sometimes you have to put in code to keep things in a subform's OnCurrent from firing twice. The usual method for this is to have a module-level variable in the subform's code to store the PK value of the last OnCurrent event. In the OnCurrent, check if the value of that variable matches the current PK value. If so, then skip the code (you're already on the second OnCurrent). If not, let the event fire and change the value of the variable. Messy? Yep!
David-W-Fenton
is there a way to check if the retrieved subforms record count is 0 or null? something like (if forms!mysubtwo.[Record count] = 0? or forms!mysubtwo.[current record??] is null or = ""? if so, what is the correct syntax?
rtochip, off hand I don't know, but why don't you post that as its own question.
CodeSlave
+2  A: 

Hi rtoChip,
One possible way to solve this problem would be to just ensure you know what order the subforms are loaded in. You can accomplish this by unbinding the subform controls and then manually loading them. Here is how to do it:

  1. Add your subform controls to the parent form as normal.
  2. Make sure you have your link fields, etc. set up how you want them.
  3. In the subform properties>data tab, delete the value in the "Source Object" Field.

Add VBA to manually bind the controls when the parent form opens:

Private Sub Form_Open(Cancel As Integer)
    Me.sfB.SourceObject = "FormB"
    Me.sfA.SourceObject = "FormA"
End Sub
Oorang
A: 

"is there a way to check if the retrieved subforms record count is 0"

if forms!myMainForm!mySubForm.form.RecordsetClone.RecordCount = 0 then....
maxhugen