在Access宏中运行代码错误

时间:2015-01-23 18:44:42

标签: vba ms-access access-vba

我正在尝试在Access中创建一个运行VBA代码的宏。我尝试使用RunCode命令运行一个函数,该函数将运行我已经创建的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

1 个答案:

答案 0 :(得分:0)

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

  • 您是如何触发RunTotals宏的?使用嵌入式宏 使用RunMacro操作或在VBA中使用DoCmd.RunMacro?
  • 功能总计()放在哪里?它需要落后于表格 触发事件或在模块中。

最后,您可以避免使用单独的对象(RunTotals宏和嵌入式宏),如果在触发事件中,您键入属性表事件行“= Totals()”或者,您可以简单地在VBA中调用该函数简单的线条:总计。但是第二个子弹中解释的位置仍然适用。