473,382 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,382 developers and data experts.

Variable Scope in VBA for MS Access

MMcCarthy
14,534 Expert Mod 8TB
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage.

Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function.

Definition of Scope
The scope of a variable where this variable can be seen or accessed from.

The levels of scope for a variable can be broken down as follows:

Procedure Scope

When a variable is declared inside a procedure it is only available within one instance of that procedure. Variables that are intended to have scope throughout a procedure should all be declared at the beginning of the procedure. Variables declared within a procedure can be declared using Dim or Static (static will be explained further in this tutorial).

Module Scope

When a variable is declared within a module but outside of any procedure then it is available thoughout the module. For Form modules the scope is limited to the code region of the form. For standard modules the limit of scope will depend on the type of declaration.

Private
If you Dim a variable in a module it will default to Private. This limits the scope of the variable to the module in which it is declared (when declared outside any procedure).

Expand|Select|Wrap|Line Numbers
  1. Dim str As String
Public
If you explicitly declare a variable Public (Dim is optional), this variable is available throughout the database. When used in a standard module there are no restrictions. This replaces the old use of Global as a variable declaration. If the module concerned is a Form module, the variable is restricted to the code region of the form in which it is declared. You cannot declare a public variable within a procedure. This will give an error. Furthermore, I've found that Public (module) variables are inaccessible to forms / reports in Access 2003.

Expand|Select|Wrap|Line Numbers
  1. Public str As String
Static
If a variable is declared Static (Dim is optional) it remains in existance and retains it's value even after the instance of the procedure in which it is declared, terminates. You can only declare Static variables within a procedure. A Static variable has a longer lifetime than an instance of the procedure in which it is declared. It remains in existance until the project terminates. The project can terminate or be reset without closure of the database.

Expand|Select|Wrap|Line Numbers
  1. Static str As String
NOTE:
All variables should be declared at the beginning of the code region in which they appear whether that is a module, procedure or block.
Jun 5 '07 #1
0 35156

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Tom | last post by:
I'm tying myself in knots trying to figure out variable scope with constants and include files. This is what I'm doing: A page (index.php) on my website includes a general purpose include file...
7
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
1
by: Luxore | last post by:
Hello, I am trying to create threaded python project and I'm running into some weird Python variable scoping. I am using the "thread" module (I know, it's old and I should be using...
2
by: Chris | last post by:
Hi, I'm having trouble with variable scope inside procedures. this is pseudocode LDAP NewUser; if (something = true) { NewUser = LDAP.FindUser(use emailaddress)
5
by: JohnR | last post by:
in VB.NET I'm trying to access my variable without resorting to SHARED scope. Here's the situation: class MYLABEL inherits LABEL and has an additional property called MYVAR. In MYLABEL I set an...
0
by: EADeveloper | last post by:
Quick ASP.Net architectural question.... We're creating a set of standard web controls for use in our app, and those custom controls need access to a set of variables that we are initializing at...
2
by: Kevin Walzer | last post by:
I'm trying to construct a simple Tkinter GUI and I'm having trouble with getting the value of an entry widget and passing it onto a callback function. But I'm not grokking variable scope correctly....
5
by: Jeff | last post by:
Hey Below is a C# program I've made.... it's an app where I experiment with variable scope. In this code you see two variables named i (one is a local variable and the other is a member of the...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.