MS-Access / Getting Started

Argument Data Types

When you specify arguments for a procedure, they always default to Variant, which is the default variable type in VBA. You can also declare your parameters as other data types.

The advantage of declaring with data types is that they introduce a discipline to your code in terms of what information the procedure is looking for. If you do not specify a type but use the default Variant, your procedure will accept anything, be it a number or a string. This can have unfortunate consequences within that procedure if you are expecting a string and a number gets passed across, or vice versa. If you specify the parameter as a string, an error will occur if you do not pass a string:

Function Myfunction(Target as String)

This can also be useful if you are writing a custom function for use in a SQL query. When users enter your function into their query, they must give the parameters according to the data type specified. If a string is specified, they must put the value in quotes or they can request the user input a value by using [Input Value] as a parameter in the function.

Optional Arguments

You can make specific arguments optional by using the Optional keyword:

Function Myfunction (Target as String, Optional Flag as Integer)

In this example, the user must specify the parameter Target as a string, but the parameter Flag will appear with square brackets around it and need not be specified. All optional parameters must come after the required ones.

[Previous] [Contents] [Next]