Results 1 to 10 of 10
  1. #1
    Star Lounger
    Join Date
    Aug 2002
    Location
    Des Moines, Iowa, USA
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Determining the state of a form (XP)

    If I have a form which can be called from other forms, how can I determine the name of the calling form? My attempt to use Application.CurrentObject does not seem to work, and I cannot find anything similar to Recordset.State to determine a form's status (open or closed)

  2. #2
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining the state of a form (XP)

    Hi Steve

    You can use a global variable for this purpose. You will have to use a "wrapper" function to retrieve global variable's value. You would set value when report is opened from form. Example: In standard code module create Public variable and function:

    Option Compare Database
    Option Explicit

    Public gstrFormName As String

    Public Function GetFormName() As String
    GetFormName = gstrFormName
    End Function

    When opening report use code like this:

    Private Sub OpenRpt_btn_Click()

    Dim strRpt As String

    gstrFormName = Me.Name
    strRpt = "rptEmployees"
    DoCmd.OpenReport strRpt, acPreview

    End Sub

    To get name of form in report, call GetFormName function. To test this, add unbound textbox to report with following expression as ControlSource:

    ="Form Opened by: " & GetFormName()

    The textbox will display name of form that opened report.

    HTH

  3. #3
    Star Lounger
    Join Date
    Aug 2002
    Location
    Des Moines, Iowa, USA
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining the state of a form (XP)

    Thank you!!! I was so wrapped up in trying to find a method that I did not think about a global variable. I think this should work pretty slick!!

    <img src=/S/groovin.gif border=0 alt=groovin width=21 height=21>

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Determining the state of a form (XP)

    A somewhat more reliable approach in the event that you may have several forms opened at once and might overwrite that global, is to store the information in the form itself. When you open the form, use the OpenArgs argument of the DoCmd.OpenForm call to pass in the name of the calling form. Then in the close event of the called form, just reference the form name you got as a string in OpenArgs.
    Charlotte

  5. #5
    Star Lounger
    Join Date
    Dec 2001
    Location
    Fredensborg, Denmark
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining the state of a form (XP)

    Why not do it the easy way, no global needed. In the open event of the called form:

    Dim strForm as String
    On Error Resume Next
    strForm=Screen.ActiveForm.Name
    If strForm="" Then
    ........ not opened by another form
    Else
    ........ opened by another form
    Endif

    If the form can only be opened from another form you don't need the error trapping.

  6. #6
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Determining the state of a form (XP)

    Isn't the "called" form the active form here?
    Wouldn't this code just return the name of the "called" form?
    Pat <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

  7. #7
    Star Lounger
    Join Date
    Dec 2001
    Location
    Fredensborg, Denmark
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining the state of a form (XP)

    No Patrick, when you first open a form, the following events occur in this order:

    Open -> Load -> Resize -> Activate -> Current

    Hence, the called form is not yet the active form.

    If the called form might be open already - put the code in the Load event.

  8. #8
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Determining the state of a form (XP)

    Well done, I didn't notice that !!
    Pat <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

  9. #9
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Determining the state of a form (XP)

    The only problem I see with that is that the strForm variable needs to be declared at the module level so that it can be used in the Close event.
    Charlotte

  10. #10
    Star Lounger
    Join Date
    Aug 2002
    Location
    Des Moines, Iowa, USA
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining the state of a form (XP)

    Wow!!! What a wealth of information. It's always great to get multiple methods to attack an issue. Each situation may need a different solution. I'm very impressed with the variety of responses, and will have to check into all the possibilities to see which one will work best for me!!

    Thanks to everyone for their ideas and methods - it was more of a discussion than I had anticipated. Hopefully someday I can get to a point where I too can achieve guru status and begin answering questions such as these.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •