Microsoft Access Database Development With VBA

Microsoft Access Topics: Enumerations

   

Introduction

Consider the following list:

Integer Item
1 Female
2 Male
3 Unknown

When writing a program, you can use 1 to represent Female. You can use 2 to represent Male. Consider this other list:

Integer Item
1 Single Family
2 Townhouse
3 Condominium
4 Unknown

This time, you can use 1 to represent a single family house and you can use 2 to represent a townhouse. One of the problems with these arrangement is that the numbers are vague and you must remember them exactly. Instead of just remembering that the constant 1 represents Single Family, you can create a list that has that type of house. In another list, instead of using 1 again, you can give it a name. Consequently, in each list, although the constant 1 would still be considered, at least it would mean something precise.

An enumeration is a series of constant integers that each has a specific position in the list and can be recognized by a meaningful name. To create an enumeration, you use the Enum keyword, followed by the name of the enumeration, press Enter, and end with End Enum:

Enum HouseTypes

End Enum

The name of the enumerator and the name of each item of the list follows the rules we reviewed for names. The section between both lines is referred to as the body of the enumeration. In the body of the enumeration, type the name of each item on its own line. The name follows the same rules we reviewed for other names. Here are examples:

Enum HouseTypes
    SingleFamily
    Townhouse
    Condominiium
    Unknown
End Enum

Using an Enumeration

After creating an enumeration, you can use it. To do this, declare a variable for it, just like we have done so far to declare other variables. Here is an example:

Private Sub cmdEnumeration_Click()
    Dim HouseType As HouseTypes
End Sub

To access a member of the enumeration, type the name of the variable, followed by a period, followed by the desired member of the enumeration. You can use that formula to initialize the variable. Here is an example:

Private Sub cmdEnumeration_Click()
    Dim HouseType As HouseTypes
    
    HouseType = HouseTypes.Condominiium
End Sub

After initializing the variable, you can use its values wherever necessary. Here is an example:

Private Sub cmdEnumeration_Click()
    Dim HouseType As HouseTypes
    
    HouseType = HouseTypes.Townhouse
    txtFilename = HouseType
End Sub

The Values of Members of an Enumeration

When you create a variable by listing the members in its body, each members holds a value of a natural number. By default, 0 is assigned to the first member. 1 is assigned to the second member, and so on. Consider the above code:

Private Sub cmdEnumeration_Click()
    Dim HouseType As HouseTypes
    
    HouseType = HouseTypes.Townhouse
    txtFilename = HouseType
End Sub

This produces 1 because Townhouse is the second member of the enumeration. If you don't want the default values for the members, you can specify the value of one or each member of the enumeration. Suppose you want the SingleFamily member in the above enumeration to have a value of 5. To do this, use the assignment operator "=" to give the desired value. The enumerator would be:

Enum HouseTypes
    SingleFamily = 5
    Townhouse
    Condominiium
    Unknown
End Enum

In this case, SingleFamily now has a value of 5, Townhouse now has a value of 6, and Condominium has a value of 7. If you want, you can also assign a value to more than one member of an enumeration. Here is an example:

Enum HouseTypes
    SingleFamily = 5
    Townhouse = 12
    Condominiium = 8
    Unknown
End Enum

Notice that you don't have to assign a value to each member, and you don't have to assign an incremental number to each member. If you omit to assign a value to a member, the member gets the value of the previous member + 1. In the above code, SingleFamily has a value of 5. Townhouse has a value of 12. Condominium has a value of 8 and Unknown has a value of 9.

Enumerations Visibility

By default, if you create an enumeration the way we have proceeded so far, it would be available only in the project it belongs to. To control an enumeration's accessibility, you can precede it with the Private or the Public keyword. Here is an example:

Private Enum HouseTypes
    SingleFamily = 5
    Townhouse = 12
    Condominiium = 8
    Unknown = 114
End Enum
 
 
 
     
 

Home Copyright © 2012-2013 FunctionX, Inc. Home