要将显示的用户数据从表单导出到excel,我创建一个临时表并导出该表。但是,通常这是可行的,如果我需要查询两个不同的表并将它们一起导出,它将不会输出任何东西,仅输出表的结构。
除了直接从表单记录源获取,手动执行代码中的查询以及使用标准输出外,我还尝试过手动查询表。
所有其他形式的代码都这样列出
sheetName = [Forms]![frmOutputPickering]![TabCtl3].Pages(page).Name
lenSQL = InStr(1, selectedPageName(page).Form.RecordSource, "FROM") - 2
strSQL = Left(selectedPageName(page).Form.RecordSource, lenSQL) & "INTO [" & sheetName & "] " _
& Mid(selectedPageName(page).Form.RecordSource, lenSQL)
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
包含两部分的代码如下:
sheetName = [Forms]![frmOutputPickering]![TabCtl3].Pages(page).Name
lenSQL = InStr(1, selectedPageName(page).Form.RecordSource, "FROM") - 2
strSQL = Left(selectedPageName(page).Form.RecordSource, lenSQL) & " INTO [" & sheetName & "] " & Mid(selectedPageName(page).Form.RecordSource, _
lenSQL, InStr(1, selectedPageName(page).Form.RecordSource, "UNION") - lenSQL - 2) & ");"
strSQL = "SELECT qryOUTAGE_NEW_WOs_PA.* INTO [" & sheetName & "] FROM [qryOUTAGE_NEW_WOs_PA];"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
lenSQL = InStr(lenSQL, selectedPageName(page).Form.RecordSource, "UNION") + 5
strSQL = "INSERT INTO [" & sheetName & "] SELECT [qryOUTAGE_NEW_WOs_PB].*;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
它应该将两个查询都导出到临时表,但是只输出表结构。
编辑:根据要求提供表单输出的原始记录源
SELECT tbl_OMSPA_v_SCOPED_WORK.WORK_ORDER_NBR AS [WO #], tbl_OMSPA_v_SCOPED_WORK.WO_REQ_NUMBER AS [WR #], tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE AS System, tbl_OMSPA_v_SCOPED_WORK.ADD_DATE AS [Add Date], tbl_OMSPA_v_SCOPED_WORK.SW_TITLE AS Description, tbl_OMSPA_v_SCOPED_WORK.SW_UNIT AS Unit, [tbl_OMSPA_v_SCOPED_WORK].[SW_UNIT]+"-"+[tbl_OMSPA_v_SCOPED_WORK].[SW_SYSTEM_CODE]+"-"+[tbl_OMSPA_v_SCOPED_WORK].[SW_EQUIPMENT_NUMBER] AS Equipment_Tag, tbl_OMSPA_v_SCOPED_WORK.SW_CRITICAL_EQUIPMENT AS Criticality, tbl_OMSPA_v_SCOPED_WORK.SCOPE_STATUS AS [Scope Status], tbl_OMSPA_v_SCOPED_WORK.SW_SHUTDOWN_NBR AS [Shutdown Number], tbl_OMSPA_v_SCOPED_WORK.I2P_Score AS [I2P Score]
FROM tbl_OMSPA_v_SCOPED_WORK
WHERE (((tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42100' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42110' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42111' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42120' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42121' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42122' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42123' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42130' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42140' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43100' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43110' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43120' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43130' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43140' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43150' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43200' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43210' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43220' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43230' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='43240' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45100' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45110' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45200' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45210' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45220' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45230' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45300' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='45310' OR (tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)='42000') AND ((tbl_OMSPA_v_SCOPED_WORK.ADD_DATE)>=[Forms]![frmOutputPickering]![TextStartDate] And (tbl_OMSPA_v_SCOPED_WORK.ADD_DATE)<=([Forms]![frmOutputPickering]![TextEndDate]+1))) UNION SELECT tbl_OMSPB_v_SCOPED_WORK.WORK_ORDER_NBR AS [WO #], tbl_OMSPB_v_SCOPED_WORK.WO_REQ_NUMBER AS [WR #], tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE AS System, tbl_OMSPB_v_SCOPED_WORK.ADD_DATE AS [Add Date], tbl_OMSPB_v_SCOPED_WORK.SW_TITLE AS Description, tbl_OMSPB_v_SCOPED_WORK.SW_UNIT AS Unit, [tbl_OMSPB_v_SCOPED_WORK].[SW_UNIT]+"-"+[tbl_OMSPB_v_SCOPED_WORK].[SW_SYSTEM_CODE]+"-"+[tbl_OMSPB_v_SCOPED_WORK].[SW_EQUIPMENT_NUMBER] AS Equipment_Tag, tbl_OMSPB_v_SCOPED_WORK.SW_CRITICAL_EQUIPMENT AS Criticality, tbl_OMSPB_v_SCOPED_WORK.SCOPE_STATUS AS [Scope Status], tbl_OMSPB_v_SCOPED_WORK.SW_SHUTDOWN_NBR AS [Shutdown Number], tbl_OMSPB_v_SCOPED_WORK.I2P_Score AS [I2P Score]
FROM tbl_OMSPB_v_SCOPED_WORK
WHERE (((tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42100' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42110' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42111' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42120' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42121' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42122' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42123' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42130' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42140' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43100' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43110' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43120' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43130' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43140' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43150' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43200' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43210' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43220' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43230' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='43240' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45100' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45110' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45200' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45210' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45220' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45230' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45300' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='45310' OR (tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)='42000') AND ((tbl_OMSPB_v_SCOPED_WORK.ADD_DATE)>=[Forms]![frmOutputPickering]![TextStartDate] And (tbl_OMSPB_v_SCOPED_WORK.ADD_DATE)<=([Forms]![frmOutputPickering]![TextEndDate]+1)));
编辑2: 代码调用的查询
qryOUTAGE_NEW_WOs_PA:
SELECT tbl_OMSPA_v_SCOPED_WORK.WORK_ORDER_NBR AS [WO #], tbl_OMSPA_v_SCOPED_WORK.WO_REQ_NUMBER AS [WR #], tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE AS System, tbl_OMSPA_v_SCOPED_WORK.ADD_DATE AS [Add Date], tbl_OMSPA_v_SCOPED_WORK.SW_TITLE AS Description, tbl_OMSPA_v_SCOPED_WORK.SW_UNIT AS Unit, [tbl_OMSPA_v_SCOPED_WORK].[SW_UNIT]+"-"+[tbl_OMSPA_v_SCOPED_WORK].[SW_SYSTEM_CODE]+"-"+[tbl_OMSPA_v_SCOPED_WORK].[SW_EQUIPMENT_NUMBER] AS Equipment_Tag, tbl_OMSPA_v_SCOPED_WORK.SW_CRITICAL_EQUIPMENT AS Criticality, tbl_OMSPA_v_SCOPED_WORK.SCOPE_STATUS AS [Scope Status], tbl_OMSPA_v_SCOPED_WORK.SW_SHUTDOWN_NBR AS [Shutdown Number], tbl_OMSPA_v_SCOPED_WORK.I2P_Score AS [I2P Score]
FROM tbl_OMSPA_v_SCOPED_WORK
WHERE (((tbl_OMSPA_v_SCOPED_WORK.SW_SYSTEM_CODE)=[Forms]![frmOutputPickering]![ListSelectedSystem]) AND ((tbl_OMSPA_v_SCOPED_WORK.ADD_DATE)>=[Forms]![frmOutputPickering]![TextStartDate] And (tbl_OMSPA_v_SCOPED_WORK.ADD_DATE)<=([Forms]![frmOutputPickering]![TextEndDate]+1)));
qryOUTAGE_NEW_WOs_PB:
SELECT tbl_OMSPB_v_SCOPED_WORK.WORK_ORDER_NBR AS [WO #], tbl_OMSPB_v_SCOPED_WORK.WO_REQ_NUMBER AS [WR #], tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE AS System, tbl_OMSPB_v_SCOPED_WORK.ADD_DATE AS [Add Date], tbl_OMSPB_v_SCOPED_WORK.SW_TITLE AS Description, tbl_OMSPB_v_SCOPED_WORK.SW_UNIT AS Unit, [tbl_OMSPB_v_SCOPED_WORK].[SW_UNIT]+"-"+[tbl_OMSPB_v_SCOPED_WORK].[SW_SYSTEM_CODE]+"-"+[tbl_OMSPB_v_SCOPED_WORK].[SW_EQUIPMENT_NUMBER] AS Equipment_Tag, tbl_OMSPB_v_SCOPED_WORK.SW_CRITICAL_EQUIPMENT AS Criticality, tbl_OMSPB_v_SCOPED_WORK.SCOPE_STATUS AS [Scope Status], tbl_OMSPB_v_SCOPED_WORK.SW_SHUTDOWN_NBR AS [Shutdown Number], tbl_OMSPB_v_SCOPED_WORK.I2P_Score AS [I2P Score]
FROM tbl_OMSPB_v_SCOPED_WORK
WHERE (((tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)=[Forms]![frmOutputPickering]![ListSelectedSystem]) AND ((tbl_OMSPB_v_SCOPED_WORK.ADD_DATE)>=[Forms]![frmOutputPickering]![TextStartDate] And (tbl_OMSPB_v_SCOPED_WORK.ADD_DATE)<=([Forms]![frmOutputPickering]![TextEndDate]+1)));
答案 0 :(得分:0)
本质上,您试图基于参数化记录集选择查询来运行操作查询(make-table)。但是,使用DoCmd.RunSQL
运行VBA SQL查询时,永远不会评估您的参数。结果,您的WHERE
子句:
WHERE (((tbl_OMSPB_v_SCOPED_WORK.SW_SYSTEM_CODE)=[Forms]![frmOutputPickering]![ListSelectedSystem])
接收一个NULL
参数值,并且不求值为TRUE
,因此不会返回任何记录。
您可以实施多种解决方案来评估参数化查询中的参数。
访问“ DoCmd.OpenQuery
将生成表查询另存为自己的存储查询,然后调用该操作。注意:无需关闭操作查询:
DoCmd.SetWarnings False
DoCmd.OpenQuery "myMakeTableQuery"
DoCmd.SetWarnings True
此GUI运行方法将跨打开的窗体定位控件[Forms]![frmOutputPickering]![ListSelectedSystem]
并相应地绑定其值。如果未打开表单,查询将不返回任何记录。
访问“ QueryDefs
将生成表查询保存在其自己存储的查询中,并在代码中保存,初始化querydef并按字面值评估参数。
Dim qdef As QueryDef, prm As Parameter
' INITIALIZE QUERYDEF
Set qdef = CurrentDb.QueryDefs("myMakeTableQuery")
' EVALUATE EACH FORM PARAMETER
For Each prm In qdef.Parameters
prm.value = Eval(prm.name)
Next prm
' RUN ACTION
qdef.Execute dbFailOnError
Excel的CopyFromRecordset
更好的是,绕过临时表的任何需要,并使用querydefs和记录集使用Excel应用程序对象输出数据以打开工作簿。下面显示了如何导出一个参数化查询。
Public Sub ExportQueryToExcel()
On Error GoTo ErrHandle
Dim qdef As QueryDef, rs As Recordset, prm As Parameter
Dim xlApp As Object, xlWb As Object
Dim i As Long
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
Set qdef = CurrentDb.QueryDefs("mySelectQuery")
For Each prm In qdef.Parameters
prm.value = Eval(prm.name)
Next prm
Set rs = qdef.OpenRecordset()
xlWb.Sheets(1).Range("A2").CopyFromRecordset rs ' DATA ROWS
For i = 1 To rs.Fields.Count
xlWb.Sheets(1).Cells(1, i) = rs.Fields(i - 1).name ' HEADER ROW
Next i
xlApp.Visible = True
ExitHandle:
rs.Close: qdef.Close
Set rs = Nothing: Set qdef = Nothing
Set xlWb = Nothing: Set xlApp = Nothing
Exit Sub
ErrHandle:
MsgBox Err.Number & ": " & Err.Description, vbCritical, "RUNTIME ERROR"
Resume ExitHandle
End Sub