【问题标题】:Trying to add a filter condition to TransferSpreadsheet using DAO and Me.filter尝试使用 DAO 和 Me.filter 向 TransferSpreadsheet 添加过滤条件
【发布时间】:2018-05-22 22:33:46
【问题描述】:

报表上有一个按钮,可以将报表的基础查询导出到excel。此功能可以正常工作,但我需要它来获取报告的标准。我有一个庞大的报告经理,他将为报告设定标准,然后将其打开。

为了方便起见,我想将me.filter 传递给在不同子中工作的变量,但这里我的问题是我需要传递过滤器以便为我假设的 sql 语句正确格式化?另一个子只是将它用作打开报告命令的 [WhereCondition]。

为了澄清,getreportsource() 部分是一个获取报告源的模块,它工作正常。 以下是变量的一些示例输出以及代码:

strRptName: TotalSalesForYear

strRptSource: qryMainDashboard

过滤条件:TxnDate >= #11/1/2017# AND TxnDate

Private Sub cmdExcel_Click()

Dim strRptName As String
Dim strRptSource As String
Dim vardate As String
Dim varExportPath As String
Dim FilterCondition As String
Dim oExcel

FilterCondition = Me.filter

' Get the Report Name
strRptName = Screen.ActiveReport.Name

' Get the RecordSource of the Report from a module
strRptSource = GetReportSource(strRptName)

'Present Date
vardate = Format$(Now(), "YYYY.MM.DD_HH-mm-ss")
'Path of export
varExportPath = "C:\Users\Public\Downloads\"

'Check for terminating backslash ExportLinkReportsOut filepath.
    If Right(varExportPath, 1) <> "\" Then
       varExportPath = varExportPath & "\"
    End If
    varExportPath = varExportPath & strRptName & ".xlsx"

' set dao and create temp table
Dim cdb As DAO.Database, qdf As DAO.QueryDef
Const tempTableName = "_tempTbl"
Set cdb = CurrentDb

'deletes temp table and handles error
On Error Resume Next
DoCmd.DeleteObject acTable, tempTableName
On Error GoTo 0

Set qdf = cdb.CreateQueryDef("")
qdf.SQL = "SELECT * INTO [" & tempTableName & "] FROM [" & strRptSource & "] where filtercondition"
qdf.Execute
Set qdf = Nothing
Set cdb = Nothing

' export spreadsheet with the temp table, the export path, and then open the spreadsheet
DoCmd.TransferSpreadsheet acExport, , tempTableName, varExportPath, True
Set oExcel = GetObject(varExportPath)
oExcel.Application.Visible = True
oExcel.Parent.Windows(1).Visible = True


End Sub

当我将qdf.SQL = "SELECT * INTO [" &amp; tempTableName &amp; "] FROM [" &amp; strRptSource &amp; "] where filtercondition" 更改为qdf.SQL = "SELECT * INTO [" &amp; tempTableName &amp; "] FROM [" &amp; strRptSource &amp; "] " 时,一切正常

问题是当我放弃过滤条件时没有过滤器,很明显。 我不断收到的错误是“运行时错误'3061':参数太少。预期为1。”

有人指点一下吗?

【问题讨论】:

    标签: sql ms-access vba ms-access-2007 dao


    【解决方案1】:

    问题在于您没有连接过滤条件。您的查询仅声明 WHERE filtercondition,而不是 WHERE TxnDate &gt;= #11/1/2017# AND TxnDate &lt;= #11/30/2017#

    qdf.SQL = "SELECT * INTO [" &amp; tempTableName &amp; "] FROM [" &amp; strRptSource &amp; "] where filtercondition" 更改为qdf.SQL = "SELECT * INTO [" &amp; tempTableName &amp; "] FROM [" &amp; strRptSource &amp; "] WHERE " &amp; filtercondition

    【讨论】:

    • 连接是我的死敌。我更改了您的代码以修复其中的连接... WHERE ' & filtercondition' 现在的问题是它仍然只会导出整个查询,而不是像没有过滤器一样在 qdf.sql 中设置的内容。知道为什么吗?
    • 这不是我分享的。这些引用是相关的。请明确您的报价,并准确复制我的建议
    • 是的,这对我来说是一个愚蠢的错误。感谢您的投入和时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多