尝试使用DAO和Me.filter向TransferSpreadsheet添加过滤条件

时间:2017-12-08 16:30:15

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

报表上有一个按钮可将报表的基础查询导出到Excel。这个功能可以正常工作但我需要它来采用报告的标准。我有一个庞大的报告管理器,它将设置报告的标准,然后将其打开。

为了方便起见,我想将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 传递给一个在不同的sub中工作的变量,但是我的问题是我需要传递过滤器以正确格式化我假设的sql语句?另一个sub只是将它用作打开报告命令的[WhereCondition]。

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

strRptName:TotalSalesForYear

strRptSource:qryMainDashboard

FilterCondition:TxnDate&gt; =#11/1/2017#AND TxnDate&lt; =#11/30/2017#

qdf.SQL = "SELECT * INTO [" & tempTableName & "] FROM [" & strRptSource & "] where filtercondition"

当我将qdf.SQL = "SELECT * INTO [" & tempTableName & "] FROM [" & strRptSource & "] "更改为void SparseMatrix2Coo(Eigen::SparseMatrix<float> Matrix, std::vector<int>& rows, std::vector<int>& cols, std::vector<float>& coeffs) { rows.clear(); cols.clear(); coeffs.clear(); for (int k=0; k < Matrix.outerSize(); ++k) { for (Eigen::SparseMatrix<float>::InnerIterator it(Matrix,k); it; ++it) { rows.push_back(it.row()); cols.push_back(it.col()); coeffs.push_back(Matrix.coeff(it.row(), it.col())); } } assert(cols.size() == coeffs.size()); assert(rows.size() == cols.size()); }

时,一切正常

问题是当我放弃filtercondition时没有过滤器,显然。 我一直遇到的错误是&#34;运行时错误&#39; 3061&#39;:太少的参数。预计1。&#34;

有人有任何指示吗?

1 个答案:

答案 0 :(得分:0)

问题是你没有连接过滤条件。您的查询仅指出WHERE filtercondition,而不是WHERE TxnDate >= #11/1/2017# AND TxnDate <= #11/30/2017#

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