VBA for MS Access 2010 tutorial- declare and access variables

Declare and access variables


In VBA, a variable can be declared at 2 levels—local and global.

-Declaring a variable at Local Level

At local level is declared in Sub-procedure  between Sub and End Sub or Function and End Function. By declaring the variable at the local level, the variable scope is limited to only the sub-procedure. This means that the variable can’t be used or accessed outside the sub-procedure. The variable declaration prototype is:

 Dim variable_name As DataType
 
Example: To declare a variable called x to store integer value, you can write as seen below:
 
   Sub mylocal
      Dim x As Integer 'Declare a variable called x to store an integer value
   End sub

-Declaring a variable at Global Level

You can declare a variable at the global level by declaring it in the Form module or the Standard modul.+Form module: In the Form module, you will declare a variable in the general section of the form. By declaring the variable in the Form module, the variable can be accessed from any code block of the form but can’t be accessed from other forms.

Example: Create a Form (Create-> Form Design) then click on View Code icon in the Ribbon menu.

 

View code--Access 2010  

 

To declare a variable called lstname to store string value in the Form module, you can write as seen below:

Dim lstname As String


Declaring Variable in form module--Access 2007


 


+Standard module: To declare a variable in the Standard module, you

 need to create a module by using the Visual Basic Editor:

Module Declaraton-Access 2010

 

In the Standard module, you need to declare the variable with public or      private keyword instead of Dim keyword. By declaring with the public keyword the variable can be accessed through out your application. However, with private keyword the variable can be accessed only within its module. The variable declaration in the Standard module can be generally written as shown below:
Public Variable_Name As Datatype Private Variable_Name As Datatype   Example: To declare a variable called PI to store a float value in the Standard module, you can write:

Public PI As Single





Comments

Name:
Comment:
Enter the text:
CAPTCHA image

.........................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word.
However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like