• Determining the state of a form (XP)

    Author
    Topic
    #380835

    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)

    Viewing 1 reply thread
    Author
    Replies
    • #639365

      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

      • #639366

        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!!

        groovin

    • #639420

      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.

      • #639477

        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.

        • #639479

          Isn’t the “called” form the active form here?
          Wouldn’t this code just return the name of the “called” form?
          Pat shrug

          • #639480

            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.

            • #639481

              Well done, I didn’t notice that !!
              Pat shrug

        • #639508

          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.

      • #639509

        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.

    Viewing 1 reply thread
    Reply To: Determining the state of a form (XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: