由于无法在列表中找到查询,因此无法将Access中的数据导入Excel。
SELECT "Assembly" AS Type, [Assembly Data].[Assembly Date] AS Dates,
[Serial Numbers].[Assembly Serial Numbers Check] AS [Serial Number],
[Assembly Data].[Work Order #] AS [Work Order #],
[Assembly Data].[Part #] AS [Part #],
[Assembly Data].[Assembly Line] AS Line,
[Assembly Data].[Assembly Shift] AS Shift,
[Assembly Data].[Assembly Notes] AS Notes, "" AS [Test Stand],
"" AS [Pass/Fail], "" AS [Type of Downtime], "" AS [Time Lost]
FROM [Assembly Data]
INNER JOIN [Serial Numbers]
ON [Assembly Data].[ID Assembly] = [Serial Numbers].[ID Assembly Data]
WHERE ((([Assembly Data].[Assembly Date])
BETWEEN [Forms]![Main Screen Performance]![Modified Start Date] AND
[Forms]![Main Screen Performance]![Modified End Date]));
请让我知道该SQL需要修复的内容,以便可以查看/链接数据库。另外,我还是SQL的新手,所以它看起来可能不太好。如果可以,请先谢谢您!
答案 0 :(得分:0)
在MS Access中,尝试将上面的选择查询转换为“ make table”查询。在Access中运行此查询,这将创建一个表,在MS Access中为该表命名,然后将创建的表直接导入Excel。
答案 1 :(得分:0)
由于您的查询在运行时依赖于Access表单控件[Forms]![Main Screen Performance]![Modified Start Date]
和[Forms]![Main Screen Performance]![Modified End Date]
的参数值,因此其结果集无法导入到Excel中,并且有可能被排除在 Select Table 中对话框以导入Access表或查询。
要解决此问题,请考虑使用命名参数(不限于Access表单控件)创建一个非常相似的新查询。然后使用VBA之类的应用程序代码,为记录集构建的参数分配值,这些参数将复制到Excel电子表格中:
SQL (使用表别名和NULL)
PARAMETERS start_date_param Datetime, end_date_param Datetime;
SELECT 'Assembly'AS Type, a.[Assembly Date] AS Dates,
[Serial Numbers].[Assembly Serial Numbers Check] AS [Serial Number],
a.[Work Order #] AS [Work Order #],
a.[Part #] AS [Part #],
a.[Assembly Line] AS Line,
a.[Assembly Shift] AS Shift,
a.[Assembly Notes] AS Notes, NULL AS [Test Stand],
NULL AS [Pass/Fail], NULL AS [Type of Downtime], NULL AS [Time Lost]
FROM [Assembly Data] a
INNER JOIN [Serial Numbers] s
ON a.[ID Assembly] = s.[ID Assembly Data]
WHERE (((a.[Assembly Date])
BETWEEN start_date_param AND end_date_param;
VBA (使用后期绑定)
Sub ParamQueryImport()
On Error GoTo ErrHandle
Dim conn As Object, db As Object, qdef As Object, rs As Object
Dim i As Integer
Set conn = CreateObject("DAO.DBEngine.120")
Set db = conn.OpenDatabase("C:Path\To\Database.accdb")
Set qdef = db.QueryDefs("mySavedQuery")
qdef!start_date_param = "#01/01/2018#"
qdef!end_date_param = "#08/13/2018#"
Set rs = qdef.OpenRecordset()
With Worksheets("MY SHEET NAME")
' COLUMN HEADERS
For i = 1 To rs.Fields.Count
.Cells(1, i) = rs.Fields(i - 1).Name
Next i
' DATA ROWS
.Range("A2").CopyFromRecordset rs
End With
Exit Sub
ErrHandle:
Msgbox Err.Number & " - " & Err.Description, vbCritial, "RUNTIME ERROR"
Set rs = Nothing: Set qdef = Nothing
Set db = Nothing: Set conn = Nothing
Resume ExitHandle
ExitHandle:
Exit Sub
End Sub