【问题标题】:Import to Excel from Access via VBA with lookup columns通过 VBA 使用查找列从 Access 导入 Excel
【发布时间】:2018-09-05 05:00:09
【问题描述】:

我正在将数据从 Access 表导入 Excel。我拥有的导入代码在拉入数据方面起作用,但是当访问表中的列是从另一个表中查找的值时,我对拉入的数据有疑问。例如,我将EmployeeID 存储在一个单独的表中,该表在我正在提取的表中进行查找。提取提取数据,但它只提取分配给员工表上员工的自动编号,而不是员工姓名。员工姓名存储在员工表的第三列中,当提取运行时我需要该值,而不是自动编号。但是,我不知道如何指定通过 VBA 在 SQL 中提取的列。有人可以帮忙吗?到目前为止,这是我所拥有的:

Sub getAccessData()

Dim DBFullName As String
Dim Connect As String, Source As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Integer
Dim lngLastColumn As Long
Dim lngLastRow As Long
Dim OXLSheet As Worksheet

Set OXLSheet = Worksheets("WorksheetName")

Worksheets("WorksheetName").Cells.Clear

'Datebase path info
DBFullName = "C:\Users\myname\Desktop\Database Backups\database.accdb"

'Open the connection for the database
Set Connection = New ADODB.Connection
Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
Connect = Connect & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Connect


'Create RecordSet
Set Recordset = New ADODB.Recordset
With Recordset

    'Data Filter
    Source = "SELECT EmployeeID FROM tblRetirements WHERE AllowEnteredInPayroll]Is Null AND ApplicationCancelled = 'No'"
    .Open Source:=Source, ActiveConnection:=Connection


    'Write field Names
    For Col = 0 To Recordset.Fields.Count - 1
        Worksheets("WorksheetName").Range("A5").Offset(0, Col).Value = Recordset.Fields(Col).Name
    Next

    'Write Recordset
    Worksheets("WorksheetName").Range("A5").Offset(1, 0).CopyFromRecordset Recordset
End With
ActiveSheet.Columns.AutoFit
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing



With OXLSheet
    lngLastColumn = .Cells(5, .Columns.Count).End(xlToLeft).Column
    lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    .ListObjects.Add(xlSrcRange, .Range(.Cells(5, 1), .Cells(lngLastRow, lngLastColumn)), , xlYes).Name = "Table1"

    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium16"
End With

End Sub

【问题讨论】:

  • 是的,但这并不是我真正想要的。我正在从 Access to Excel 中的表中提取数据,以便我可以使用数据透视表、图表和操作数据。提取工作正常,但当我的 Access 表中的列从 access 中的另一个表中查找时,我似乎无法获取数据。我需要一种方法来通过提取上的 SQL 更改绑定的列号。
  • 那么您需要创建一个连接查询并将结果导出到 Excel,这样可以 100% 工作
  • 您需要查看 SQL 连接,例如。 from join tblEmployees on tblRetirements.EmployeeID =tblEmployees.EmployeeID
  • @JosephD 不确定加入一列是什么意思,但在这里您有一个表是主表 = tblRetirements 和另一个表,您保留查找列的值。在这种情况下,您需要创建一个LEFT JOIN 查询,其中您的主表是左表,另一个是右表。创建一个查询并点击 SQL 并粘贴此查询语句并查看结果,然后您可以玩弄得到您想要的 select * from tblRetirements left join tblEmployees on tblRetirements.EmployeeID=tblEmployees.EmployeeID

标签: excel vba ms-access-2010


【解决方案1】:

您在查找列中看到的内容实际上是连接的产物,因此要获取名称而不是 ID,您需要定义 SQL 查询并导出其结果而不是表本身。要包含主表中的所有记录,您需要使用LEFT JOIN。如果您使用INNER JOIN,那么您将得到相同的结果,除非您的主表中有记录,其雇员表中的相关记录已被删除:

