2.4. Using Variables and Constants to Store Values
Now that you have a basic understanding of how to create procedures, turn your attention to learning how to use variables and constants in those procedures. Variables and constants can be used to store values that are used by your Access program. You have already seen a few examples of using variables to store values. This chapter covers variables in a bit more detail because they are so important to writing VBA code.
2.4.1. Types of Variables
Various types of variables can be declared and used in your procedures. The most common variables are probably String, Integer, Long, Currency, and Date, although other variables are also commonly used. The following table illustrates the various data types that are available in VBA and offers an explanation of the type of value each can store.
Figure 2.25. Figure 2-25
| Data Type | What It Stores |
|---|---|
| Attachment | File attachment |
| Boolean | True or false |
| Byte | Positive integers from 0 to 255 |
| Currency | Positive and negative currency values with four decimal places |
| Date | Date and time from 1/1/0100 to 12/31/9999 |
| Double | 8-byte decimal values |
| Hyperlink | URL for a hyperlink |
| Integer | 2-byte integers from −32,768 to +32,768 |
| Long | 4-byte integers from −2 billion to +2 billion |
| Object | Access object reference |
| Single | 4-byte decimal values |
| Data Type | What It Stores |
|---|---|
| String (variable length) | From 0 to 2 billion characters |
| String (fixed length) | From ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access