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
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