MS-Access / Getting Started

Enum Statement

Use an Enum statement in a module Declarations section to assign long integer values to named members of an enumeration. You can use an enumeration name as a restricted Long data type.

Syntax

[Public | Private] Enum enumerationname
    <member> [= <long integer expression>]
    ...
End Enum

Notes

Enumerations are constant values that you cannot change when your code is running. Include the Public keyword to define an enumeration that is available to all procedures in all modules in your database. Include the Private keyword to declare an enumeration that is available only within the module where the declaration is made. Enumerations are public by default.

You must declare at least one member within an enumeration. If you do not provide a <long integer expression> assignment, Visual Basic adds 1 to the previous value or assigns 0 if the member is the first member of the enumeration. The <long integer expression> cannot include variables, user-defined functions, or Visual Basic built-in functions (such as CLng). You can include simple literals and other previously defined constants or enumerations.

Enumerations are most useful as a replacement for the Long data type in a Function or Sub statement. When you call the function or sub procedure in code, you can use one of the enumeration names in place of a variable, constant, or literal. If you select the Auto List Members option, Visual Basic displays the available names in a drop-down list as you type the sub or function call in your code.

Example

To declare a public enumeration for days of the week and use the enumeration in a procedure, enter the following:

Option Explicit
Public Enum DaysOfWeek
    Sunday = 1
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
End Enum

Public Function NextDate(lngDay As DaysOfWeek) As Date
' This function returns the next date
' that matches the day of week requested
Dim intThisDay As Integer, datDate As Date
    ' Get today
     datDate = Date
    ' Figure out today's day of week
    intThisDay = WeekDay(datDate)
    ' Calculate next day depending on
    ' whether date requested is higher or lower
    If intThisDay < lngDay Then
	NextDate = datDate + (lngDay - intThisDay)
    Else
	NextDate = datDate + (lngDay + 7) - intThisDay
    End If
End Function

You can test the function from the Immediate window by entering the following:

?NextDate(Monday)
[Previous] [Contents] [Next]