Used at module level to declare references to external procedures in a dynamic-link library (DLL).
[Public | Private ] Declare Sub name Lib "libname" [Alias"aliasname" ][([arglist])]
[Public | Private ] Declare Function name Lib "libname" [Alias"aliasname" ] [([arglist])][As type]
The Declare statement syntax has these parts:
Part | Description |
Public | Used to declare procedures that are available to all other procedures in all modules. |
Private | Used to declare procedures that are available only within the module where the declaration is made. |
Sub | Indicates that the procedure doesnt return a value. |
Function | Indicates that the procedure returns a value that can be used in an expression. |
name | Any valid procedure name. |
Lib | Indicates that a DLL or code resource contains the procedure being declared. The Lib clause is required for all declarations. |
libname | Name of the DLL or code resource that contains the declared procedure. |
Alias | Indicates that the procedure being called has another name in the DLL. This is useful when the external procedure name is the same as a keyword. You can also use Alias when a DLL procedure has the same name as a public variable or constant or any other procedure in the same scope. Alias is also useful if any characters in the DLL procedure name arent allowed by the DLL naming convention. |
aliasname | Name of the procedure in the DLL or code resource. |
If the first character is not a #, aliasname is the name of the procedures entry point in the DLL. If # is the first character, all characters that follow must indicate the ordinal number of the procedures entry point. | |
The resourcetype is any valid four-character constant. If omitted, the default resourcetype is CODE. The resourcename is the procedure name in the code resource. If resourcename is omitted, it is assumed to be the same as name. |
Part | Description |
arglist | List of variables representing arguments that are passed to the procedure when it is called. |
type | Data type of the value returned by a Function procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (variable length only), Object, Variant, a user-defined type, or an object type. |
The arglist argument has the following syntax and parts:
[Optional][ByVal | ByRef][ParamArray] varname[( )][As type]
Part | Description |
Optional | Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. All Optional arguments must be Variant. Optional cant be used for any argument if ParamArray is used. |
ByVal | Indicates that the argument is passed by value. |
ByRef | Indicates that the argument is passed by reference. |
ParamArray | Used only as the last argument in arglist to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. May not be used with ByVal, ByRef, or Optional. |
varname | Name of the variable representing the argument being passed to the procedure; follows standard variable naming conventions. |
type | Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (variable length only), Object, Variant, a user-defined type, or an object type. |
For Function procedures, the data type of the procedure determines the data type it returns. You can use an As clause following the arglist to specify the return type of the function. Within arglist you can use an As clause to specify the data type of any of the arguments passed to the procedure. In addition to specifying any of the standard data types, you can specify As Any in the arglist to inhibit type checking and allow any data type to be passed to the procedure.
Empty parentheses indicate that the Sub or Function procedure has no arguments and that Visual Basic should ensure that none are passed. In the following example, First takes no arguments. If you use arguments in a call to First, an error occurs:
Declare Sub First Lib "MyLib" ()
If you include an argument list, the number and type of arguments are checked each time the procedure is called. In the following example, First takes one Long argument:
Declare Sub First Lib "MyLib" (X As Long)
Note You cant have fixed-length strings in the argument list of a Declare statement only variable-length strings can be passed to procedures. Fixed-length strings can appear as procedure arguments, but they are converted to variable-length strings before being passed.
Note A constant, vbNullString, is provided by Visual Basic for use when calling external procedures, where the external procedure requires a string whose value is zero. This is not the same thing as a zero-length ("") string.
In the 32-bit version of Microsoft Windows, dynamic-link library (DLL) procedure names are case-sensitive; those in the 16-bit version of Microsoft Windows (3.1 and earlier) are not.
Call Statement, Function Statement, LastDLLError Property, Sub Statement.
In Microsoft Access, Declare statements are public by default. In a standard module, a public Declare statement is available to both the current database and any referencing databases. You can preface a Declare statement with the Private keyword to ensure that it is not available outside of the current database. To use a Declare statement in a form module or report module, you must precede the statement with the Private keyword.
In Microsoft Access, you can use the Declare statement at the module level of a standard module to declare a reference to an external procedure in a dynamic-link library (DLL). A Declare statement is public by default. In order to include a Declare statement in a form or report module, precede it with the Private keyword.
The following example references a procedure in a DLL, then calls that procedure from Visual Basic.
' In standard module.Sub MessageBeep Lib "User32" (ByVal intN As Integer) ' In form or report module.Declare Sub MessageBeep Lib "User32" (ByVal intN As Integer) ' Once you have referenced procedure in DLL with Declare ' statement, you can call that procedure normally from code.SystemBeep() Dim intB As Integer, intI As Integer Dim sngStart, sngPause sngPause = 2 intB = InputBox("How many beeps would you like to hear?") For intI = 1 to intB ' Call system function. MessageBeep(intB) ' Get start time. sngStart = Timer Do While Timer < sngStart + sngPause ' Pause for two seconds. Loop Next intISub