KP_SoCal
Board Regular
- Joined
- Nov 17, 2009
- Messages
- 116
Hi all, I'm running the following subroutine within an excel module to import a table from an access database into the worksheet named Summary starting from row A3.
The nice thing about this code is that it allows me to import starting from any row I designate. The one thing that I can NOT figure is how to import the FIELD NAMES of the table.
Does anyone have any ideas on how to modify the code below to bring in the field names with the raw data as opposed to just the row data?
The nice thing about this code is that it allows me to import starting from any row I designate. The one thing that I can NOT figure is how to import the FIELD NAMES of the table.
Does anyone have any ideas on how to modify the code below to bring in the field names with the raw data as opposed to just the row data?
Code:
Private Sub PullSummaryData()
Const strDb As String = "C:\db\AccessDatabase.accdb"
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Sheets("Summary").Select
Const strQry As String = "SELECT * FROM [AccessDataTable]"
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDb & ";"
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Open strQry
End With
Sheets("Summary").Range("A3").CopyFromRecordset rs
rs.Close
cn.Close
End Sub