How to import table from database in excel worksheet using excel VBA

BizBoy

Board Regular
Joined
Jul 26, 2012
Messages
118
Hi,

I have an access database file named as ‘Directory’.
In this file, there are three tables. I need to import table named as ‘Employee Directory’ in excel workbook by VBA.

Can anyone please help me in this. How do I do this.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi,

Found solution at below website. Thanks.

http://www.askeygeek.com/vba-code-to-import-access-tablequery-data-to-excel/

Code:
Sub Import_Access_Data()    
    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sQRY As String
    Dim strFilePath As String
    
    strFilePath = "\\a\a\a\A\A\A\03-Apr-2018\Directory.accdb"   'Replace the ‘DatabaseFolder’ and ‘myDB.accdb’ with your DB path and DB name
    
    Set cnn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & strFilePath & ";"
    
    sQRY = "SELECT * FROM [Employee Directory]" 'Replace ‘tblData’ with your Access DB Table name or Query name from which you want to download the data
    
    rs.CursorLocation = adUseClient
    rs.Open sQRY, cnn, adOpenStatic, adLockReadOnly
    
    Sheet1.Range("A1").CopyFromRecordset rs
    
    rs.Close
    
    Set rs = Nothing
    
    cnn.Close
    Set cnn = Nothing
End Sub
 
Last edited:
Upvote 0
Hi,

With this code. I am getting data. But this is without headers.
How do I get data with headers. Can anyone please help me in this.

Please ignore above query.

Solution is as below.

Dim iCols As Long
For iCols = 0 To rs.Fields.Count - 1
Sheet1.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
 
Last edited:
Upvote 0
Can you not use TransferSpreadsheet function? Looks to me like what you have is a lot of work for what you want to do. Other options are to import the sheet as a table, or simply link to it from Access. I rarely us ADODB for anything - it's not being promoted by M$ like it used to be.
 
Upvote 0

Forum statistics

Threads
1,221,614
Messages
6,160,838
Members
451,672
Latest member
LexCie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top