Setfocus to another form if it's open

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two forms, frm_One and frm_Two. There is code on frm_One to check if
frm_Two is open and if it is switch to it:

If Forms![frm_Two].Visible = True Then
Forms![frm_Two].SetFocus

But this doesn't work as if frm_Two is closed I get the 'Database can't find
the form frm_Two... error.'
How else can I do this?
--
Adam Thwaites
Access Database Designer
adam.*spamless*[email protected]
Manchester, UK
(I have no access to other sites apart from microsoft.com so posting
external links is no use to me)
 
If you don't have it already, you can use the IsLoaded function from the
sample MDB NorthWind.Mdb
=============================
public function IsLoaded(FormName as String) As Boolean
dim strFName as String ' convert to lcase
dim frm as Form ' iterate forms collection

' ignore case
strFName = LCase$(FormName)

' assume it's not there
IsLoaded = False

' look through open forms
For Each frm in Forms
' could use StrComp if you prefer
If LCase$(frm.Name) = strTemp
' found it
IsLoaded = True
' don't bother going round any more
Exit For

End If
Next frm
End Function
===============================
And then use your code
If IsLoaded("frm_Two") Then
Forms![frm_Two].SetFocus
 
If you use A2000 or later, use:

If (CurrentProject.AllForms("frm_Two").IsLoaded = True) Then
Forms![frm_Two].SetFocus
....
 
Excellent, works like a charm. Thanks

If (CurrentProject.AllForms("frm_Two").IsLoaded = True) Then

--
Adam Thwaites
Access Database Designer
adam.*spamless*[email protected]
Manchester, UK
(I have no access to other sites apart from microsoft.com so posting
external links is no use to me)


Van T. Dinh said:
If you use A2000 or later, use:

If (CurrentProject.AllForms("frm_Two").IsLoaded = True) Then
Forms![frm_Two].SetFocus
....

--
HTH
Van T. Dinh
MVP (Access)



Adam Thwaites said:
I have two forms, frm_One and frm_Two. There is code on frm_One to check if
frm_Two is open and if it is switch to it:

If Forms![frm_Two].Visible = True Then
Forms![frm_Two].SetFocus

But this doesn't work as if frm_Two is closed I get the 'Database can't
find
the form frm_Two... error.'
How else can I do this?
--
Adam Thwaites
Access Database Designer
adam.*spamless*[email protected]
Manchester, UK
(I have no access to other sites apart from microsoft.com so posting
external links is no use to me)
 
Back
Top