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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value of Variable does not carry forward 1

Status
Not open for further replies.

OscarAlberto

Programmer
Dec 4, 2002
15
CR
I define a Dim variable - and give it a value - in one UserForm procedure and within this procedure, call another Module procedure in the same project.

The variable does not hold its value when executing the Module procedure. I used a MsgBox to determine that the value when executing the new procedure is 0 (zero), but I gave it an 8 value in the UserForm procedure.


How can I make the variable hold its value when the Module procedure is called????
 
I hope this helps:
- The quick (and "incorrect" according to MSDN Library) way: make the variable public.

- The hard (and "correct") way: store the variable in the system registry or a separate .ini file.

I can't say I really know how to do the latter, but it's the way you're supposed to do it. Do some searching on the MSDN Library and you'll see what I mean.

The trouble with "public" variables is that they are too free to mess up the system... or somethin'

 
oh yeah...you can pass the variable by reference (default) to the new procedure. That might work too...

(Otherwise the operation will create a new instance of the variable that lives local to the procedure. )

you can modify or simply use this referenced variable inside the new procedure and it will reflect on the parent procedure that called that variable.

make sense?
 
Please clarify, since I am new to VBA (and programming as a matter of fact)
 
Oscar,

You need to do some reading at a minimum in VBA Help. I would recommend that you purchase a reference book on VBA programming in the application that you are using.

But here's a help snippit on the scope of variables...

Understanding Scope and Visibility


Scope refers to the availability of a variable, constant, or procedure for use by another procedure. There are three scoping levels: procedure-level, private module-level, and public module-level.

You determine the scope of a variable when you declare it. It's a good idea to declare all variables explicitly to avoid naming-conflict errors between variables with different scopes.

Defining Procedure-Level Scope
A variable or constant defined within a procedure is not visible outside that procedure. Only the procedure that contains the variable declaration can use it. In the following example, the first procedure displays a message box that contains a string. The second procedure displays a blank message box because the variable strMsg is local to the first procedure.

Sub LocalVariable()
Dim strMsg As String
strMsg = "This variable can't be used outside this procedure."
MsgBox strMsg
End Sub

Sub OutsideScope()
MsgBox strMsg
End Sub

Defining Private Module-Level Scope
You can define module-level variables and constants in the Declarations section of a module. Module-level variables can be either public or private. Public variables are available to all procedures in all modules in a project; private variables are available only to procedures in that module. By default, variables declared with the Dim statement in the Declarations section are scoped as private. However, by preceding the variable with the Private keyword, the scope is obvious in your code.

In the following example, the string variable strMsg is available to any procedures defined in the module. When the second procedure is called, it displays the contents of the string variable strMsg in a dialog box.

' Add following to Declarations section of module.
Private strMsg sAs String

Sub InitializePrivateVariable()
strMsg = "This variable can't be used outside this module."
End Sub

Sub UsePrivateVariable()
MsgBox strMsg
End Sub

Note Public procedures in a standard module or class module are available to any referencing project. To limit the scope of all procedures in a module to the current project, add an Option Private Module statement to the Declarations section of the module. Public variables and procedures will still be available to other procedures in the current project, but not to referencing projects.

Defining Public Module-Level Scope
If you declare a module-level variable as public, it's available to all procedures in the project. In the following example, the string variable strMsg can be used by any procedure in any module in the project.

' Include in Declarations section of module.
Public strMsg As String

All procedures are public by default, except for event procedures. When Visual Basic creates an event procedure, the Private keyword is automatically inserted before the procedure declaration. For all other procedures, you must explicitly declare the procedure with the Private keyword if you do not want it to be public.

You can use public procedures, variables, and constants defined in standard modules or class modules from referencing projects. However, you must first set a reference to the project in which they are defined.

Public procedures, variables, and constants defined in other than standard or class modules, such as form modules or report modules, are not available to referencing projects, because these modules are private to the project in which they reside.


Skip,
Skip@TheOfficeExperts.com
 
Oscar: There are many ways to do what you have suggested. Here is a simple example:

With a form having one text box and one command button, put this in the form's code page:
[blue]
Code:
  Private Sub CommandButton1_Click()
  Dim sText As String
    sText = TextBox1.Text
    Call AnotherSub(sText)
    TextBox1.Text = sText
  End Sub
[/color]

Put this code in a separate code module:
[blue]
Code:
  Sub AnotherSub(SomeText As String)
    MsgBox SomeText
    SomeText = "xyz"
  End Sub
[/color]

Run (F5) the form. Enter "abc" and click the button. You should see the message box indicating that the value from the text box (actually the sText variable) is available in the sub. Clear the message box and you should see that the sub can pass back a new value in the same variable.

Here is a slightly different way to do it, using a function instead of a sub. (Study the help file to understand the difference.)

Form code:[blue]
Code:
Private Sub CommandButton1_Click()
  TextBox1.Text = MyFunction(TextBox1.Text)
End Sub
[/color]

Module code:[blue]
Code:
Function MyFunction(SomeText As String) As String
  MsgBox SomeText
  MyFunction = "xyz"
End Function
[/color]

As you can see, using a function can result in fewer lines of code and fewer variables needing to be defined.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top