querydefs 运行-时间错误 3265:在此集合中找不到项目

querydefs run-time error 3265: item not found in this collection

在 Access 2007 中,我正在尝试发送查询结果,但是,我一直收到错误 "Run-time error '3265': Item not found in this collection." 错误出现在行中:

Set qry = CurrentDb.QueryDefs(ReportQueryName)

我检查了字段上的拼写,并尝试乱用“工具”>“参考”以确保我拥有正确的库。

这是我当前的代码:

Private Sub Command202_Click()

Dim qry As DAO.QueryDef
Dim strSQL As String
Dim ReportQueryName As String

ReportQueryName = "ReportEmail"

Set qry = CurrentDb.QueryDefs(ReportQueryName)
strSQL = "SELECT [ID], [title] FROM Cases WHERE ID = " & Me.ID
qry.SQL = strSQL

DoCmd.SendObject acSendQuery, "ReportEmail", acFormatXLSX, "email@address.com", ..., , False

End Sub

您不能使用 QueryDefs 创建新查询 - 您必须改用 CreateQueryDef

Private Sub Command202_Click()

    Dim qry As DAO.QueryDef
    Dim strSQL As String
    Dim ReportQueryName As String

    ReportQueryName = "ReportEmail"

    strSQL = "SELECT [ID], [title] FROM Cases WHERE ID = " & Me.ID

    Set qry = CurrentDb.CreateQueryDef(ReportQueryName,strSQL)

    DoCmd.SendObject acSendQuery, "ReportEmail", acFormatXLSX, _
    "email@address.com", ..., , False

End Sub

有时您的新查询不会立即显示在 Access windows 中。

如果需要,可以使用:

Application.RefreshDatabaseWindow