Solved

Creating an Excel Piviot Table with a SQL Parameter Query

Posted on 2001-07-05
4
178 Views
Last Modified: 2007-12-19

Hello!

I am trying to create an excel pivot table which gets launched inside a VB application.  I think there is two roads I can go by:

1. I can build an SQL statement in VB with some date fields that the user can select, then pass that sql to the excel pivot table.  OR

2. I can build a parameter sql statement inside the Excel pivot table and pass the parameters from the VB app.  

Either way would work fine for me, but I can't get either of these options to work.  I need help, my deadline is approaching.  Anyone have any ideas.  If so can you SPELL them out, I clueless on this one.

Thanks!
0
Comment
Question by:JayQue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6256835
1) sounds to me to be the easiest. Where is the source data coming from? Is it an excel sheet or some external data source such as SQL server or MS access?
0
 

Author Comment

by:JayQue
ID: 6256880

An sql statement would be built from VB, it would be hardcoded inside the app.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6256940
Ok, here is a starter, this uses a SQL server table as the source.

    With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
        .Connection = Array(Array( _
        "ODBC;DRIVER=SQL Server;SERVER=<MYSERVERNAME>;APP=<MYAPPLICATION>;WSID=<MYWSID>;DATABASE=<MYDATABASE>;QueryLogFile=Yes;QueryLogTime=Yes;StatsLo" _
        ), Array("g_On=2;Trusted_Connection=Yes"))
        .CommandType = xlCmdSql
        .CommandText = Array( _
        "SELECT EmployeeAbsences.EmployeeNumber, EmployeeAbsences.Year, EmployeeAbsences.Code" & Chr(13) & "" & Chr(10) & "FROM <MYDATABASE>" _
        , ".dbo.EmployeeAbsences EmployeeAbsences")
        .CreatePivotTable TableDestination:=Range("A3"), TableName:= _
        "MyPivotTable"
    End With
    ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:=Array( _
        "EmployeeNumber", "Year")
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Code").Orientation = _
        xlDataField

Replace the bits in <...> with something that fits your scenario. Then obviously amend the sql statement and the fields and you should have the beginnings of the pivot table.
0
 

Author Comment

by:JayQue
ID: 6276600

Thanks For All Your Help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
to transfer string from C lanaguage to VBA 4 85
Sending a email via excel using vba 6 117
Automatic Email Reminder 4 94
VBA: loop recent folder and copy txt file. 8 56
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question