Detecting if Form is Open

  • Thread starter Thread starter Chaplain Doug
  • Start date Start date
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.
 
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
 
Back
Top