How do I assign an object reference once so I can use it in multiple subs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryaanmichael
    New Member
    • May 2012
    • 24

    How do I assign an object reference once so I can use it in multiple subs?

    I have multiple events (click, afterupdate, etc) that perform different actions using the same object. How do I assign an object's reference only once so that I don't have to retype it for every sub or function. For example, the below code works, but I'm trying to avoid typing the "Set userName = me.userName" for every event. Isn't there a way to define it and use it for all subs? Here is a general script the shows what I mean.

    Code:
    Public Sub button1_Click
    Dim userName, field1
    Set userName = me.userName
    Set field1 = me.field1
    MsgBox "Button 1 was clicked"
    End Sub
    
    Public Sub button2_Click
    Dim userName, amount1
    Set userName = me.userName
    Set amount1 = me.amount1
    MsgBox "Hi " & userName & ". The amount is:" & amount1 
    End Sub
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Declare the Variable as 'Public' in a Standard Code Module. In this manner, once it has been assigned/re-assigned a Value, it will be Visible (Global) as long as the Database is Open.
    Code:
    'In a Standard Code Module
    Public userName As String

    Comment

    • ryaanmichael
      New Member
      • May 2012
      • 24

      #3
      Hi ADezii,

      Thank you for your response. Where do I insert the "Set userName = me.userName" declaration so that it is available globally as well?

      Originally posted by ADezii
      Declare the Variable as 'Public' in a Standard Code Module. In this manner, once it has been assigned/re-assigned a Value, it will be Visible (Global) as long as the Database is Open.
      Code:
      'In a Standard Code Module
      Public userName As String

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Wherever you want it to be first initialized.

        Comment

        • ryaanmichael
          New Member
          • May 2012
          • 24

          #5
          Hmm I can't get it to work. Maybe I'm not understanding what a "Standard Code Module" is. I create this by going to "Insert" and then "Module", correct?

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            I sent you a very simply Demo that will:
            1. Declare a Global Variable in a Standard Code Module (Module1).
              Code:
              Public strCurrentUser As String
            2. Initialize that Variable in the Load() Event of the Main Form (frmTest).
              Code:
              Private Sub Form_Load()
                strCurrentUser = Application.CurrentUser
              End Sub
            3. Retrieve the Value stored in that Variable when a Command Button (cmdTest) on frmTest is clicked.
              Code:
              Private Sub cmdTest_Click()
               MsgBox "The Current User is " & strCurrentUser
              End Sub
            4. This should be everything you need to get the overall picture.
            Attached Files

            Comment

            • ryaanmichael
              New Member
              • May 2012
              • 24

              #7
              Thanks so much ADezii, I understand now. Thanks!

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                You are quite welcome.

                Comment

                Working...