Every variable you intend to use in your application must
have a name. This name
allows you to identify the area of memory that would have been
reserved for a variable. There are rules you must observe when naming your
variables. The rules are those of Microsoft Visual Basic (and not Microsoft
Access):
- The name must begin with a letter (a-z or A-Z) or an
underscore
- The name cannot contain a period (.) or a special character
- The name must not contain a space
- The name must not exceed 255 characters. You should limit the name of a
variable to 30 characters
- The name must be unique in the same scope
Besides, or on top of, these rules, you can add
your own conventions that would make your code easier to understand.
Practical
Learning: Introducing Variables |
|
- Start Microsoft Access and, from the resources that accompany this
site, open the Exercise1 database
- In the Database window, click the Forms button
- To create a form, double-click Create Form in Design View
- On the Standard toolbar, click the Code button
- In the Object combo box, select Detail
- In the Procedure combo box, select Click
- Press Tab and type the following:
Private Sub Detail_Click()
SomeColor = vbRed
Detail.BackColor = SomeColor
End Sub
|
- Return to Microsoft Access and display the form in Form View
- Click the form and notice that it appears red
- After using the form, switch it back to Design View and return to
Microsoft Visual Basic
When writing your code, you can use any variable just by
specifying its name. When you provide this name, the computer
directly creates an area in memory for it. Microsoft Visual Basic allows you to directly use any name for a variable as
you see fit. If you use various variables like that, this could
result in many confusions in your code. As mentioned earlier, you
can first declare a variable before using it.
|
|
To declare a variable, you use the Dim
keyword followed by the name of the variable. Here is an example:
Private Sub Form_Load()
Dim BackgroundColor
End Sub
Declaring a variable simply communicates to
Visual Basic the name of that variable. You can still use a mix of
declared and not-declared variables. If you declare one variable and
then start using another variable with a similar but somewhat
different name, Visual Basic would still consider that you
are using two variables. This can create a great deal of
confusion because you may be trying to use the same variable
referred to twice. The solution to this possible confusion is to
tell Visual Basic that a variable cannot be used if it has not been
primarily declared. To communicate this, on top of each file you use
in the Code Editor, type
Option Explicit
This can also be done automatically for each
file by checking the Require Variable Declaration in the
Options dialog box.
Practical Learning: Using a Variable
|
|
- On the main menu of Microsoft Visual Basic, click Tools ->
Options...
- Click the Editor property page. In the Code Settings section, put a
check mark in the Require Variable Declaration check box
- Click OK and return to Microsoft Access
- Close Microsoft Visual Basic
- From Microsoft Access, open the Department of Records and Statistics
database you created in Lesson 1
- When asked whether you want to save the form, click No
- To create a form, in the Forms section of the Database window,
double-click Create Form In Design View
- On the Standard toolbar, click the Code button and notice that the
top section of the file now displays Option Explicit
Every time the user enters a value in an application.
That value is primarily considered as text. This means that, if you want
to use such a value in an expression or a calculation that expects a
specific value other than text, you must convert it from that text.
Fortunately, Microsoft Visual Basic provides an effective mechanism to
convert a text value to one of the other values we will see next.
To convert text to another value, there is a function
adapted for the purpose and that depends on the type of value you want to
convert it to. We will mention each when necessary. |
|
|
|