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)
In this tutorial:
- Visual Basic Fundamentals
- Visual Basic Development Environment
- Visual Basic Editor Window
- Relationship Between Access and Visual Basic
- Visual Basic Debugging Tools
- Working with the Watch Window
- Variables and Constants
- Variable and Constant Scope
- Declaring Constants and Variables
- Dim Statement
- Enum Statement
- Event Statement
- Private Statement
- Public Statement
- Static Statement
- Type Statement
- Collections, Objects, Properties, and Methods
- DAO Architecture
- ADO Architecture
- Referencing Collections, Objects, and Properties
- Use Exclamation Points and Periods
- Assigning an Object Variable-Set Statement
- Object Methods
- Manipulating Complex Data Types Using DAO
- Working with ADO Recordsets
- Functions and Subroutines
- Sub Statement
- Understanding Class Modules
- Property Let
- Property Set
- Controlling the Flow of Statements
- Do...Loop Statement
- For...Next Statement
- For Each...Next Statement
- If...Then...Else Statement
- RaiseEvent Statement
- Stop Statement
- With...End Statement
- Running Macro Actions and Menu Commands
- Executing an Access Command
- Trapping Errors
- Working with 64-Bit Access Visual Basic for Applications
- Using LongPtr Data Types
- Supporting Older Versions of Access
- Using LongLong Data Types