Results 1 to 5 of 5

Thread: Universal Variable?

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Universal Variable?

    I know this is a stupid question but I'm really not sure how to do it. I just want to be able to set a variable that can be accessed from any module or form. I know I can just create a form with a textbox that anything can access but is there a way to accomplish the same thing except using a variable instead of a textbox/object? Thank you

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Universal Variable?

    Declare the variable as Public in a bas module:

    vb Code:
    1. ' in Module1
    2. Option Explicit
    3.  
    4. Public MyVar As Long

    From a Form, you can call it like:
    vb Code:
    1. MyVar = 1
    2. 'or,
    3. Module1.MyVar = 1 'Better. Reduces confusion
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: Universal Variable?

    Can I do the same thing if I'm setting the variable in a class module?

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Universal Variable?

    Quote Originally Posted by abazabam
    Can I do the same thing if I'm setting the variable in a class module?
    You'll have to declare the class as public, ie:

    vb Code:
    1. 'In a module
    2. Public myClass As Class1
    3.  
    4. 'In a form
    5. Private Sub Form_Load()
    6. Set myClass = New Class1
    7. myClass.Variable = 2
    8. End Sub
    9.  
    10. 'In another form
    11. Private Sub Form_Load()
    12. MsgBox myClass.Variable '2
    13. End Sub

    And don't forget to destroy the object before your program closes by using: Set myClass = Nothing

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Universal Variable?

    This link will further explain what iPrank and DigiRev have told you.

Posting Permissions

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



Click Here to Expand Forum to Full Width