Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Creating Excel Pivot Table from Access Query

Creating Excel Pivot Table from Access Query

Creating Excel Pivot Table from Access Query


I have been trying to generate a pivot table in MS Excel from an Access query without success. I can get the data into excel however cannot create the Pivot, keep getting error:

Run-time error '5': Invalid procedure call or argument


Private Sub cmdPTable_Click()

    Dim ExpCheck As Integer
    ExpCheck = MsgBox("Are you sure you want to export the Status Report?", vbExclamation + vbYesNo + vbDefaultButton2, "Status Report")
    If ExpCheck = 7 Then
    Exit Sub
    End If

    Dim MyDatabase As DAO.Database
    Dim MyQueryDef As DAO.QueryDef
    Dim MyRecordset As DAO.Recordset

    Dim i As Integer

    Set MyDatabase = CurrentDb
    Set MyQueryDef = MyDatabase.QueryDefs("StatusReport")

    Set MyRecordset = MyQueryDef.OpenRecordset

    Dim xlApp As Object
    Set xlApp = CreateObject("Excel.Application")
    With xlApp
    .Visible = True
    .ActiveSheet.Range("A2").CopyFromRecordset MyRecordset
    .ActiveSheet.Range("A1:H1").Interior.ColorIndex = 37
    .ActiveSheet.Name = "Data"
    For i = 1 To MyRecordset.Fields.Count
        xlApp.ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
    Next i
    .ActiveSheet.Name = "Analysis"
    .ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Data!R1C1:R700C6").CreatePivotTable TableDestination:="Analysis!R3C1", tableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
    End With
End Sub 

Any suggestions where this is falling over?

It is the last line which is causing the error. The data exports into Excel and creates a new sheet called 'Analysis' but then fails to create the Pivot Table.

Many thanks

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close