Check form if is open in vba (1 Viewer)

dmarop

Registered User.
Local time
Today, 23:12
Joined
May 17, 2013
Messages
41
Hello,

I will need your help for the following.

I have two Forms and i want when the code is run to check if frmMain is open to make it focus and if is not to open it.

I found the code below but it is not working.

Code:
    If CurrentProject.AllForms("frmMain").IsLoaded Then
          Forms!frmMain.SetFocus
    Else
        DoCmd.OpenForm "frmMain", acNormal
    End If

Can you help me to found a solution?

Regards,
Dimitris
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:12
Joined
Jan 23, 2006
Messages
15,379
Here's a sample from a 2010 post by vbaInet
Code:
Sub Button1_Click(nameOfForm As String)
If Application.SysCmd(acSysCmdGetObjectState, acForm, nameOfForm) = acObjStateOpen Then
MsgBox "I am open"
End If
End Sub
 

dmarop

Registered User.
Local time
Today, 23:12
Joined
May 17, 2013
Messages
41
Hi,

I don't want with the method of Click in button.

I am running a loop from a Module and i want in a point to make a check if the form is open or if is not to open it.
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:12
Joined
Jan 23, 2006
Messages
15,379
You should do some more work with vba.
I provided the material as a starting point since I didn't know your environment nor your vba knowledge.

Here is a sub procedure you can use.
It is Public and can be called from your code.

Code:
Sub CheckForm(nameOfForm As String)
    If Application.SysCmd(acSysCmdGetObjectState, acForm, nameOfForm) = acObjStateOpen Then
    Else
        DoCmd.OpenForm nameOfForm
    End If
End Sub
I tested it successfully with a form that was not loaded called FRM_Contact.
It Opened the form as expected.
Code:
Sub testit()
    Dim fn As String
    fn = "FRM_Contact"
    CheckForm (fn)
End Sub

Good luck.
 

G37Sam

Registered User.
Local time
Tomorrow, 00:12
Joined
Apr 23, 2008
Messages
454
You're an absolute lifesaver jdraw - I spent the past half hour searching for a solution for this and all I came across was

CurrentProject.AllForms(FormName).IsLoaded

which kept returning debug errors if the form wasn't loaded, thanks jdraw
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:12
Joined
Jan 23, 2006
Messages
15,379
You are very welcome. I've been called all kinds of things, but this is the first lifesaver.
Thanks.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:12
Joined
May 7, 2009
Messages
19,229
any other value other than 0 will mean the form is open (either in form view or design view)

If SysCmd(acSysCmdGetObjectState, acForm, nameOfForm) <> 0
'the form is open
End If
 

Users who are viewing this thread

Top Bottom