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!

Is a form open or not? How to tell in VBA?

Status
Not open for further replies.
Oct 6, 2000
7
US
In Access2000 I have a subform used by two different main forms. On the exit event of one of the subforms fields I want to setfocus on a field on the open main form, so that the user doesn't get stuck tabbing inside the subform. How can VBA determine which of two main forms is open so that I can set focus to a field on whichever main form is open? Or if there is another way to end the tabbing loop that would be helpful too. I tried referencing the IsLoaded property of one of the main forms to check if it was open, but lacking any examples I couldn't make the code work. [sig][/sig]
 
One is to creatre a global variable called say whichform and set it to the forms name and check it with If statements in hte other forms.

In a Module put this code
Global whichform

In form coming from
whichform = "Mainform"

In form going to
If whichform = "Mainform" then
me!Text1.setfocus
Else
' do something different
End if



[sig]<p>DougP, MCP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br> Ask me how Bar-codes can help you be more productive.[/sig]
 
you can use

Me.Parent.Name


PaulF [sig][/sig]
 
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 = &quot;Yes&quot;
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 = &quot;MotorDataEntry&quot;
retMsg = SeeIfFormLoaded(FormToCheckFor)
If retMsg = &quot;Yes&quot; 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 = &quot;MotorDataReview&quot;
retMsg = SeeIfFormLoaded(FormToCheckFor)
If retMsg = &quot;Yes&quot; Then
'-- On MotorDataReview tab form -- Focus on subform
Set ctl = Forms!MotorDataReview!SORShopOrdersForm
ctl.SetFocus
Else
'-- On ShopOrders not in Tab Forms -- transfer
DocName = &quot;MotorDataReview&quot;
DoCmd.Close acForm, &quot;ShopOrdersList&quot;, acSaveYes
DoCmd.OpenForm DocName, , , LinkCriteria
End If
[sig][/sig]
 
I appreciate all the examples, and I think every answer would work for me, but PaulF gave me all I really needed with me.parent.name. Short, sweet, done!


Private Sub ScheduledDate_Exit(Cancel As Integer)
Dim myparent As String
myparent = Me.Parent.Name
If myparent = &quot;frmProposals&quot; Then
Form_frmProposals.sbfrmProposalDetails.SetFocus
Else: Form_frmAlliantProposal.sbfAlliantProposalDetails.SetFocus
End If
End Sub [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top