Waiting for subform to load...

J

Jack Leach

I have a form that has two subforms (not nested) on it. The Current event of
the first subform sets a filter for the second subform. The problem is that
on intial open, the first subform's current event runs before the second
subform is loaded.

I have a SubformLoaded function that checks whether a particular subform is
loaded... my thought was, on the current event of the first sub, to include a
module level flag for the opening of the form. If the flag is false (the
form is first opening), then run a loop until the second subform loads...

If pbFlagOpen = False Then
'form is just opening
While Not SubformLoaded("Forms!formname", "subcontrol")
Sleep 500
Wend
pbFlagOpen = True
End If

'carry on...


I was hoping that while the code was looping/sleeping, the rest of the form
would be loading and after a few seconds the function would return True, but
this is not the case.

The only other way I can think to deal with this situation is to include a
similar flag on the second subform, check it during the current event, if the
form is just opening call the function from the first sub that sets the
filter (this would have to be a seperate, public function from the Current
event...

Does anyone have any thoughts/experience on a better way to handle the
situation?? Any insight greatly appreciated.



FYI for those interested, here's the function that checks to see if a
subform is loaded (it has come in quite handy)


'==============================================================================
' NAME: SubFormLoaded
' DESC: Checks to see if a subform is loaded
' ARGS: sESStringParent: ES Statement (ex. Forms!frmThisForm)
' sSubControl: Name of the container control for the questionable
subform
' USAGE: [example as if in frmQuotes module]
' If SubFormLoaded("Forms!frmQuotes", "ctlfrmQtys") Then
' Me.ctlfrmQtys.Form.FilterOn = True
' End I
'==============================================================================
Public Function SubFormLoaded( _
sESStringParent As String, _
sSubControl As String _
) As Boolean
On Error GoTo Error_Proc
Dim Ret As Boolean
'=========================
Dim sEvalString As String
Dim vDummy As Variant
'=========================

sEvalString = sESStringParent & "!" & sSubControl & ".Form.Name"

vDummy = Eval(sEvalString)

Ret = True
'=========================
Exit_Proc:
SubFormLoaded = Ret
Exit Function
Error_Proc:
Ret = False
Resume Exit_Proc
Resume
End Function

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
D

Douglas J. Steele

Since subforms are actually instantiate before their parent forms, why not
load the second subform with no data and put your logic in the parent form's
Current event instead?

The following function might be better than what you've got:

Public Function SubFormLoaded( _
sESStringParent As String, _
sSubControl As String _
) As Boolean
On Error GoTo Error_Proc
Dim Ret As Boolean
'=========================
Dim sEvalString As String
'=========================

sEvalString = Forms(sESStringParent).Controls(sSubControl.Form.Name
Ret = True
'=========================
Exit_Proc:
SubFormLoaded = Ret
Exit Function
Error_Proc:

Ret = False
Resume Exit_Proc
Resume
End Function

You'd call it like

If pbFlagOpen = False Then
'form is just opening
While Not SubformLoaded("formname", "subcontrol")
Sleep 500
Wend
pbFlagOpen = True
End If

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
(no e-mails, please!)

Jack Leach said:
I have a form that has two subforms (not nested) on it. The Current event
of
the first subform sets a filter for the second subform. The problem is
that
on intial open, the first subform's current event runs before the second
subform is loaded.

I have a SubformLoaded function that checks whether a particular subform
is
loaded... my thought was, on the current event of the first sub, to
include a
module level flag for the opening of the form. If the flag is false (the
form is first opening), then run a loop until the second subform loads...

If pbFlagOpen = False Then
'form is just opening
While Not SubformLoaded("Forms!formname", "subcontrol")
Sleep 500
Wend
pbFlagOpen = True
End If

'carry on...


I was hoping that while the code was looping/sleeping, the rest of the
form
would be loading and after a few seconds the function would return True,
but
this is not the case.

The only other way I can think to deal with this situation is to include a
similar flag on the second subform, check it during the current event, if
the
form is just opening call the function from the first sub that sets the
filter (this would have to be a seperate, public function from the Current
event...

Does anyone have any thoughts/experience on a better way to handle the
situation?? Any insight greatly appreciated.



FYI for those interested, here's the function that checks to see if a
subform is loaded (it has come in quite handy)


'==============================================================================
' NAME: SubFormLoaded
' DESC: Checks to see if a subform is loaded
' ARGS: sESStringParent: ES Statement (ex. Forms!frmThisForm)
' sSubControl: Name of the container control for the questionable
subform
' USAGE: [example as if in frmQuotes module]
' If SubFormLoaded("Forms!frmQuotes", "ctlfrmQtys") Then
' Me.ctlfrmQtys.Form.FilterOn = True
' End If
'==============================================================================
Public Function SubFormLoaded( _
sESStringParent As String, _
sSubControl As String _
) As Boolean
On Error GoTo Error_Proc
Dim Ret As Boolean
'=========================
Dim sEvalString As String
Dim vDummy As Variant
'=========================

sEvalString = sESStringParent & "!" & sSubControl & ".Form.Name"

vDummy = Eval(sEvalString)

Ret = True
'=========================
Exit_Proc:
SubFormLoaded = Ret
Exit Function
Error_Proc:
Ret = False
Resume Exit_Proc
Resume
End Function

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 

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