Programming Fundamentals Using VBA - Constants
(Page 4 of 9 )
Many times you may want to declare a value that will not change. This type is called a Constant and is declared using the keyword Const in place of the normal declaration using Dim.
As an example:
Const conNumber1 As Integer
Const conDate As Date = #03/02/04#
Notice that you preface the variable’s name with con instead of the normal type. Also, when declaring a constant, you must assign it an initial value, or you will get a syntax error message when you leave the line.
Input and Output You now know how to save data with the use of variables. But how do you get the information into a variable? Or read the information that is stored in them? In many ways, that will be what we spend the rest of this book examining. However, let’s begin by looking at a couple of simple techniques for testing our code.
One of the simplest techniques for getting information from the user into a variable is to use a built-in function called InputBox. This will give you a simple dialog box with a prompt.
To code this in our small example, you would enter the shaded lines shown here:
Sub addNumbers()
'Declare the variables
Dim intNumber1 As Integer
Dim intNumber2 As Integer
Dim intSum As Integer
'Create InputBoxes to enter numbers
intNumber1 = InputBox("Enter the first number")
intNumber2 = InputBox("Enter the second number")
End Sub
Notice that for clarity’s sake, I also added a comment to indicate what I am doing.
The entry intNumber1 = InputBox is called an assignment statement, and it assigns the value the user enters to the variable. In this case two assignments have been created: one for intNumber1 and the other for intNumber2.
You can execute the code by selecting the Run button:

That brings up a dialog box like the one shown here for the first entry (a second one will appear for the second entry).

If you followed the preceding steps, the code will run and the variables will be set. However, there is no means of outputting the results. There are two ways to create an output for now. The first way is to add the code, shown with shading, here:
Sub addNumbers()
'Declare the variables
Dim intNumber1 As Integer
Dim intNumber2 As Integer
Dim intSum As Integer
'Create InputBoxes to enter numbers
intNumber1 = InputBox("Enter the first number")
intNumber2 = InputBox("Enter the second number")
'Add numbers
intSum=intNumber1 + intNumber 2
' Create an output
Debug.Print "The numbers entered were " & intNumber1 & " and " & _
intNumber2
End Sub
If you run the code now, you will once again get the two dialog boxes. Notice that the output will appear in the Immediate window, located at the bottom of the editor environment:

This window is used as a temporary testing place for our code and will come in handy initially.
There are several issues worth discussing here. First of all, the line:
Debug.Print "The numbers entered were " & intNumber1 & " and " & _
intNumber2
This is called a concatenation because it brings various components together. The literal text is enclosed in quotation marks, and the variables are not. The two types are separated by the ampersand.
Also, notice that the line is broken with a space and then an underscore. When you break up a statement onto multiple lines, VBA requires this. Throughout this book, we may need to do that for typographical reasons. However, you can keep the code on one line.
Another way of formatting an output is to use a technique you first saw in Chapter 4. You could use a message box by replacing the output line with the following line of code:
MsgBox "The numbers entered were " & intNumber1 & " and " & intNumber2
This will produce output similar to the following:

You now have your first VBA program running.
Next: Control Structures >>
More Visual Basic.NET Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter six of the book Access VBA Programming, written by Charles E. Brown and Ron Petusha (McGraw-Hill/Osborne, 2004; ISBN: 0072231971). Check it out at your favorite bookstore today. Buy this book now.
|
|