Function to test if a form is Active?

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

Guest

I have a form (Form C) that can be opened by a command button from either of
two other forms (Forms A and B).

Forms A and B each have lists that need to be updated with info input into
Form C. If there was only ONE form that caled Form C, I would use the ON
CLOSE event for Form C and the code: [Form A.List].Requery.

Of course, if Form C is called by the Form B, then I get an error message
when the form is closed, since it can't find the Form A referred to in the
code.

SO, I'm thinking I should write an "IF...Then" statement that tests to see
whether Form A or From B is open, then does the Requery for the appropriate
list.

But I'm missing the piece of code that would do that test. Would it include
a function, like maybe "If IsActive([Form A)] then..." ? Or something like
"If [Form A].IsOpen = True then ..."

Any ideas? (And does anyone know off a good reference source for all the
functions and methods that are available to use?)

THanks!
 
You could use the OpenArgs parameter to indicate which form opened it.
I've used this numerous times when I need to *only* run code when a
certain form is open.
 
SO, I'm thinking I should write an "IF...Then" statement that tests to
see whether Form A or From B is open, then does the Requery for the
appropriate list.

I'd probably go with the OpenArgs solution. On the other hand, if there is
a chance that you'd have FormA and FormB open at the same time, and they
both needed updating, then it's easy enough to do code like

If FormIsOpen("FormA") Then
Forms("FormA").MyListcontrol.Requery

End If

If FormIsOpen("FormB") Then
Forms("FormB").MyListcontrol.Requery

End If


and so on. If you don't know how to write the FormIsOpen function, just
post back here, or google.

All the best


Tim F
 
el zorro said:
I have a form (Form C) that can be opened by a command button from either of
two other forms (Forms A and B).

Forms A and B each have lists that need to be updated with info input into
Form C. If there was only ONE form that caled Form C, I would use the ON
CLOSE event for Form C and the code: [Form A.List].Requery.

Of course, if Form C is called by the Form B, then I get an error message
when the form is closed, since it can't find the Form A referred to in the
code.

SO, I'm thinking I should write an "IF...Then" statement that tests to see
whether Form A or From B is open, then does the Requery for the appropriate
list.

But I'm missing the piece of code that would do that test. Would it include
a function, like maybe "If IsActive([Form A)] then..." ? Or something like
"If [Form A].IsOpen = True then ..."

Any ideas? (And does anyone know off a good reference source for all the
functions and methods that are available to use?)

THanks!
 
THanks for your responses. I've given both approaches a try, but no luck.

I'm not sure how to use the OpenAgs parameter. I'm guessing I need to
specify it in the event for the command button that opens Form C. Something
like:

DoCmd.OpenForm stDocName, , , stLinkCriteria, acFormAdd, , {Something here?
Not Sure What}

THen I guess the name of the form or command button that calls From C will
can be recognized by code I write in the ON CLOSE event of FOrm C, something
like

If FormC.Open Arg = "name of opening Form" Then... but I'm obviously
struggling here.

I like the "FormIsOpen" function idea--was that an actual function? Access
doesn't seem to recognize it; I get an error message when I try to compile it
: "Sub or Function not defined."

Any shove in the right direction for either approach will be greatly
apprecated. Thanks!
 
I have a form (Form C) that can be opened by a command button from either of
two other forms (Forms A and B).

Forms A and B each have lists that need to be updated with info input into
Form C. If there was only ONE form that caled Form C, I would use the ON
CLOSE event for Form C and the code: [Form A.List].Requery.

Of course, if Form C is called by the Form B, then I get an error message
when the form is closed, since it can't find the Form A referred to in the
code.

SO, I'm thinking I should write an "IF...Then" statement that tests to see
whether Form A or From B is open, then does the Requery for the appropriate
list.

But I'm missing the piece of code that would do that test. Would it include
a function, like maybe "If IsActive([Form A)] then..." ? Or something like
"If [Form A].IsOpen = True then ..."

Any ideas? (And does anyone know off a good reference source for all the
functions and methods that are available to use?)

THanks!

The easiest way for me is to code the click event on FormA and on
FormB like this:

DoCmd.OpenForm "FormC", , , , ,acDialog , Me.Name

Note: using acDialog stops all execution until you then close or hide
FormC. I think that is what you want. If not, retain the comma but do
not write acDialog.
Me.Name should be written exactly as I have.
No need to actually write the form's name.

On Form C, code the Declarations section (up at the very top) of the
form's code sheet:
Option Explicit
Dim strWho as String

Then code the formC Load event:
If Not IsNull(Me.OpenArgs) Then
strWho = Me.OpenArgs
End If

Then, where ever else in your code you need to requery the first form,
use the syntax
forms(strWho).Requery (or whatever you want to do).
 
The OpenArgs parameter can contain any alphanumeric text that you
desire. Simply encapsulate the string in quotes to pass the value as in
"frmInvoicing"

Then in the form that you're passing the value to reference the OpenArgs
property of the form as in Me.OpenArgs when you need to grab it as in...

Sub Form_Load()
Select Case me.OpenArgs
Case "frmInvoicing"
Case "frmItineraries"
Case else
End Select
End Sub

-or-

Sub Form_Close()
If me.OpenArgs = "frmInvoicing" Then Forms(me.openArgs).Requery
End Sub

Note how I used the Forms collection along with the value in the
..OpenArgs to requery the form from which the current form was opened.

el said:
THanks for your responses. I've given both approaches a try, but no luck.

I'm not sure how to use the OpenAgs parameter. I'm guessing I need to
specify it in the event for the command button that opens Form C. Something
like:

DoCmd.OpenForm stDocName, , , stLinkCriteria, acFormAdd, , {Something here?
Not Sure What}

