Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if Form is Open 5

Status
Not open for further replies.

gallas

Technical User
Feb 5, 2002
55
GB
I have a form which users can launch from a couple of places. The on update and close events of the form run code that causes a requery of another form (actually a subform on form). The code is:
Forms!CompaniesForm!ContactLogSubform.Requery
However, CompaniesForm might not be open in certain circumstances. What could be the code to run to check if the form is open before trying to run the requery code? I also probably need help with the If-Then bits of code. If there isn't a way to do this I could probably put in code to ignore the error generated when the form isn't open. Tks.
 
There is a functio called IsLoaded() which does exactly what you want:
Function IsLoaded(aformname As String) As Integer
Dim X As String
On Error GoTo IsLoaded_ERR
X = Forms(aformname).Name 'No error so return true
IsLoaded = True
Exit Function
IsLoaded_ERR:
If err = 2450 Then 'Form cannot be loaded
IsLoaded = False
Else
MsgBox ("An error occurred Function IsLoaded :" & err & " " & Error(err))
End If
Exit Function
End Function

This would be used as:
If IsLoaded("frmNumber1") Then
do this and that........
ElseIf IsLoaded("frmNumber2") Then
don't bother doing it....
Else
Make a cup of tea.....
End if

Frank J Hill
FHS Services Ltd.
frank@fhsservices.co.uk
 
Or... what is probably easier:

If SysCmd(acSysCmdGetObjectState, acForm, &quot;frmName&quot;) <> 0 Then
'Form Is Open
Else
'Form Is Closed
End If
 
Hi Trendsetter, Code partly works but I don't know what the code should be after the Then. I have;

Private Sub Form_Close()
If IsLoaded(CompaniesForm) Then Forms!CompaniesForm!ContactLogSubform.Requery
End Sub

Probably there is a different structure to run the requery after a Then. Can you help? If the form is open then I want to run the requery, if not continue with the close. In the case of after update, if the form is not open do nothing. Tks.
 
You've got it right - requery just tags on the end and because it is a single instruction you don't need to use End if.

Frank J Hill
FHS Services Ltd.
frank@fhsservices.co.uk
 
Tks Fubear and Trendy for helpful msgs (means stars!). Fubear, yr code worked fine. Trendy yours didn't. For some reason the requery action wasn't fired when the form was loaded. Probably because my Function statement (which lives in Global Code) is different. I think it was created by the Switchboard manager and I didn't want to mess with it. In case you're interested here it is:

Function IsLoaded(ByVal strFormName As String) As Integer
' Returns True if the specified form is open in Form view or Datasheet view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function

Anyway what do I know? Not VBA thats for sure.
:->
 
Many thanks anyway I appreciate it!!

Frank J Hill
FHS Services Ltd.
frank@fhsservices.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top