首页 > 解决方案 > 在 MS ACCES / VBA 中将参数传递给函数的方法

问题描述

我目前正在努力理解 MS Access VBA 中其他人的代码。

他定义了一个这样的函数

Function transferTable(tableType As String) As Boolean

当函数被调用时,它看起来像这样

transferTable = True

根据参数 tableType 有几个选择案例语句。例如

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

我只是不明白我们在哪里将“值”传递给 tableType。我在 VBA 代码中找不到它,并且没有一个查询/表包含名为 tableType 的列。

有没有其他方法可以将参数传递给 MS Access VBA 中的函数?我仍然是编程的新手......

标签: vbafunctionms-access

解决方案


transferTable = True

这不是函数的调用。这只能函数内,在分配函数返回值的部分代码中。

这是一个简单的例子;这个函数接受一个Long参数,True如果参数大于0则返回,False否则:

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

这是您在赋值运算符左侧看到函数名称的唯一地方。

在调用该函数的地方,代码可能如下所示:

MyFunction 42

或者像这样:

result = MyFunction(42)

或者像这样:

If MyFunction(42) Then

这就是将参数传递给任何SubFunction过程的方式......没有其他方法。

考虑这个(众所周知的?)函数用法:

MsgBox "message"

和:

result = MsgBox("message", vbYesNo)

或者:

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

推荐阅读