Databases Reference
In-Depth Information
Access can tell that the argument
dbOpenForwardOnly
is the value for the second
parameter (
Type
) of the function. Such arguments are called
positional arguments
.
Many built-in VBA/DAO functions also allow
named arguments
. For example, the
OpenRecordset
function can be called as follows:
Set rs = CurrentDb.OpenRecordset(Name:="Objects", _
Type:=dbOpenForwardOnly)
Here, each argument has the form:
ParameterName:=Argument
There are three main advantages to named arguments:
•
Named arguments can improve readability and clarity.
•
Blank spaces (separated by commas) are required for missing optional arguments
when using a positional declaration, but not when using named arguments.
•
The order in which named arguments are listed is immaterial, which, of course, is
not the case for positional arguments. For instance, the previous function call
could be written:
•
Set rs = CurrentDb.OpenRecordset(Type:=dbOpenForwardOnly, _
Name:="Objects")
Named arguments can improve readability quite a bit, and they are highly recommended.
However, they can require considerably more space, so for the short examples in this
book, I usually will not use them.
11.3.3 ByRef Versus ByVal Parameters
Parameters come in two flavors:
ByRef
and
ByVal
. Many programmers do not have a
clear understanding of these concepts, but they are very important and not that difficult to
understand.
To explain the difference, I present the two procedures in Example 11-4.
ProcedureA
simply sets the value of the module-level variable
x
to 5, displays that value, calls the
procedure
AddOne
with the argument
x
, and then displays the value of
x
again.
Example 11-4. Testing the ByVal and ByRef keywords
Sub ProcedureA( )
x = 5 ' Set x to 5
MsgBox x ' Display x
Call AddOne(x) ' Call AddOne
MsgBox x ' Display x again
End Sub
Sub AddOne(ByRef i As Integer)
i = i + 1