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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if a form is open 8

Status
Not open for further replies.

KornGeek

Programmer
Aug 1, 2002
1,961
US
Is there a way in VB to determine if a form is open or not?

When I'm on FormA, I want to display a message if FormB is open.

Essentially, I want to only allow one of the two forms to be open at a time. If they try to open the second, I want to force them to choose which one they want open.

Thanks for your help.
 
you can tell if a form is open with a function like this:

Function FormIsOpen(FormName As String) As Boolean
On Error Resume Next
Dim MyForm As Form
Set MyForm = Nothing
Set MyForm = Forms(FormName)
FormIsOpen = Not MyForm Is Nothing
End Function


then, to trap the form open, create an event procedure for the formOpen event

Private Sub Form_Open(Cancel As Integer)
Cancel = FormIsOpen("Blah")
End Sub
 
I agree that this is some good code. Thank you for the quick response. Have another star.
 
Here's how you do it in Access 2000.

If (CurrentProject.AllForms("FormB").IsLoaded) then
Msgbox "form is loaded"
Else
Msgbox "form is not loaded"
end if
 
Personally I prefer the SysCmd method which takes the syntax

Code:
ObjectState = SysCmd(action[, objecttype][, objectname])

Not only does this let you find out if a form is open, it tells you its status. It can also be used for objects other than forms, but here's a form based example.

Code:
Dim bytState as Byte
bytState = SysCmd(acSysCmdGetObjectState, acForm, "frmNameHere")

Note that this returns a value that is any combination of the following constants:

Constant: acObjStateOpen
State: Open
Value: 1

Constant: acObjStateDirty
State: Changed but not saved
Value: 2

Constant: acObjStateNew
State: New
Value: 4
[pc2]
 
Wow. Thanks for the help everybody. These are all great suggestions showing 3 different ways to do something that I wasn't even sure was possible. You guy are great. I love Tek-Tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top