Detecting if Form is Open

C

Chaplain Doug

I have a form with two subforms on it. When a record is
selected in one subform, the RecordSource is updated in
the other to reflect the records associated with the
selected record. I do this in the OnCurrent event:

Private Sub Form_Current()
Dim strSQL As String
strSQL = "SELECT * FROM [Male Progress Query] " & _
" WHERE [StudentID] = " & Me.StudentID
Forms![Male Program Records]![Male Progress
subform2].Form.RecordSource = strSQL
Forms![Male Program Records]![Male Progress
subform2].Visible = True
End Sub

When I first open the main form the assignment to
RecordSource fails (because I suspect that the seconf
subform is not yet open). However, once all forms are up
the assignment works. Should I do this in another way?
Or how do I detect that the second subform is not open
before I try to set the RecordSource? Thanks.
 
D

Douglas J. Steele

SysCmd(acSysCmdGetObjectState, acForm, FormName) should return a non-zero
value if the form is open.

Alternatively, you can write a little helper function like the following
untested aircode that loops through the Forms collection to see whether it's
open:

Function FormIsOpen(FormName As String) As Boolean
Dim frmCurr As Form
Dim booStatus As Boolean

booStatus = False

For Each frmCurr In Forms
If StrComp(frmCurr.Name, FormName, vbTextCompare) = 0 Then
booStatus = True
Exit For
End If
Next frmCurr

FormIsOpen = booStatus

End Function
 

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