运行 Access 宏中的代码错误

Run Code error in Access Macro

我正在尝试在 Access 中创建一个宏,它将 运行 VBA 编码。我正在尝试使用 运行Code 命令来 运行 一个函数,该函数将 运行 我已经创建的 VBA 函数,但由于某种原因它说找不到它,我真的很困惑为什么。我已经发布了宏的图片以及下面的代码。任何帮助将不胜感激。

宏:

http://imgur.com/uankmL0

代码:

Function Totals()

Dim db As Database

Set db = CurrentDb
foldername = CurrentProject.Path & "\GeneralTotals"
deleteTotals = "DELETE * FROM Totals"

totalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED], [TOTAL RECENTLY IMPORTED]) " & _
"SELECT A.cnt, B.cnt, C.cnt, D.cnt " & _
"FROM ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM VerifiedFormularies " & _
") AS A " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ImportMetricsIDs " & _
") as B " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ShouldImportMetricsIDsTable " & _
    "WHERE [IMPORTSTATUS]= 'Yes' " & _
") AS C " & _
", ( " & _
    "SELECT COUNT([LATEST]) as cnt " & _
    "FROM VerifiedFormularies " & _
    "WHERE [LATEST]= 'Yes' " & _
") AS D "


db.Execute deleteTotals
db.Execute totalVerified


DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel5, "Totals", foldername

MsgBox ("Totals have been exported to: " & foldername)

End Function

您的问题很可能取决于触发事件的位置和代码放置。

  • 您如何触发 RunTotals 宏?使用嵌入式宏 使用 RunMacro 操作或在 VBA 中使用 DoCmd.RunMacro?
  • Function Totals() 放在哪里?它需要在表格后面 触发事件或在模块中。

最后,如果在触发事件中输入 属性 sheet 事件行“=Totals()”,则可以避免使用单独的对象(RunTotals 宏和嵌入式宏)或者,您可以简单地使用简单的行调用 VBA 中的函数:Totals。但是第二个项目符号中解释的位置仍然适用。