Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

Import to Excel from Access via VBA with lookup columns

I am working on importing data from an Access table into Excel. The import code that I have works in terms of pulling in data, but I have an issue with the data that is pulled in when the column in the access table is a looked up value from another table. For instance, I have EmployeeID stored in a separate table which is looked up in the table that I am extracting. The extract pulls the data, but it only pulls the autonumber that's assigned to the employee on the employee table rather than the employee name. The employee name is stored in the third column of the employee table and I need that value when the extract runs, not the autonumber. However, I don't know how to specify the column that extracts in SQL via VBA. Can someone please help? Here's what I have so far:

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:UsersmynameDesktopDatabase Backupsdatabase.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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you see in a lookup column is actually the product of a join so to get the names instead of the IDs you need to define a SQL query and export its result instead of the table itself. To include all of the records from you main table, you need to use the LEFT JOIN. If you use INNER JOIN then you will get the same result unless you have records in your main table whose related record in the employee table has been deleted:

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:UsersmynameDesktopDatabase Backupsdatabase.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...