Hello:
I am trying to create a pivot table in EXCEL using an ACCESS Database. However, I am getting an error which says -Runtime Error 1004 : [Microsoft][ODBC Microsoft ACCESS Driver] Could not find the file 'unknown'.
This is a VBA code straight from John Walkenbach's book. It wuns when I run the code from CD provided in the book. However, it does not run when I save the code and ACCESS Database on the c: drive.
Following is the code:
Sub CreatePivotTableFromDB()
Dim PTCache As PivotCache
Dim PT As PivotTable
' Delete PivotSheet if it exists
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)
' Path to database file
DBFile = ThisWorkbook.Path & "\budget.mdb"
' Connection String
ConString = "ODBC;DSN=MS Access Database;DBQ=" & DBFile
' QueryString
QueryString = "SELECT * FROM `" & ThisWorkbook.Path & _
"\BUDGET`.Budget 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
' Add fields
.PivotFields("DEPARTMENT"
.Orientation = xlRowField
.PivotFields("MONTH"
.Orientation = xlColumnField
.PivotFields("DIVISION"
.Orientation = xlPageField
.PivotFields("BUDGET"
.Orientation = xlDataField
.PivotFields("ACTUAL"
.Orientation = xlDataField
End With
End Sub
Can somebody help me?
Thanks!
I am trying to create a pivot table in EXCEL using an ACCESS Database. However, I am getting an error which says -Runtime Error 1004 : [Microsoft][ODBC Microsoft ACCESS Driver] Could not find the file 'unknown'.
This is a VBA code straight from John Walkenbach's book. It wuns when I run the code from CD provided in the book. However, it does not run when I save the code and ACCESS Database on the c: drive.
Following is the code:
Sub CreatePivotTableFromDB()
Dim PTCache As PivotCache
Dim PT As PivotTable
' Delete PivotSheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets("PivotSheet"
On Error GoTo 0
' Create a Pivot Cache
Set PTCache = ActiveWorkbook.PivotCaches.Add _
(SourceType:=xlExternal)
' Path to database file
DBFile = ThisWorkbook.Path & "\budget.mdb"
' Connection String
ConString = "ODBC;DSN=MS Access Database;DBQ=" & DBFile
' QueryString
QueryString = "SELECT * FROM `" & ThisWorkbook.Path & _
"\BUDGET`.Budget 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"
TableName:="BudgetPivot"
' Add fields
With PT
' Add fields
.PivotFields("DEPARTMENT"
.PivotFields("MONTH"
.PivotFields("DIVISION"
.PivotFields("BUDGET"
.PivotFields("ACTUAL"
End With
End Sub
Can somebody help me?
Thanks!