Results 1 to 4 of 4

Thread: [RESOLVED] Variables

  1. #1

    Thread Starter
    Member
    Join Date
    May 1999
    Posts
    57

    Resolved [RESOLVED] Variables

    Is there a way to use a variable throughout an entire project. By declaring a variable as Public, it seems to be limited to the events associated with that particular form, not the project in total.

    Also, I am running into problems trying to transfer the value of more thatn one variable to a module

    Dim NumTeams as Integer
    Dim NumBoards as Integer
    NumTeams = Val(Textboix1.text)
    NumBoards= Val(Textbox2.text)
    Call Modsub(NumTeams,NumBoards)
    That last line syntax workd with only one variable but not more than 1

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Variables

    Thread moved from the FAQ forum, which is not the place to post your questions - VB6 assumed.

    Public is what you want (Private is limited to the code file it is in), the problem you are having is that it is defined in a form - so outside of the form you need to specify the form too, eg:
    Code:
    Form2.Variable = 2
    This is needed because you can create multiple copies of the same form (and thus variables), and need to be able to determine which would be used.


    As to calling the sub, what you showed should be fine, however if you leave out the Call keyword (or if it is a function, use the result) you need to remove the brackets.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Variables

    Use a module for Public variables. They will be accessible throughout the project.

    Any sub or function will only accept the variables for which there are parameters. I.E., if your sub is
    Code:
    Public Sub ModSub(strThisString As String)
    'code
    End Sub
    then it is only going to accept one variable because there is only one parameter in which to feed the variable.

    If you need your sub to take two variables, then create two parameters in the sub.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Variables

    Variable Scope:

    • A variable defined in a Sub or Function is only available to that Sub or Function
    • A variable defined at the top of a form with Private (preferred) or Dim is available to all Subs and Functions in that form and that form only
    • A variable defined as Private at the top of a code module is available to all Subs and Functions in that code module and that code module only
    • A variable defined as Public (preferred) or Global at the top of a code module is available to all Subs and Functions in the app

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