【发布时间】: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 [" & tempTableName & "] FROM [" & strRptSource & "] where filtercondition" 更改为qdf.SQL = "SELECT * INTO [" & tempTableName & "] FROM [" & strRptSource & "] " 时,一切正常
问题是当我放弃过滤条件时没有过滤器,很明显。 我不断收到的错误是“运行时错误'3061':参数太少。预期为1。”
有人指点一下吗?
【问题讨论】:
标签: sql ms-access vba ms-access-2007 dao