[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Create Excel pivot table from within VB6 app (NOT VBA)

Hi guys,

I need to create  Excel pivot table from within my vb app
Here is a relevant piece of code:


.......
Dim pc As PivotCache

Set pc = oWBook.PivotCaches.Add(xlExternal)
With pc
    Set .Recordset = rstRecordset  ' <-Got error  1004 - Application-defined or object-defined error
    .CreatePivotTable Range("A3")
End With
.....


rstRecordset  at that moment is open and contains all requested data.
What am I doing wrong?
0
Michael_D
Asked:
Michael_D
  • 4
1 Solution
 
EDDYKTCommented:
0
 
Michael_DAuthor Commented:
EDDYKT,
Yes, I did try almost the same
But instead of ODBC I am using OLE DB  (data came from MS SQL Server 2000)
0
 
Michael_DAuthor Commented:
When i am using ODBC driver to connect to Server the code works.

Is this one of limitations of Excel? Or I have somehow indicate that I am using OLEDB?

 

0
 
Michael_DAuthor Commented:
OK I got it!

Here is the working code:
Dim pc As PivotCache
Dim pt As PivotTable
Set pc = oWBook.PivotCaches.Add(xlExternal)
With pc
    .Connection = "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=<MyDB>;Data Source=<MySQLServer>"
    .CommandType = xlCmdSql
    .CommandText = "SELECT MyRowField,MyColField,MyDataField FROM MyTable"
    Set pt = .CreatePivotTable(oWSheet.Range("A3"))
End With
   
pt.AddFields "MyRowField", "MyColField"
pt.PivotFields("MyDataField").Orientation = xlDataField
pt.ColumnGrand = False
pt.RowGrand = False


In my first attempts I forgot to put OLEDB in
                        |
                        V
.Connection = "OLEDB; .....

0
 
Michael_DAuthor Commented:
Anyway I don't like to ask for refund so I will give the points to EDDYKT
Thank you
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now