Nicole
Nicole

Reputation: 19

Ways to pass arguments to a function in MS ACCES / VBA

I am currently struggling to understand someone else's code in MS Access VBA.

He has defined a function like this

Function transferTable(tableType As String) As Boolean

when the function is called, it looks like that

transferTable = True

Depending on the argument tableType there are several select case statements. e.g.

Select Case tableType
        Case "rc": range = "A" & row & ":AD" & row
        Case "66": range = "A" & row & ":M" & row
        Case "25": range = "A" & row & ":L" & row
        Case "67": range = "A" & row & ":L" & row
        Case "77": range = "A" & row & ":L" & row
        Case "47": range = "A" & row & ":L" & row
        Case "c2": range = "A" & row & ":S" & row
    End Select

I just don't understand where we pass a "value" to tableType. I can't find it in the VBA Code and none of the queries/tables contain a column with the name tableType.

Are there any other ways to pass an argument to a function in MS Access VBA? I am still a total newbe to programming...

Upvotes: 0

Views: 206

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71187

transferTable = True

That isn't an invocation of the function. This can only be within the function, in a part of the code that assigns the function's return value.

Here's a simple example; this function takes a Long parameter and returns True if the parameter is greater than 0, False otherwise:

Public Function MyFunction(ByVal someParameter As Long) As Boolean
    MyFunction = (someParameter > 0)
End Function

That's the only place you'll ever see a function's name on the left-hand side of an assignment operator.

Where that function is invoked, the code might look like this:

MyFunction 42

Or like this:

result = MyFunction(42)

Or like this:

If MyFunction(42) Then

And that's how arguments are passed to any Sub or Function procedure... there is no other way.

Consider this (well-known?) function usage:

MsgBox "message"

And:

result = MsgBox("message", vbYesNo)

Or:

If MsgBox("message", vbYesNo) = vbYes Then

Upvotes: 1

Related Questions