Need Help on SubForm

J

John Lane

I have two subforms on a main form. In the On Current Event in subform "A", I
want to populate an unbound field in subform "B". I get an error message and
I think it is because subform "B" is possilby not open yet. I don't really
want subform "A" to populate subform"B" until I click the record selector in
subform "A" and move it up or down - but I think it fires during the opening
of subform "A" anyway. Is there a way to stop the On Current in "A" from
firing during the open, or somehow skip the code when the subfroms opens but
run after? Thanks.
 
J

John Lane

The error message I am getting is #2455, which says: "You entered an
expression that has an invalid reference to the property Form/Report." and
the statement causing it looks like this:

Forms!frmAccountRegister!subfrmTabs!C_PayTo = Me![Payee/Payor]

Me = subform "A" and
Forms!frmAccountRegister!subfrmTabs!C_PayTo = subform "B"
 
D

Dirk Goldgar

John Lane said:
I have two subforms on a main form. In the On Current Event in subform "A",
I
want to populate an unbound field in subform "B". I get an error message
and
I think it is because subform "B" is possilby not open yet. I don't really
want subform "A" to populate subform"B" until I click the record selector
in
subform "A" and move it up or down - but I think it fires during the
opening
of subform "A" anyway. Is there a way to stop the On Current in "A" from
firing during the open, or somehow skip the code when the subfroms opens
but
run after? Thanks.


I'm not entirely sure I follow you, but when you open the main form, the
Open event of the subform will fire first, followed by its Current event,
and then the main form's Open and Current events will fire. So when the
subform's Current event first fires, its parent form isn't even open yet.
When you have two subforms, I'm not sure if there's any way to predict which
will open first, but it seems to me that you could maybe check in the
Current event of subform "A" whether a reference to subform "B" through the
parent causes an error. Something along the lines of:

Private Sub Form_Current()

Dim ctl As Control

On Error Resume Next

Set ctl = Me.Parent!SubformB.Form!TextboxOnB
If Err.Number = 0 Then
ctl.Value = "Foo"
End If
Set ctl = Nothing

End Sub

If that doesn't work, you might have a public variable or control on Subform
A that you set from the Current event of the main form. Then your code in
the subform's Current event would check this variable or control to see if
it should proceed.
 
P

Petr Danes

Sorry, I misread your OP. Dirk makes a good point: it's hard to predict
which subform will get loaded first. The error message certainly sounds like
the other subfrom isn't yet active, which can be the case if it gets loaded
second. In any case, the global variable idea should work, only test it in
subform A, not the main form.

Define a Public variable in a separate module:

Public BAlive As Boolean

In the Open event of subform B, put something like

BAlive=True

and in the OnCurrent event of subform A, try

If BAlive then Forms!frmAccountRegister!subfrmTabs!C_PayTo =
Me![Payee/Payor]

That will prevent the line giving you problems from executing until the
subform B code has run.

Petr




John Lane said:
The error message I am getting is #2455, which says: "You entered an
expression that has an invalid reference to the property Form/Report." and
the statement causing it looks like this:

Forms!frmAccountRegister!subfrmTabs!C_PayTo = Me![Payee/Payor]

Me = subform "A" and
Forms!frmAccountRegister!subfrmTabs!C_PayTo = subform "B"

Petr Danes said:
Not sure if timing collisions are what you are experiencing, but if so,
you
could define a global variable, set it in the Open or Initialize event of
the subform and test it in the OnCurrent event of the main form. That
would
keep you from running until the subform had loaded and run its startup
code.

Petr



"John Lane" <[email protected]> píše v diskusním
příspěvku
 
P

Petr Danes

Good for you. What did you do, just ignore the error or do you handle it
somehow?

Petr
 

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