Microsoft Access
--
Questions
--
Followers
Top Experts
I just ran the query isolated and i was working fine.
Also, i just left the parameters of the query by default and when i was executed with those parameters, my vba code ran fine too.
This is the query
SELECT QuotesFile.Theater, QuotesFile.Country, QuotesFile.[Created By User Type], ([Customer Name]) AS Customer, IIf([Created By User Name] Is Null,"EMPTY",UCase([Created By User Name])) & " - " & [Created By User Type] AS [Created by], IIf([Converted By User Name] Is Null,"EMPTY",UCase([Converted By User Name])) & " - " & [Converted By User Type] AS [Converted by], Sum(IIf(QuotesFile.[Created By Source]='CSCC' And QuotesFile.[Create Convert]=0,1,0)) AS [CSCC-Q], Sum(IIf([Created By Source]="CSCC" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [CSCC-Q-V], Sum(IIf(QuotesFile.[Created By Source]='IQT' And QuotesFile.[Create Convert]=0,1,0)) AS [IQT-Q], Sum(IIf([Created By Source]="IQT" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [IQT-Q-V], Sum(IIf(QuotesFile.[Created By Source]='SCC' And QuotesFile.[Create Convert]=0,1,0)) AS [SCC-Q], Sum(IIf([Created By Source]="SCC" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [SCC-Q-V], Sum(IIf([Created By Source]="CSCC",1,0))/Count([ID]) AS [QAR %], Sum(IIf(QuotesFile.[Created By Source]='CSCC' And QuotesFile.[Create Convert]=1,1,0)) AS [CSCC-O], Sum(IIf([Created By Source]="CSCC" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [CSCC-O-V], Sum(IIf(QuotesFile.[Created By Source]='IQT' And QuotesFile.[Create Convert]=1,1,0)) AS [IQT-O], Sum(IIf([Created By Source]="IQT" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [IQT-O-V], Sum(IIf(QuotesFile.[Created By Source]='SCC' And QuotesFile.[Create Convert]=1,1,0)) AS [SCC-O], Sum(IIf([Created By Source]="SCC" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [SCC-O-V], Sum(IIf([Converted By Source]="CSCC",1,0))/Count(IIf([Converted By Source],1,0)) AS [OAR %], QuotesFile.[Creation Date]
FROM QuotesFile
WHERE (((QuotesFile.[Creation Date]) Between [Forms]![frmDataAnalysis]![txtFOM] And [Forms]![frmDataAnalysis]![txtEOM]))
GROUP BY QuotesFile.Theater, QuotesFile.Country, QuotesFile.[Created By User Type], ([Customer Name]), IIf([Created By User Name] Is Null,"EMPTY",UCase([Created By User Name])) & " - " & [Created By User Type], IIf([Converted By User Name] Is Null,"EMPTY",UCase([Converted By User Name])) & " - " & [Converted By User Type], QuotesFile.[Creation Date], LCase([Created By User]) & " - " & [Created By User Type]
HAVING (((QuotesFile.Theater)=[Forms]![frmDataAnalysis]![cboTheater]) AND ((QuotesFile.Country)=[Forms]![frmDataAnalysis]![cboCountry]));
VBA Code
Public Sub PivotTableExportData()
Dim xlapp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlws As Excel.Worksheet
Dim adors As ADODB.Recordset
Dim x As Long
Set xlapp = New Excel.Application
xlapp.Visible = True
Set xlwb = xlapp.Workbooks.Add
Set xlws = xlwb.Worksheets(1)
xlws.Name = "RawData"
Set adors = New ADODB.Recordset
adors.Open "qryPivotbyPartner", CurrentProject.Connection, adOpenStatic, adLockReadOnly
For x = 1 To adors.Fields.Count
xlws.Cells(1, x) = adors.Fields(x - 1).Name
Next
xlws.Cells(2, 1).CopyFromRecordset adors
adors.Close
Set adors = Nothing
xlwb.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=xlws.Name & "!" & _
xlws.Cells(1, 1).CurrentRegion.Address).CreatePivotTable TableDestination:="", _
tableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
xlapp.ActiveSheet.PivotTableWizard TableDestination:=xlapp.ActiveSheet.Cells(3, 1)
xlapp.ActiveSheet.Cells(3, 1).Select
With xlapp.ActiveSheet.PivotTables("PivotTable2")
'.AddFields RowFields:=Array("Customer Name", "Created by", "Converted by"), PageFields:="Created By User Type"
.AddFields RowFields:=Array("Customer", "Data"), PageFields:="Created By User Type"
'.AddFields RowFields:=Array("Created by", "Data")
'.AddFields RowFields:=Array("Converted by", "Data")
'.AddFields PageFields:=Array("Created By User Type", "Creation Date")
'.AddFields RowFields:=Array(, "Data"), PageFields:="Country", PageFields:="Created By User Type"
'.Selection.ShowDetail = False
'.AddFields RowFields:=Array("Created by", "Data"), PageFields:="Country", PageFields:="Created By User Type"
'.AddFields RowFields:=Array("Customer", "Data"), PageFields:="Country"
'.Name = "Verdana"
'.Size = 8
With .PivotFields("CSCC-Q")
.Orientation = xlDataField
.Caption = "Number CSCC-Q"
.Position = 1
.Function = xlCount
End With
With .PivotFields("CSCC-Q-V")
.Orientation = xlDataField
.Caption = "Total CSCC-Q-V"
.Position = 2
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-Q")
.Orientation = xlDataField
.Caption = "Number IQT-Q"
.Position = 3
.Function = xlCount
End With
With .PivotFields("IQT-Q-V")
.Orientation = xlDataField
.Caption = "Total IQT-Q"
.Position = 4
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-Q")
.Orientation = xlDataField
.Caption = "Number SCC-Q"
.Position = 5
.Function = xlCount
End With
With .PivotFields("SCC-Q-V")
.Orientation = xlDataField
.Caption = "Total SCC-Q-V"
.Position = 6
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("CSCC-O")
.Orientation = xlDataField
.Caption = "Number CSCC-O"
.Position = 7
.Function = xlCount
End With
With .PivotFields("CSCC-O-V")
.Orientation = xlDataField
.Caption = "Total CSCC-O-V"
.Position = 8
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-O")
.Orientation = xlDataField
.Caption = "Number IQT-O"
.Position = 9
.Function = xlCount
End With
With .PivotFields("IQT-O-V")
.Orientation = xlDataField
.Caption = "Total IQT-O"
.Position = 10
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-O")
.Orientation = xlDataField
.Caption = "Number SCC-O"
.Position = 11
.Function = xlCount
End With
With .PivotFields("SCC-O-V")
.Orientation = xlDataField
.Caption = "Total SCC-O-V"
.Position = 12
.NumberFormat = "$#,##0.00"
End With
'With .PivotFields("OAR %")
' .Orientation = xlDataField
' .Caption = "% Orders-Adoption"
' .Position = 13
'.NumberFormat = "$#,##0.00"
'End With
With .DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
End With
xlapp.ActiveSheet.Name = "Pivot-Data"
Set xlws = Nothing
Set xlwb = Nothing
Set xlapp = Nothing
MsgBox "Done"
End Sub
debugginerror.JPGruntimerror-2147217900.JPG
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Your VBA code will look something like:
' Query database using stored procedure (requires command object)
Set MyCommand = New ADODB.Command
Set MyCommand.ActiveConnection
MyCommand.CommandText = "qrySomeQuery" ' <- name of procedure
MyCommand.CommandType = adCmdStoredProc
With MyCommand
.Parameters.Refresh
.Parameters.Append .CreateParameter("QueryTex
.Parameters.Append .CreateParameter("QueryLon
.Parameters.Append .CreateParameter("QueryDat
.Parameters.Append .CreateParameter("QueryDat
.Parameters.Append .CreateParameter("BooleanP
' For more information about CreateParameter syntax:
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
' and navigate to CreateParameter Method
End With
' Open recordset using command object
Set MyRecordset = New ADODB.Recordset
MyRecordset.Open MyCommand, , adOpenDynamic, adLockPessimistic
' For more information about Open method syntax:
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
' and navigate to Open Method (ADO Recordset)
Kevin
that means instead of using parameters by form, I need to include something different?
I do not understand well this matter, do you have any example available
CREATE PROCEDURE pivotByParner[par1] DateTime, [par2] DateTime, [par3] Integer, [par4] Long
as SELECT QuotesFile.Theater, QuotesFile.Country, QuotesFile.[Created By User Type], ([Customer Name]) AS Customer, IIf([Created By User Name] Is Null,"EMPTY",UCase([Created By User Name])) & " - " & [Created By User Type] AS [Created by], IIf([Converted By User Name] Is Null,"EMPTY",UCase([Converted By User Name])) & " - " & [Converted By User Type] AS [Converted by], Sum(IIf(QuotesFile.[Created By Source]='CSCC' And QuotesFile.[Create Convert]=0,1,0)) AS [CSCC-Q], Sum(IIf([Created By Source]="CSCC" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [CSCC-Q-V], Sum(IIf(QuotesFile.[Created By Source]='IQT' And QuotesFile.[Create Convert]=0,1,0)) AS [IQT-Q], Sum(IIf([Created By Source]="IQT" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [IQT-Q-V], Sum(IIf(QuotesFile.[Created By Source]='SCC' And QuotesFile.[Create Convert]=0,1,0)) AS [SCC-Q], Sum(IIf([Created By Source]="SCC" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [SCC-Q-V], Sum(IIf([Created By Source]="CSCC",1,0))/Count([ID]) AS [QAR %], Sum(IIf(QuotesFile.[Created By Source]='CSCC' And QuotesFile.[Create Convert]=1,1,0)) AS [CSCC-O], Sum(IIf([Created By Source]="CSCC" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [CSCC-O-V], Sum(IIf(QuotesFile.[Created By Source]='IQT' And QuotesFile.[Create Convert]=1,1,0)) AS [IQT-O], Sum(IIf([Created By Source]="IQT" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [IQT-O-V], Sum(IIf(QuotesFile.[Created By Source]='SCC' And QuotesFile.[Create Convert]=1,1,0)) AS [SCC-O], Sum(IIf([Created By Source]="SCC" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [SCC-O-V], Sum(IIf([Converted By Source]="CSCC",1,0))/Count(IIf([Converted By Source],1,0)) AS [OAR %], QuotesFile.[Creation Date]
FROM QuotesFile
WHERE (((QuotesFile.[Creation Date]) Between [Forms]![frmDataAnalysis]![txtFOM] And [Forms]![frmDataAnalysis]![txtEOM]))
GROUP BY QuotesFile.Theater, QuotesFile.Country, QuotesFile.[Created By User Type], ([Customer Name]), IIf([Created By User Name] Is Null,"EMPTY",UCase([Created By User Name])) & " - " & [Created By User Type], IIf([Converted By User Name] Is Null,"EMPTY",UCase([Converted By User Name])) & " - " & [Converted By User Type], QuotesFile.[Creation Date], LCase([Created By User]) & " - " & [Created By User Type]
HAVING (((QuotesFile.Theater)=[Forms]![frmDataAnalysis]![cboTheater]) AND ((QuotesFile.Country)=[Forms]![frmDataAnalysis]![cboCountry]));






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
The function below will open a recordset from a query, having VB evaluate the parameters, as Jet cannot see them when run from VB (the forms are only available to queries opened from the interface or from the DoCmd object).
Cheers!
(°v°)
Function OpenQuery(pstrName As String) As DAO.Recordset
Dim par As DAO.Parameter
On Error GoTo Failure
With CurrentDb.QueryDefs(pstrName)
For Each par In .Parameters
par.Value = Eval(par.Name)
Next par
Set OpenQuery = .OpenRecordset(dbOpenDynaset)
End With
Exit Function
Failure:
MsgBox Err.Description
Err.Clear
End Function
[Forms]![frmDataAnalysis]!
to:
[TheaterParam]
Then, in the ADODB code, the command parameter is set up like so:
.Parameters.Append .CreateParameter("TheaterP
And that's it.
Kevin
This the VBA Code
Public Sub PivotTableExportData()
Dim xlapp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlws As Excel.Worksheet
'Dim adors As ADODB.Recordset
Dim MyRecordset As ADODB.Recordset
Dim MyCommand As ADODB.Command
Dim MyDatabase As ADODB.Connection
Dim x As Long
Set xlapp = New Excel.Application
xlapp.Visible = True
Set xlwb = xlapp.Workbooks.Add
Set xlws = xlwb.Worksheets(1)
xlws.Name = "RawData"
'Set adors = New ADODB.Recordset
Set MyCommand = New ADODB.Command
Set MyCommand.ActiveConnection = MyDatabase
MyCommand.CommandText = "qryPivotbyPartner" ' <- name of procedure
MyCommand.CommandType = adCmdStoredProc
With MyCommand
.Parameters.Refresh
.Parameters.Append .CreateParameter("TheaterParam", adVarChar, adParamInput, 10, "cboTheater")
.Parameters.Append .CreateParameter("CountryParam", adVarChar, adParamInput, 10, "cboCountry")
.Parameters.Append .CreateParameter("FOMParam", adDate, adParamInput, , txtFOM)
.Parameters.Append .CreateParameter("EOMParam", adDate, adParamInput, , txtEOM)
'.Parameters.Append .CreateParameter("QueryDateTimeStampParam", adDBTimeStamp, adParamInput, , DateTimeValue)
'.Parameters.Append .CreateParameter("BooleanParam", adBoolean, adParamInput, , BooleanValue)
' For more information about CreateParameter syntax:
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
' and navigate to CreateParameter Method
End With
' Open recordset using command object
Set MyRecordset = New ADODB.Recordset
MyRecordset.Open MyCommand, , adOpenDynamic, adLockPessimistic
' For more information about Open method syntax:
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
' and navigate to Open Method (ADO Recordset)
'adors.Open "qryPivotbyPartner", CurrentProject.Connection, adOpenStatic, adLockReadOnly
For x = 1 To MyRecordset.Fields.Count
'For x = 1 To adors.Fields.Count
'xlws.Cells(1, x) = adors.Fields(x - 1).Name
xlws.Cells(1, x) = MyRecordset.Fields(x - 1).Name
Next
'xlws.Cells(2, 1).CopyFromRecordset adors
xlws.Cells(2, 1).CopyFromRecordset MyRecordset
'adors.Close
MyRecordset.Close
'Set adors = Nothing
Set MyRecordset = Nothing
xlwb.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=xlws.Name & "!" & _
xlws.Cells(1, 1).CurrentRegion.Address).CreatePivotTable TableDestination:="", _
tableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
xlapp.ActiveSheet.PivotTableWizard TableDestination:=xlapp.ActiveSheet.Cells(3, 1)
xlapp.ActiveSheet.Cells(3, 1).Select
With xlapp.ActiveSheet.PivotTables("PivotTable2")
.AddFields RowFields:=Array("Customer", "Data"), PageFields:="Created By User Type"
With .PivotFields("CSCC-Q")
.Orientation = xlDataField
.Caption = "Number CSCC-Q"
.Position = 1
.Function = xlCount
End With
With .PivotFields("CSCC-Q-V")
.Orientation = xlDataField
.Caption = "Total CSCC-Q-V"
.Position = 2
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-Q")
.Orientation = xlDataField
.Caption = "Number IQT-Q"
.Position = 3
.Function = xlCount
End With
With .PivotFields("IQT-Q-V")
.Orientation = xlDataField
.Caption = "Total IQT-Q"
.Position = 4
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-Q")
.Orientation = xlDataField
.Caption = "Number SCC-Q"
.Position = 5
.Function = xlCount
End With
With .PivotFields("SCC-Q-V")
.Orientation = xlDataField
.Caption = "Total SCC-Q-V"
.Position = 6
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("CSCC-O")
.Orientation = xlDataField
.Caption = "Number CSCC-O"
.Position = 7
.Function = xlCount
End With
With .PivotFields("CSCC-O-V")
.Orientation = xlDataField
.Caption = "Total CSCC-O-V"
.Position = 8
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-O")
.Orientation = xlDataField
.Caption = "Number IQT-O"
.Position = 9
.Function = xlCount
End With
With .PivotFields("IQT-O-V")
.Orientation = xlDataField
.Caption = "Total IQT-O"
.Position = 10
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-O")
.Orientation = xlDataField
.Caption = "Number SCC-O"
.Position = 11
.Function = xlCount
End With
With .PivotFields("SCC-O-V")
.Orientation = xlDataField
.Caption = "Total SCC-O-V"
.Position = 12
.NumberFormat = "$#,##0.00"
End With
'With .PivotFields("OAR %")
' .Orientation = xlDataField
' .Caption = "% Orders-Adoption"
' .Position = 13
'.NumberFormat = "$#,##0.00"
'End With
With .DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
End With
xlapp.ActiveSheet.Name = "Pivot-Data"
Set xlws = Nothing
Set xlwb = Nothing
Set xlapp = Nothing
MsgBox "Done"
End Sub
Query Code
SELECT QuotesFile.Theater, QuotesFile.Country, QuotesFile.[Created By User Type], ([Customer Name]) AS Customer, IIf([Created By User Name] Is Null,"EMPTY",UCase([Created By User Name])) & " - " & [Created By User Type] AS [Created by], IIf([Converted By User Name] Is Null,"EMPTY",UCase([Converted By User Name])) & " - " & [Converted By User Type] AS [Converted by], Sum(IIf(QuotesFile.[Created By Source]='CSCC' And QuotesFile.[Create Convert]=0,1,0)) AS [CSCC-Q], Sum(IIf([Created By Source]="CSCC" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [CSCC-Q-V], Sum(IIf(QuotesFile.[Created By Source]='IQT' And QuotesFile.[Create Convert]=0,1,0)) AS [IQT-Q], Sum(IIf([Created By Source]="IQT" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [IQT-Q-V], Sum(IIf(QuotesFile.[Created By Source]='SCC' And QuotesFile.[Create Convert]=0,1,0)) AS [SCC-Q], Sum(IIf([Created By Source]="SCC" And QuotesFile.[Create Convert]=0,QuotesFile.[Net Price],0)) AS [SCC-Q-V], Sum(IIf([Created By Source]="CSCC",1,0))/Count([ID]) AS [QAR %], Sum(IIf(QuotesFile.[Created By Source]='CSCC' And QuotesFile.[Create Convert]=1,1,0)) AS [CSCC-O], Sum(IIf([Created By Source]="CSCC" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [CSCC-O-V], Sum(IIf(QuotesFile.[Created By Source]='IQT' And QuotesFile.[Create Convert]=1,1,0)) AS [IQT-O], Sum(IIf([Created By Source]="IQT" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [IQT-O-V], Sum(IIf(QuotesFile.[Created By Source]='SCC' And QuotesFile.[Create Convert]=1,1,0)) AS [SCC-O], Sum(IIf([Created By Source]="SCC" And QuotesFile.[Create Convert]=1,QuotesFile.[Net Price],0)) AS [SCC-O-V], Sum(IIf([Converted By Source]="CSCC",1,0))/Count(IIf([Converted By Source],1,0)) AS [OAR %], QuotesFile.[Creation Date]
FROM QuotesFile
WHERE (((QuotesFile.[Creation Date]) Between [FOMParam] And [EOMParam]))
GROUP BY QuotesFile.Theater, QuotesFile.Country, QuotesFile.[Created By User Type], ([Customer Name]), IIf([Created By User Name] Is Null,"EMPTY",UCase([Created By User Name])) & " - " & [Created By User Type], IIf([Converted By User Name] Is Null,"EMPTY",UCase([Converted By User Name])) & " - " & [Converted By User Type], QuotesFile.[Creation Date], LCase([Created By User]) & " - " & [Created By User Type]
HAVING (((QuotesFile.Theater)=[TheaterParam]) AND ((QuotesFile.Country)=[CountryParam]));

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I am just getting a little bit confuse
I know that I have to leave my query as I coded before :) What about using this function?
If that doesn't work, you will have to follow the instructions other experts have given to re-create your parameters from code.
Finally, there is a third option: rewrite your entire SQL in code, replacing the criteria with constant expressions derived from your form. In the long run, that's what I use the most in this type of situations.
(°v°)
' Dim adors As ADODB.Recordset
Dim daors As DAO.Recordset
' ....
' Set adors = New ADODB.Recordset
' adors.Open "qryPivotbyPartner", CurrentProject.Connection, adOpenStatic, adLockReadOnly
Set daprs = OpenQuery("qryPivotbyPartner") ' function defined above
' ...
' xlws.Cells(2, 1).CopyFromRecordset adors
xlws.Cells(2, 1).CopyFromRecordset daors ' <--- potential problem






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
' Open database connection
Set MyDatabase = New ADODB.Connection
MyDatabase.CursorLocation = adUseClient
MyDatabase.Open "Provider=Microsoft.Jet.OL
' For more information about Open syntax:
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
' and navigate to Open Method (ADO Connection)
' Additional help constructing connection strings can be found at http://www.connectionstrings.com/
Kevin
Also, my application does not use user neither password, so i will leave as blank or as the way that you post.
I will try and I will let you know
thanks
You can ask the user for the path to the database file and then substitute it into the connection string:
Dim FilePathName As String
FilePathName = Application.GetOpenFilenam
If Len(FilePathName) = 0 Then Exit Sub
' ...
MyDatabase.Open "Provider=Microsoft.Jet.OL
You might also consider asking the user for the Access database path when they first run the application and storing it somewhere so they don't have to be asked again.
Kevin

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Thanks
Kevin
Public Sub PivotTableExportData()
Dim xlApp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlws As Excel.Worksheet
'Dim adors As ADODB.Recordset
Dim MyRecordset As ADODB.Recordset
Dim MyCommand As ADODB.Command
Dim MyDatabase As ADODB.Connection
Dim x As Long
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlwb = xlApp.Workbooks.Add
Set xlws = xlwb.Worksheets(1)
xlws.Name = "RawData"
'Set adors = New ADODB.Recordset
Set MyDatabase = New ADODB.Connection
MyDatabase.CursorLocation = adUseClient
MyDatabase.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\Documents and Settings\giofranc\My Documents\My Projects\FY08-02\CSCC Adoption Metrics Tool\CSCC-AMT-3292008.mdb'; User Id=admin; Password=;"
'Set MyCommand = New ADODB.Command
Set MyCommand = New ADODB.Connection
Set MyCommand.ActiveConnection = MyDatabase
MyCommand.CommandText = "ADOqryPivotbyPartner" ' <- name of procedure
MyCommand.CommandType = adCmdStoredProc
With MyCommand
.Parameters.Refresh
.Parameters.Append .CreateParameter("TheaterParam", adVarChar, adParamInput, 10, "cboTheater")
.Parameters.Append .CreateParameter("CountryParam", adVarChar, adParamInput, 10, "cboCountry")
.Parameters.Append .CreateParameter("FOMParam", adDate, adParamInput, , txtFOM)
.Parameters.Append .CreateParameter("EOMParam", adDate, adParamInput, , txtEOM)
'.Parameters.Append .CreateParameter("QueryDateTimeStampParam", adDBTimeStamp, adParamInput, , DateTimeValue)
'.Parameters.Append .CreateParameter("BooleanParam", adBoolean, adParamInput, , BooleanValue)
' For more information about CreateParameter syntax:
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
' and navigate to CreateParameter Method
End With
' Open recordset using command object
Set MyRecordset = New ADODB.Recordset
MyRecordset.Open MyCommand, , adOpenDynamic, adLockPessimistic
' For more information about Open method syntax:
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
' and navigate to Open Method (ADO Recordset)
'adors.Open "qryPivotbyPartner", CurrentProject.Connection, adOpenStatic, adLockReadOnly
For x = 1 To MyRecordset.Fields.Count
'For x = 1 To adors.Fields.Count
'xlws.Cells(1, x) = adors.Fields(x - 1).Name
xlws.Cells(1, x) = MyRecordset.Fields(x - 1).Name
Next
'xlws.Cells(2, 1).CopyFromRecordset adors
xlws.Cells(2, 1).CopyFromRecordset MyRecordset
'adors.Close
MyRecordset.Close
'Set adors = Nothing
Set MyRecordset = Nothing
xlwb.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=xlws.Name & "!" & _
xlws.Cells(1, 1).CurrentRegion.Address).CreatePivotTable TableDestination:="", _
tableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
xlApp.ActiveSheet.PivotTableWizard TableDestination:=xlApp.ActiveSheet.Cells(3, 1)
xlApp.ActiveSheet.Cells(3, 1).Select
With xlApp.ActiveSheet.PivotTables("PivotTable2")
.AddFields RowFields:=Array("Customer", "Data"), PageFields:="Created By User Type"
With .PivotFields("CSCC-Q")
.Orientation = xlDataField
.Caption = "Number CSCC-Q"
.Position = 1
.Function = xlCount
End With
With .PivotFields("CSCC-Q-V")
.Orientation = xlDataField
.Caption = "Total CSCC-Q-V"
.Position = 2
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-Q")
.Orientation = xlDataField
.Caption = "Number IQT-Q"
.Position = 3
.Function = xlCount
End With
With .PivotFields("IQT-Q-V")
.Orientation = xlDataField
.Caption = "Total IQT-Q"
.Position = 4
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-Q")
.Orientation = xlDataField
.Caption = "Number SCC-Q"
.Position = 5
.Function = xlCount
End With
With .PivotFields("SCC-Q-V")
.Orientation = xlDataField
.Caption = "Total SCC-Q-V"
.Position = 6
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("CSCC-O")
.Orientation = xlDataField
.Caption = "Number CSCC-O"
.Position = 7
.Function = xlCount
End With
With .PivotFields("CSCC-O-V")
.Orientation = xlDataField
.Caption = "Total CSCC-O-V"
.Position = 8
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-O")
.Orientation = xlDataField
.Caption = "Number IQT-O"
.Position = 9
.Function = xlCount
End With
With .PivotFields("IQT-O-V")
.Orientation = xlDataField
.Caption = "Total IQT-O"
.Position = 10
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-O")
.Orientation = xlDataField
.Caption = "Number SCC-O"
.Position = 11
.Function = xlCount
End With
With .PivotFields("SCC-O-V")
.Orientation = xlDataField
.Caption = "Total SCC-O-V"
.Position = 12
.NumberFormat = "$#,##0.00"
End With
'With .PivotFields("OAR %")
' .Orientation = xlDataField
' .Caption = "% Orders-Adoption"
' .Position = 13
'.NumberFormat = "$#,##0.00"
'End With
With .DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
End With
xlApp.ActiveSheet.Name = "Pivot-Data"
Set xlws = Nothing
Set xlwb = Nothing
Set xlApp = Nothing
MsgBox "Done"
End Sub






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Dim FilePathName As String
FilePathName = Application.GetOpenFilenam
If Len(FilePathName) = 0 Then Exit Sub
ThisWorkbook.Names.Add "MyAccessDBPath", FilePathName
' ...
MyDatabase.Open "Provider=Microsoft.Jet.OL
Kevin
I tested yours too but it is not working, could u able to look up it.
Public Sub PivotTableExportData()
Dim xlApp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlws As Excel.Worksheet
Dim daors As DAO.Recordset
Dim x As Long
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlwb = xlApp.Workbooks.Add
Set xlws = xlwb.Worksheets(1)
xlws.Name = "RawData"
Set daprs = OpenQuery("qryPivotbyPartner") ' function defined above
xlws.Cells(2, 1).CopyFromRecordset daors ' <--- potential problem
xlwb.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=xlws.Name & "!" & _
xlws.Cells(1, 1).CurrentRegion.Address).CreatePivotTable TableDestination:="", _
tableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10
xlApp.ActiveSheet.PivotTableWizard TableDestination:=xlApp.ActiveSheet.Cells(3, 1)
xlApp.ActiveSheet.Cells(3, 1).Select
With xlApp.ActiveSheet.PivotTables("PivotTable2")
.AddFields RowFields:=Array("Customer", "Data"), PageFields:="Created By User Type"
With .PivotFields("CSCC-Q")
.Orientation = xlDataField
.Caption = "Number CSCC-Q"
.Position = 1
.Function = xlCount
End With
With .PivotFields("CSCC-Q-V")
.Orientation = xlDataField
.Caption = "Total CSCC-Q-V"
.Position = 2
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-Q")
.Orientation = xlDataField
.Caption = "Number IQT-Q"
.Position = 3
.Function = xlCount
End With
With .PivotFields("IQT-Q-V")
.Orientation = xlDataField
.Caption = "Total IQT-Q"
.Position = 4
.Function = xlSum
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-Q")
.Orientation = xlDataField
.Caption = "Number SCC-Q"
.Position = 5
.Function = xlCount
End With
With .PivotFields("SCC-Q-V")
.Orientation = xlDataField
.Caption = "Total SCC-Q-V"
.Position = 6
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("CSCC-O")
.Orientation = xlDataField
.Caption = "Number CSCC-O"
.Position = 7
.Function = xlCount
End With
With .PivotFields("CSCC-O-V")
.Orientation = xlDataField
.Caption = "Total CSCC-O-V"
.Position = 8
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("IQT-O")
.Orientation = xlDataField
.Caption = "Number IQT-O"
.Position = 9
.Function = xlCount
End With
With .PivotFields("IQT-O-V")
.Orientation = xlDataField
.Caption = "Total IQT-O"
.Position = 10
.NumberFormat = "$#,##0.00"
End With
With .PivotFields("SCC-O")
.Orientation = xlDataField
.Caption = "Number SCC-O"
.Position = 11
.Function = xlCount
End With
With .PivotFields("SCC-O-V")
.Orientation = xlDataField
.Caption = "Total SCC-O-V"
.Position = 12
.NumberFormat = "$#,##0.00"
End With
'With .PivotFields("OAR %")
' .Orientation = xlDataField
' .Caption = "% Orders-Adoption"
' .Position = 13
'.NumberFormat = "$#,##0.00"
'End With
With .DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
End With
xlApp.ActiveSheet.Name = "Pivot-Data"
Set xlws = Nothing
Set xlwb = Nothing
Set xlApp = Nothing
MsgBox "Done"
End Sub

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
'Set MyCommand = New ADODB.Command
Set MyCommand = New ADODB.Connection
should be:
Set MyCommand = New ADODB.Command
Kevin
(°v°)
Kevin






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Anyone could assist me on better way?
txtFOM mm/dd/yyyy
txtEOM mm/dd/yyyy
cboTheater
cboCountry
from my form since my SQL Is huge
I am really appreciate much help, thanks
Thanks
Function DateSQL(pvarFOM, pvarEOM, Theater, Country) As String
If IsNull(pvarDate) Then
DateSQL = "Null"
Else
DateSQLFOM = "#" & Format(pvarFOM, "yyyy-mm-dd") & "#"
DateSQLEOM = "#" & Format(pvarEOM, "mm/dd/yyyy") & "#"
SQLTheater = Me.cboTheater
SQLCountry = Me.Country
End If
End Function
....
Dim strSQL As String
With Forms!frmDataAnalysis
strSQL _
= " SELECT * FROM QuotesFile" _
& " WHERE [Creation Date])" _
& " Between " & DateSQL(!txtFOM) _
& " And " & DateSQL(!txtEOM)
End With
' use query...
? DateSQL(Date())
#2008-04-15#
or, in your case and if the form is open:
? DateSQL(Forms!frmDataAnaly
#2000-01-01# ' or whatever is current
The fragment below the function is meant as an illustration on how to build queries from scratch in code, if you wanted to try that. Observe how the function is used to build the string using the dates from your form.
But seeing how you modified the function, I don't think you should go this way just yet. Gather a little more experience in programming, because it really quite tricky to write code for one language (SQL) from another (VB).
Revert back to one of the previous solutions.
(°v°)

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Could you able to look up it this link it is related, thanks
https://www.experts-exchange.com/questions/23313405/Exporting-query-to-and-Excel-Template.html
Good luck!
(°v°)
Thanks






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
This post will cancel the pending deletion request.
Kevin
The bottom line is gfranco needs to explain what the current situation is. If he has resolved the issue then I would like to see that resolution posted. If that resolution is based on solutions that Harfang and/or I provided than credit should be provided.
Kevin
The last contribuitor post said: that i need little bit more knowledge on programming. I know that i am not an expert, and i was trying to look something good that might fit to my requiriment.
I will give you a bonus, for me is not a problem, but i thought that i would be more supported.
Thanks anyway for your time :)

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Microsoft Access
--
Questions
--
Followers
Top Experts
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.