Results 1 to 3 of 3

Thread: use same variables in different procedures

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    12

    use same variables in different procedures

    it´s me again..... ;-)


    there is another problem:
    i need to use variables i used in the first sub in the second sub too.


    how can i manage this? it´s very important!

    please help me!

    thx, alex

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Its to do with scope (read up on it).

    Two ways:
    1) If you are coding in the same module (form module or normal module) then you need to declare the variables in that module. These variables can be accessed to that module only (private) but any sub/function in that module, or by the whole application/VBA project (public)
    eg:
    VB Code:
    1. public strAString as string
    2.  
    3. public sub teststr()
    4.     msgbox strAString
    5. end sub

    2) You pass all variables required as parameters, including objects. Read up on Byref and ByVal
    Example:
    VB Code:
    1. public sub test1()
    2. '---- declare and set up lngValue and rst in here before using / calling the second sub
    3.  
    4.    test2(lngValue,rst)
    5. end sub
    6.  
    7. public sub test2(byval lngAValue as long, byref rst as adodb.recordset)
    8.  
    9. end sub


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    12
    but i have two subs where i need to use the variables from the former sub in the one that comes later.


    i declared the variables before any sub starts:

    Public time As Integer
    Public time1 As Integer

    ....and so on......

    then:

    Public timestring As String
    Public timestring1 As String

    Then the first Sub starts:

    Public Sub DatenAnzeigen_Click()

    here i set the values for the variables:

    time=Workbooks(2).Worksheets("Zeiterfassung").Range("E9")
    time1=------------------------------------------------------.Range("E10")


    timestring=CStr(time)
    timestring1=CStr(time1)


    End Sub


    Public Sub DatenAnzeigen2()

    And here i need to use the variables declared in the former sub.


    Have i made any mistakes?

    please tell me

    thx, alex

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