在 Excel 中看不到 Access 查询

Posted

技术标签:

【中文标题】在 Excel 中看不到 Access 查询【英文标题】:Can't see Access Queries in Excel 【发布时间】:2018-08-13 19:18:56 【问题描述】:

我无法将我的数据从 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 的新手,所以它可能看起来不太好。如果您能提供帮助,请提前感谢您!

【问题讨论】:

【参考方案1】:

在 MS Access 中,尝试将上面的选择查询转换为“制作表”查询。在 Access 中运行此查询将创建一个表,在 MS Access 中为该表命名,然后将创建的表直接导入 Excel。

【讨论】:

【参考方案2】:

由于您的查询依赖于运行时来自 Access 表单控件 [Forms]![Main Screen Performance]![Modified Start Date][Forms]![Main Screen Performance]![Modified End Date] 的参数值,因此无法将其结果集导入 Excel,并且可能在选择表对话框中排除以导入 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

【讨论】:

以上是关于在 Excel 中看不到 Access 查询的主要内容,如果未能解决你的问题,请参考以下文章

IE 在响应中看不到 CORS 标头

在 python 中看不到数据库查询输出

在android位置权限中看不到总是允许

为啥我的模型在关系子查询中看不到 spatie 权限特征方法?

Access 数据库引擎找不到对象 - 对象是 Excel 命名范围

在 Grafana Graph 中看不到 InfluxDB 的时间和数据