Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Dim Statement

Status
Not open for further replies.

hockeyman9474

IS-IT--Management
Oct 11, 2007
17
US
Hey guys. It's been a while since I've done this. Can somebody please provide me with the correct coding to populate a variable that can be used on another form. I know that I have to dim it at the module level and make that module public, but I can't remember what the coding looks like.

Thanks
 



HELP said:
Public Statement


Used at module level to declare public variables and allocate storage space.

Syntax

Public [WithEvents] varname[([subscripts])] [As [New] type] [,[WithEvents] varname[([subscripts])] [As [New] type]] . . .

The Public statement syntax has these parts:

Part Description
WithEvents Optional. Keyword specifying that varname is an object variable used to respond to events triggered by an ActiveX object. WithEvents is valid only in class modules. You can declare as many individual variables as you like using WithEvents, but you can't create arrays with WithEvents. You can't use New with WithEvents.
varname Required. Name of the variable; follows standard variable naming conventions.
subscripts Optional. Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax:
[lower To] upper [,[lower To] upper] . . .

When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.

New Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsic data type, can't be used to declare instances of dependent objects, and can't be used with WithEvents.
type Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String, (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable being defined.



Remarks

Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside.

Caution The Public statement can't be used in a class module to declare a fixed-length string variable.

Use the Public statement to declare the data type of a variable. For example, the following statement declares a variable as an Integer:

Public NumberOfEmployees As Integer

Also use a Public statement to declare the object type of a variable. The following statement declares a variable for a new instance of a worksheet.

Public X As New Worksheet

If the New keyword is not used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object.

You can also use the Public statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.

If you don't specify a data type or object type and there is no Deftype statement in the module, the variable is Variant by default.

When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it were a separate variable.
Post back if you have a specific question.

Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
So the module should start like so:


Public Sub btnNext2_Click()
Public LASTNAME As String
 
More like:
Code:
Option Explicit
Public LASTNAME As String

Private Sub btnNext2_Click()

...

Have fun.

---- Andy
 
Andy is right. Declare your Public variables before any procedures (Subs or Functions), right after (hopefully) Option Explicit. Just like Andy's example.

However, if you want to use the variable on "another form", that Public variable has to be in a standard module, NOT the first userform module. A public variable declared on one userform module is NOT public in the module of another userform.

Look up Scope in Help.

faq219-2884

Gerry
My paintings and sculpture
 

Not necessarily...

If the code I gave here is in UserForm1, you can access the value of it, and assign the value to it, from anywhere by refering to that Form:

MsgBox UserForm1.LASTNAME
or
UserForm1.LASTNAME = "Bill Clinton"

Although I agree with Gerry - declaring it in the standard Module (*.bas) is a better way.

Have fun.

---- Andy
 
If the code I gave here is in UserForm1, you can access the value of it, and assign the value to it, from anywhere by refering to that Form:"

NOT quite correct.

Yes, you CAN refer to it (and assign it) - without an error - from Userform2 with:

Msgbox Userform1.Lastname

Say Userform1 sets Lastname = "Jones"

Condition A: Userform1 is still loaded.
U&serform2 instruction: Msgbox Userform1.Lastname
Result: "Jones"

Condition B: Userform1 is NOT still loaded.
U&serform2 instruction: Msgbox Userform1.Lastname
Result: ""

The Public variable in Userform1 only retains its value within scope. Once a object module is closed (and userforms are object modules), then all variables, Public or otherwise, are dead.

The interesting thing about this, is that even if you do not use Userform1 - in other words, you never set Lastname - you can STILL execute:

Msgbox Userform1.Lastname

from Userform2 without an error.

The declaration of a Public variable in ANY module adds it to the listed available variables. You can see it in IntelliSense.

But again, it will ONLY have a value (set in userform1!!!)as long as Userform1 is loaded.

You CAN set it in Userform2.

Public variable in Userform1 is Lastname.

Userform1 is NEVER loaded.

userform2 is loaded, and:

Userform1.Lastname = "Bob"
Msgbox Userform1.Lastname

will return Bob.

So yes, another userform can SET and use a Public variable declared in another userform module. But it can NOT return that variable value if it is set by userform1, and userform1 is not still loaded. Unloading userform1 sets the variable to Null.

For it to retain value (set by userform1) no matter what (to be truly Public IMO), it must be declared in a standard module.


faq219-2884

Gerry
My paintings and sculpture
 
>The interesting thing about this, is that even if you do not use Userform1 - in other words, you never set Lastname - you can STILL execute

This is because VBA forms are autoinstancing classes; make any reference to a method or property of a form and, if an instance of that form is not currently loaded, a new one is instanatiated and the result from that new instance is returned (which in turn means that public variable on that form will contain the default value for that variable type; i.e. the variable doesn't get zeroed, it is actually a brand new variable ...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top