THen I guess the name of the form or command button that calls From C will
can be recognized by code I write in the ON CLOSE event of FOrm C, something
like

If FormC.Open Arg = "name of opening Form" Then... but I'm obviously
struggling here.

I like the "FormIsOpen" function idea--was that an actual function? Access
doesn't seem to recognize it; I get an error message when I try to compile it
: "Sub or Function not defined."

Any shove in the right direction for either approach will be greatly
apprecated. Thanks!



:

I have a form (Form C) that can be opened by a command button from either of
two other forms (Forms A and B).

Forms A and B each have lists that need to be updated with info input into
Form C. If there was only ONE form that caled Form C, I would use the ON
CLOSE event for Form C and the code: [Form A.List].Requery.

Of course, if Form C is called by the Form B, then I get an error message
when the form is closed, since it can't find the Form A referred to in the
code.

SO, I'm thinking I should write an "IF...Then" statement that tests to see
whether Form A or From B is open, then does the Requery for the appropriate
list.

But I'm missing the piece of code that would do that test. Would it include
a function, like maybe "If IsActive([Form A)] then..." ? Or something like
"If [Form A].IsOpen = True then ..."

Any ideas? (And does anyone know off a good reference source for all the
functions and methods that are available to use?)

THanks!
 
As always, You people are great! Thanks for the help!

I went with the OpenArgs approach. Form A now has an OpenArgs string in the
DoCmd.OpenForm command that opens Form C, and Form B does not. For the On
Close event for Form C I now have an If...Then statement that begins with
"If Not IsNull(Me.OpenArgs)..." So i just use the presence of any string
being sent to form C via OpenArgs to identify which form opened it. Maybe not
the most robust approach--the OpenARgs string could be anything, I just wrote
"OpenArgs"-- but it seems to work.

fredg said:
I have a form (Form C) that can be opened by a command button from either of
two other forms (Forms A and B).

Forms A and B each have lists that need to be updated with info input into
Form C. If there was only ONE form that caled Form C, I would use the ON
CLOSE event for Form C and the code: [Form A.List].Requery.

Of course, if Form C is called by the Form B, then I get an error message
when the form is closed, since it can't find the Form A referred to in the
code.

SO, I'm thinking I should write an "IF...Then" statement that tests to see
whether Form A or From B is open, then does the Requery for the appropriate
list.

But I'm missing the piece of code that would do that test. Would it include
a function, like maybe "If IsActive([Form A)] then..." ? Or something like
"If [Form A].IsOpen = True then ..."

Any ideas? (And does anyone know off a good reference source for all the
functions and methods that are available to use?)

THanks!

The easiest way for me is to code the click event on FormA and on
FormB like this:

DoCmd.OpenForm "FormC", , , , ,acDialog , Me.Name

Note: using acDialog stops all execution until you then close or hide
FormC. I think that is what you want. If not, retain the comma but do
not write acDialog.
Me.Name should be written exactly as I have.
No need to actually write the form's name.

On Form C, code the Declarations section (up at the very top) of the
form's code sheet:
Option Explicit
Dim strWho as String

Then code the formC Load event:
If Not IsNull(Me.OpenArgs) Then
strWho = Me.OpenArgs
End If

Then, where ever else in your code you need to requery the first form,
use the syntax
forms(strWho).Requery (or whatever you want to do).
 
I like this function for this.

Function isLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet
view.

Const conObjStateClosed = 0
Const conDesignView = 0

isLoaded = False

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
isLoaded = True
End If
End If

End Function
 
No. Irish Pubs are great, although Taverna's are better.

el said:
As always, You people are great! Thanks for the help!

I went with the OpenArgs approach. Form A now has an OpenArgs string in the
DoCmd.OpenForm command that opens Form C, and Form B does not. For the On
Close event for Form C I now have an If...Then statement that begins with
"If Not IsNull(Me.OpenArgs)..." So i just use the presence of any string
being sent to form C via OpenArgs to identify which form opened it. Maybe not
the most robust approach--the OpenARgs string could be anything, I just wrote
"OpenArgs"-- but it seems to work.

:

I have a form (Form C) that can be opened by a command button from either of
two other forms (Forms A and B).

Forms A and B each have lists that need to be updated with info input into
Form C. If there was only ONE form that caled Form C, I would use the ON
CLOSE event for Form C and the code: [Form A.List].Requery.

Of course, if Form C is called by the Form B, then I get an error message
when the form is closed, since it can't find the Form A referred to in the
code.

SO, I'm thinking I should write an "IF...Then" statement that tests to see
whether Form A or From B is open, then does the Requery for the appropriate
list.

But I'm missing the piece of code that would do that test. Would it include
a function, like maybe "If IsActive([Form A)] then..." ? Or something like
"If [Form A].IsOpen = True then ..."

Any ideas? (And does anyone know off a good reference source for all the
functions and methods that are available to use?)

THanks!

The easiest way for me is to code the click event on FormA and on
FormB like this:

DoCmd.OpenForm "FormC", , , , ,acDialog , Me.Name

Note: using acDialog stops all execution until you then close or hide
FormC. I think that is what you want. If not, retain the comma but do
not write acDialog.
Me.Name should be written exactly as I have.
No need to actually write the form's name.

On Form C, code the Declarations section (up at the very top) of the
form's code sheet:
Option Explicit
Dim strWho as String

Then code the formC Load event:
If Not IsNull(Me.OpenArgs) Then
strWho = Me.OpenArgs
End If

Then, where ever else in your code you need to requery the first form,
use the syntax
forms(strWho).Requery (or whatever you want to do).
 
Back
Top