从Excel错误2517调用Access VBA函数

时间:2014-07-15 21:53:23

标签: vba

我搜索了一个解决方案的答案,并没有看到一个符合我试图做的事情,所以我发帖。

我有一个补偿电子表格,可以调用和访问数据库功能。这是excel代码:

Option Explicit

    Public appAccess As Access.Application

    Private Sub AutoComp(WritingLevel)
        If appAccess Is Nothing Then
            Set appAccess = New Access.Application
            With appAccess
                .OpenCurrentDatabase "C:\MyPath\Compensation.accdb"
                .Visible = False
                Debug.Print .Run("AutoComm", WritingLevel)
            End With
        End If
    End Sub

调试语句生成运行时错误2517无法找到程序' AutoComm。'这是数据库中的AutoComm:

Public Function AutoComm(WritingRepContract As String) As Single
    If ValidRepTitle(WritingRepContract) Then
        AutoComp = DLookup("[1st_Year]", "tblAuto_Comm", "[Title] ='" & WritingRepContract & "'")
    End If
End Function

即使我只有AutoComm弹出一个msgbox,它也永远不会那么远。它的行为似乎无法找到访问代码。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这对我有用

Excel中:

Option Explicit

Public appAccess As Access.Application

Private Sub AutoComp()

    If appAccess Is Nothing Then
        Set appAccess = New Access.Application
        With appAccess
            .Visible = False
            .OpenCurrentDatabase "C:\_Stuff\test.accdb"
        End With
    End If

    With appAccess
        Debug.Print "Result:" & .Run("AutoComm", "TestInput")
    End With

End Sub

访问(在标准模块中):

Option Compare Database
Option Explicit

Public Function AutoComm(WritingRepContract As String) As String
    AutoComm = "WritingRepContract = " & WritingRepContract
End Function