Solved

Creating an Excel Piviot Table with a SQL Parameter Query

Posted on 2001-07-05
4
162 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
  • 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now