Another way is to create a Function in the standard module to check for active or open forms. I created this since a sub form could be accessed from more than one main form.
Function SeeIfFormLoaded(FormToCheckFor) As String
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
' Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
If obj.IsLoaded = True Then
' Print name of obj.
'''Debug.Print obj.Name
If FormToCheckFor = obj.Name Then
SeeIfFormLoaded = "Yes"
Exit Function
End If
End If
Next obj
End Function
Call this function when you want to check for the form being open by the following code in the sub form, which is an example from one of my apps.
Dim ctl As control, FormToCheckFor As String, retMsg As String
Dim DocName As String, LinkCriteria As String
FormToCheckFor = "MotorDataEntry"
retMsg = SeeIfFormLoaded(FormToCheckFor)
If retMsg = "Yes" Then
'-- On MotorDataEntry tab form -- Focus on subform
Set ctl = Forms!MotorDataEntry!tabsubFormShopOrder
ctl.SetFocus
Exit Sub
End If
'---- Check if on Motor review tab form
FormToCheckFor = "MotorDataReview"
retMsg = SeeIfFormLoaded(FormToCheckFor)
If retMsg = "Yes" Then
'-- On MotorDataReview tab form -- Focus on subform
Set ctl = Forms!MotorDataReview!SORShopOrdersForm
ctl.SetFocus
Else
'-- On ShopOrders not in Tab Forms -- transfer
DocName = "MotorDataReview"
DoCmd.Close acForm, "ShopOrdersList", acSaveYes
DoCmd.OpenForm DocName, , , LinkCriteria
End If
[sig][/sig]