Global Get Form Name sub

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kpfunf
    New Member
    • Feb 2008
    • 78

    Global Get Form Name sub

    I have the following code for a form:
    [CODE=vb]Private Sub CommentsButton( )
    On Error GoTo Err_CommentsBut ton_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    gPreviousForm = Me.Form.Name
    stDocName = "Comments Form"
    DoCmd.Close
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    Exit_CommentsBu tton_Click:
    Exit Sub

    Err_CommentsBut ton_Click:
    MsgBox Err.Description
    Resume Exit_CommentsBu tton_Click

    End Sub[/CODE]
    I have this code in numerous forms (code works great). The issue is, I would like to make a public sub in a standard module, so I can just call a sub rather than have it in the code of each form. But when I paste it into a standard module, and change Private to Public, I get an error because you can't use "Me" in a standard module. How can I edit the code to reference the current form (Me) without using that term?
  • Scott Price
    Recognized Expert Top Contributor
    • Jul 2007
    • 1384

    #2
    Try
    Code:
    Forms!Form.Name
    .

    Regards,
    Scott

    Comment

    • kpfunf
      New Member
      • Feb 2008
      • 78

      #3
      Scott,
      With that change, I get an error message. The code is looking for a form named "Form".

      Comment

      • Scott Price
        Recognized Expert Top Contributor
        • Jul 2007
        • 1384

        #4
        I'm sorry, I should have explained that one a little better.

        Code:
        Forms![Form Name].[Control Name]
        OR
        Code:
        Forms![Form Name].[Property]
        is the correct syntax when referring to a form from outside of it's form module.

        I suspect this isn't going to be helpful to you in this particular instance, since if I understand you correctly, you are attempting to write the form name to a global variable to be used later in your code processing.

        A function that would do this is:

        Code:
        Public Function GetPreviousForm(formName As Form) As String
        
        GetPreviousForm = formName.Name
        
        End Function
        This code would go into a standard code module, the global variable pForm would be declared as String at the top of the module. (i.e. Global pForm As String)

        The code to call this function from any form's code module:

        Code:
        pForm = GetPreviousForm(Me)
        Debug.Print pForm
        Regards,
        Scott

        Comment

        • kpfunf
          New Member
          • Feb 2008
          • 78

          #5
          Scott,
          I think we're almost done. I have the following code in the Global module:
          [CODE=vb]Option Compare Database

          Global pForm As String

          Public Function GetPreviousForm (formName As Form) As String

          GetPreviousForm = formName.Name

          End Function
          Public Sub CommentOpen()
          On Error GoTo Err_CommentsBut ton_Click

          Dim stDocName As String
          Dim stLinkCriteria As String
          pForm = GetPreviousForm (Me)
          stDocName = "Comments Form"
          DoCmd.Close
          DoCmd.OpenForm stDocName, , , stLinkCriteria

          Exit_CommentsBu tton_Click:
          Exit Sub

          Err_CommentsBut ton_Click:
          MsgBox Err.Description
          Resume Exit_CommentsBu tton_Click

          End Sub[/CODE]

          For each form, in it's code I have [CODE=vb]Call CommentOpen[/CODE]
          When run, this error: "Compile Error: Invalid use of Me keyword" on line 15 above.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32657

            #6
            If you consider that Me only has any meaning in the form's module, you need to take out any references to Me in the global procedures, and change your calling code (from the form module) to :
            Code:
            Call CommentOpen(Me)
            The declaration for CommentOpen() should now be :
            Code:
            Public Sub CommentOpen(frmForm As Form)
            ...and line 15 should now be :
            Code:
            pForm = GetPreviousForm(frmForm)

            Comment

            • kpfunf
              New Member
              • Feb 2008
              • 78

              #7
              Thanks Scott and NeoPa.


              NeoPa, do you have any links to articles explaining Subs and arglists? I've learned programming "on-the-fly" and don't have a complete understanding of the building blocks. Thanks!

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32657

                #8
                If you go to the VBA window (Alt-F11 from Access) and select Help / Microsoft Visual Basic Help, you will find a Contents tab.

                From here navigate to Visual basic Language reference / Keywords / Public and then select the link for Function Statement or Sub Statement.

                You should find all the details you need in here.

                Comment

                Working...