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

dim vs. private? 2

Status
Not open for further replies.

Eupher

MIS
Jul 18, 2002
1,724
US
I feel like I should know this, but haven't pinned it down yet. Is there any functional difference between declaring a variable with a Private statement vs. a Dim statement? Looking at the help files, it appears the scope of each is the same. What am I missing? Thanks!

Ken S.
 
Ken
I am looking at Alison Balter's book "Mastering Access 2000 Development."

She uses as an example
Private mintAge As Integer
and then says...
"The value of a variable declared as Private is that it can be changed by any subroutine or function within that module. For example, the following subroutine increments the value of the variable mintAge by 1."
Private Sub cmdModuleAge_Click()
mintAge = mintAge + 1
Me.txtNewAge = mintAge
End Sub


She adds...
"Notice the naming convention of using the letter 'm' to prefix the name of the variable which denotes the variable as a Private module-level variable. You should use Private declarations only for variables that need to be seen by multiple procedures in the same module; aim for making most of your variables Local to make your code modular and more bullet-proof."

Does that help?

Tom
 
Thanks, Tom - but how is that different from declaring a module level Dim variable? IOW, what is the functional difference between these 2?:

Code:
Option Compare Database
Option Explicit
[b]Dim[/b] strMyString As String
Or:
Code:
Option Compare Database
Option Explicit
[b]Private[/b] strMyString As String
From the description in the Help files it seems Dim is the only variable that can be declared within a procedure; but I can't see any difference between Private and Dim when declared at the module level. Looks like module-level Dim and Private variables are both available to all procedures within that module. Or have I got it wrong somehow?

Ken S.
 
private variables are available to all procedures within that module, but NOT to anything else that's OUTSIDE of that module...

i.e. if you want to access formB's private variable from formA, you can't.

but if you use Dim then you can.

--------------------
Procrastinate Now!
 
Crowley16,

Thanks for your response. Your answer surprised me, so I did a bit of testing. The Access VBA compiler disagrees with you. If I declare a form module-level Dim variable on FormA, any code to initialize it from a procedure on FormB results in a compiler error - "variable not defined." If I declare a Dim or Private variable in a regular module and compile, same error. If I declare a form module-level Public variable on FormA and initialize it from FormB using the fully qualified variable name i.e. Forms!FormA.variablename, then the variable is available as long as FormA is open. Not so with Dim or Private - they will compile successfully, but will bomb at run-time.

So from my tests it looks like:

1) Public variables declared in a regular module are available throughout the project.
2) Private and Dim variables declared in a regular module are available to procedures within that module.
3) Public variables declaredin a form's module are available throughout the project if the form is open and the variable is referred to by its fully qualified name.
4) Private and Dim variables declared in a form's module are only available to procedures within that form's module.

And I still can't find a functional difference between module-level Dim and Private variables.

Ken S.
 
I still can't find a functional difference between module-level Dim and Private variables.

I know it's risky to use VB as an example for VBA but VB help specifically says:

At the module level, there is no difference between Private and Dim. However, using Private is recommended because it readily contrasts with Public and makes your code easier to read.

Geoff Franklin
 
Aha! I thought as much. Thanks, Geoff.

Ken S.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top