Understanding VBA Data Types

When you create a table in Access, you need to define the data type of every field in the table. Data types in tables include things like Text (for storing short strings of text), Memo (larger chunks of text), Number (for numbers), and Date/Time (for dates and times).

VBA can work with data stored in tables. But just like tables, VBA often needs to know the type of information it's working with. As you see shortly, VBA code has a couple of places where you can define data types. You need to know what the various data types mean.

Table 4-1 lists the data types that you work with in VBA. The data type names are listed in the left column, each followed by a brief description. The Storage Size column shows how many bytes each data type consumes. The Declaration Character column shows an optional character that can be used at the end of a name to specify a data type. That's really more information than you need; just knowing the names of the various data types is sufficient for now.

Table 4-1 lists the data types that you work with in VBA. The data type names are listed in the left column, each followed by a brief description. The Storage Size column shows how many bytes each data type consumes. The Declaration Character column shows an optional character that can be used at the end of a name to specify a data type. That's really more information than you need; just knowing the names of the various data types is sufficient for now.

Table 4-1

VBA Data Types

Data Type

Acceptable Values

Storage Size

Declaration Character

Boolean

True (-1) or False (0)

2 bytes

Byte

0 to 255

1 byte

Currency

-922,337,203,685,477.5808 to 922,337,203,685,477.5807

8 bytes

@

Date

January 1, 100 to December 31, 9999

8 bytes

Double

-1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values

8 bytes

#

(continued)

Table 4-1 (continued)

Data Type

Acceptable Values

Storage Size

Declaration Character

Integer

-32,768 to 32,767

2 bytes

g, %

Long

-2,147,483,648 to 2,147,483,647

4 bytes

&

Object

Name of any object

4 bytes

Single

-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values

4 bytes

1

String

Any text from 0 to about 2,000,000,000 characters in length

10 + string length

$

Variant (no text)

Any number up to the range of the Double data type

16 bytes

Variant (with text)

Any text up to 2,000,000,000 characters long

22 + string length

In VBA code, you often use data types just to store little bits of information for short periods. The reasons for storing data with VBA code vary. One of the first places you're likely to encounter data types in VBA is when you want to pass data to, or from, your custom procedure.

0 0

Post a comment