VBA to see if a form is open, and close it

E

el zorro

I have 2 forms, Form A and Form B. I have a control button on Form B. When
the user clicks that control, I want to execute VBA code that will close Form
A if it is open. But I can't seem to get the right coding. I've tried
variations of this...

If FormA.Open = True Then Close it AND [do the the instructions here]
ELse [do the instructions here]
End If

THere must be a simple command or function test if Form A is open. I've
tried "If IsLoaded(Forms!Form A)..." but no dice. And, of course, I don't
want an error message if FormA is not open and the program can't find it.

Also, what is the code to use to Close Form A from Form B? (I usually use
DoCmd.Close, but that's when I'm actually in the form I'm closing.

Thanks!
 
F

fredg

I have 2 forms, Form A and Form B. I have a control button on Form B. When
the user clicks that control, I want to execute VBA code that will close Form
A if it is open. But I can't seem to get the right coding. I've tried
variations of this...

If FormA.Open = True Then Close it AND [do the the instructions here]
ELse [do the instructions here]
End If

THere must be a simple command or function test if Form A is open. I've
tried "If IsLoaded(Forms!Form A)..." but no dice. And, of course, I don't
want an error message if FormA is not open and the program can't find it.

Also, what is the code to use to Close Form A from Form B? (I usually use
DoCmd.Close, but that's when I'm actually in the form I'm closing.

Thanks!

It doesn't matter if a form is not open when you close it.
Just close it. Nothing bad will happen. No error, no nothing.

If it is necessary to determine if a form is open you can use the
IsLoaded function to determine if a form is open.
In Access 2000 or newer:

If Not CurrentProject.AllForms("FormA").IsLoaded Then
Do something here
Else
Do something else
End If

*************

In Access 97, copy this function (from the Northwind.mdb sample
database) into a module:

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

Then code some event:
If Not IsLoaded("FormA") Then
Do this
Else
Do that
End if
 
G

Graham Mandeno

To check if FormA is loaded, use:

If CurrentProject.AllForms("FormA").IsLoaded Then

To close FormA, use:

DoCmd.Close acForm, "FormA"

In fact, I never use DoCmd.Close by itself because it sometimes closes the
wrong object because of timing issues. To close the current form, use:

DoCmd.Close acForm, Me.Name
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top