Any help would greatly be appreciated.
I am trying to understand ADO connections and finding very little info and help. I am setting up Excel Spreadsheet and need to import some information from Access Database VBA to manipulate data.
I found this piece of code from a site but I now get the following error:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Added a reference to the Microsoft ActiveX Data Object x.x library.
I am trying to understand ADO connections and finding very little info and help. I am setting up Excel Spreadsheet and need to import some information from Access Database VBA to manipulate data.
I found this piece of code from a site but I now get the following error:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Added a reference to the Microsoft ActiveX Data Object x.x library.
Code:
Sub CreatePivotTableFromDB()
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim DBFile As Variant
On Error Resume Next
Application.DisplayAlerts = False
Sheets("PivotSheet").Delete
On Error GoTo 0
'Create a Pivot Cache
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
'Connect to database and do query
DBFile = ThisWorkbook.Path & "\budget.mdb"
ConString = "ODBC;DSN=MS Acess Database;DBQ=" & DBFile
QueryString = "SELECT * FROM BUDGET"
With PTCache
.Connection = ConString
.CommandText = QueryString
End With
'add new worksheet
Worksheets.Add
ActiveSheet.Name = "PivotSheet"
'create pivot table
Set PT = PTCache.CreatePivotTable(TableDestination:=Sheets("PivotSheet").Range("A1"), TableName:="BudgetPivot")
'add fields
With PT
.PivotFields("DEPARTMENT").Orientation = xlRowField
.PivotFields("MONTH").Orientation = xlColumnField
.PivotFields("DIVISION").Orientation = xlPageField
.PivotFields("BUDGET").Orientation = xlDataField
.PivotFields("ACTUAL").Orientation = xlDataField
End With
End Sub