How to pull all the field names from an Access into Excel using VBA

UnDePlusieurs

New Member
Joined
May 30, 2019
Messages
1
Hi,

My access table is Assets.mdb which has a table called Assets in it.
The Assets table contains fields InventoryNum, Quantity, DatePurchased etc

I have managed using VBA to make a connection with that table and pull every record from it in Excel.

But I am having trouble pulling the field names of each column in the table and putting them in r1c1, r1c2 etc has column headers.
When this is done, I will be able to convert all the data and headers into a table.

References are all set.

Please help,
Pierre

Code I have so far.

Sub ConnectAccess()
Dim MyConnection As ADODB.Connection
Dim MyRecordset As ADODB.Recordset
Dim MyQuery As String
Set MyConnection = New ADODB.Connection
Set MyRecordset = New ADODB.Recordset
MyConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=c:\Users\Pierre\Documents\HomeManage\Assets.mdb"
MyQuery = "Select all * from assets;"
'Open the connection
MyConnection.Open MyConnectionString
'Execute the query
Set MyRecordset = MyConnection.Execute(MyQuery)
'Copy the output of query in Feuil1

' Write field names
'------------------------------------------------------------------------
'This part here gives me problems
Dim Col As Integer

For Col = 0 To MyRecordset.Fields.Count - 1
Sheets("Feuil1").Range(“A1”).Offset(0, Col).Value = MyRecordset.Fields(Col).Name
Next
'-------------------------------------------------------------------------


Worksheets("Feuil1").Range("A2").CopyFromRecordset MyRecordset
'Closing the connection and setting the values of declared variables as Nothing or simply to NULL.
MyRecordset.Close
Set MyRecordset = Nothing
MyConnection.Close
Set MyConnection = Nothing
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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