ms-access - querydefs 运行时错误 3265 : item not found in this collection

标签 ms-access vba dao

在 Access 2007 中,我尝试发送查询结果,但是,我不断收到错误“运行时错误‘3265’:在此集合中找不到项目。”线上出现错误:

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, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3a323e36331f3e3b3b2d3a2c2c713c3032" rel="noreferrer noopener nofollow">[email protected]</a>", ..., , 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, _
    "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8a828e8683af8e8b8b9d8a9c9cc18c8082" rel="noreferrer noopener nofollow">[email protected]</a>", ..., , False

End Sub

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

如果您愿意,可以使用:

Application.RefreshDatabaseWindow

关于ms-access - querydefs 运行时错误 3265 : item not found in this collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802047/

相关文章:

ms-access - Microsoft Access 是否具有全文搜索功能?

java - 读取带有感叹号的字符串包括句号

vba - 在 access 97 中查找完整路径的目录部分(减去文件名)

vba - 单击时出现错误 "Argument is not optional"

excel VBA - 通过按 <esc> 停止正在运行的脚本 - 没有错误消息

sql - 加快 Access 2016年查询

vba - 从 VBA 中的不同模块调用子例程

grails - 如何注入(inject)与 grails 中的任何域类无关的自定义服务?

java - 如何使用map来减少Spring MVC Web应用程序中的对象创建?

java - 从数据库中搜索java servlet