Sub getAccessData()

    Dim DBFullName As String
    Dim Connect As String, Source As String
    Dim Connection As ADODB.Connection
    Dim Recordset As ADODB.Recordset
    Dim Col As Integer
    Dim lngLastColumn As Long
    Dim lngLastRow As Long
    Dim OXLSheet As Worksheet

    Set OXLSheet = Worksheets("WorksheetName")

    Worksheets("WorksheetName").Cells.Clear

    'Datebase path info
    DBFullName = "C:\Users\myname\Desktop\Database Backups\database.accdb"

    'Open the connection for the database
    Set Connection = New ADODB.Connection
    Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
    Connect = Connect & "Data Source=" & DBFullName & ";"
    Connection.Open ConnectionString:=Connect


    'Create RecordSet
    Set Recordset = New ADODB.Recordset
    With Recordset

        'Data Filter
        Source = "SELECT tblEmployeeID.Name FROM tblRetirements " & _
        "LEFT JOIN tblEmployeeID on tblRetirements.EmployeeID = tblEmployeeID.Name " & _
        "WHERE [AllowEnteredInPayroll] Is Null AND ApplicationCancelled = 'No'"
        .Open Source:=Source, ActiveConnection:=Connection


        'Write field Names
        For Col = 0 To Recordset.Fields.Count - 1
            Worksheets("WorksheetName").Range("A5").Offset(0, Col).Value = Recordset.Fields(Col).Name
        Next

        'Write Recordset
        Worksheets("WorksheetName").Range("A5").Offset(1, 0).CopyFromRecordset Recordset
    End With
    ActiveSheet.Columns.AutoFit
    Set Recordset = Nothing
    Connection.Close
    Set Connection = Nothing



    With OXLSheet
        lngLastColumn = .Cells(5, .Columns.Count).End(xlToLeft).Column
        lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .ListObjects.Add(xlSrcRange, .Range(.Cells(5, 1), .Cells(lngLastRow, lngLastColumn)), , xlYes).Name = "Table1"

        ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium16"
    End With

End Sub

【讨论】:

    【解决方案2】:

    修改代码:

    Sub getAccessData()
    
    Dim DBFullName As String
    Dim Connect As String, Source As String
    Dim Connection As ADODB.Connection
    Dim Recordset As ADODB.Recordset
    Dim Col As Integer
    Dim lngLastColumn As Long
    Dim lngLastRow As Long
    Dim OXLSheet As Worksheet
    
    Set OXLSheet = Worksheets("WorksheetName")
    
    Worksheets("WorksheetName").Cells.Clear
    
    'Datebase path info
    DBFullName = "C:\Users\myname\Desktop\Database Backups\database.accdb"
    
    'Open the connection for the database
    Set Connection = New ADODB.Connection
    Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
    Connect = Connect & "Data Source=" & DBFullName & ";"
    Connection.Open ConnectionString:=Connect
    
    
    'Create RecordSet
    Set Recordset = New ADODB.Recordset
    With Recordset
    
        'Data Filter
        Source = "SELECT tblEmployeeID.Name FROM tblRetirements " & _
        "INNER JOIN tblEmployeeID on tblRetirements.EmployeeID = tblEmployeeID.Name " & _
        "WHERE [AllowEnteredInPayroll] Is Null AND ApplicationCancelled = 'No'"
        .Open Source:=Source, ActiveConnection:=Connection
    
    
        'Write field Names
        For Col = 0 To Recordset.Fields.Count - 1
            Worksheets("WorksheetName").Range("A5").Offset(0, Col).Value = Recordset.Fields(Col).Name
        Next
    
        'Write Recordset
        Worksheets("WorksheetName").Range("A5").Offset(1, 0).CopyFromRecordset Recordset
    End With
    ActiveSheet.Columns.AutoFit
    Set Recordset = Nothing
    Connection.Close
    Set Connection = Nothing
    
    
    
    With OXLSheet
        lngLastColumn = .Cells(5, .Columns.Count).End(xlToLeft).Column
        lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .ListObjects.Add(xlSrcRange, .Range(.Cells(5, 1), .Cells(lngLastRow, lngLastColumn)), , xlYes).Name = "Table1"
    
        ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium16"
    End With
    
    End Sub
    

    【讨论】:

    • 这是真的吗tblRetirements.EmployeeID = tblEmployeeID.Name
    • @Nathan_Sav 不能为真,表示查询将返回 0 条记录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多