Passing DateTime Picker values to query

G

Guest

I have a query:

TRANSFORM Count(EscalationTable.EscalationID) AS CountOfEscalationID
SELECT EscalationReasons.EscalationReason,
Count(EscalationTable.EscalationID) AS [TOTALS BY REASON]
FROM EscalationReasons INNER JOIN (Region INNER JOIN EscalationTable ON
Region.RegionID = EscalationTable.RegionID) ON
EscalationReasons.EscalationReasonID = EscalationTable.EscalationReasonID
WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value]) AND
((EscalationTable.ErrorByID) Between 3 And 8))
GROUP BY EscalationReasons.EscalationReason, EscalationTable.ErrorByID
PIVOT Region.RegionName;

This query is called from VBA code:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"q-ExcelExportHEFCOpportunity", "C:\Documents and
Settings\UserName\Desktop\Escalation Data.xls", , "HEFC Opportunities by
Market"

Whe query is run, I get a Error message that Jet Engine does not recognize
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] as a field name or
expression.

What am I doing wrong here?
 
G

Guest

Hi Deki,

Please see my article:
How To Create A Crosstab Query
http://www.access.qbuilt.com/html/crosstab_queries.html

Pay particular attention to paragraph 15.

If my answer has helped you, please answer yes to the question that reads
"Did this post answer the question?" at the bottom of the message thread.


Tom

http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

:

I have a query:

TRANSFORM Count(EscalationTable.EscalationID) AS CountOfEscalationID
SELECT EscalationReasons.EscalationReason,
Count(EscalationTable.EscalationID) AS [TOTALS BY REASON]
FROM EscalationReasons INNER JOIN (Region INNER JOIN EscalationTable ON
Region.RegionID = EscalationTable.RegionID) ON
EscalationReasons.EscalationReasonID = EscalationTable.EscalationReasonID
WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value]) AND
((EscalationTable.ErrorByID) Between 3 And 8))
GROUP BY EscalationReasons.EscalationReason, EscalationTable.ErrorByID
PIVOT Region.RegionName;

This query is called from VBA code:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"q-ExcelExportHEFCOpportunity", "C:\Documents and
Settings\UserName\Desktop\Escalation Data.xls", , "HEFC Opportunities by
Market"

Whe query is run, I get a Error message that Jet Engine does not recognize
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] as a field name or
expression.

What am I doing wrong here?
 
N

Nikos Yannacopoulos

Deki,

Unlike other query types, crosstabs demand that you explicitly declare
parameters as such. While in query design, go to menu item Query >
Parameters and paste your parameter expression, selecting the
appropriate time in the second column.

HTH,
Nikos
 
G

Guest

Thank you Tom and Nikos, that was helpfull.
However, I'm running a query that creates excel spreadsheet and two
additonal worksheets in it (total of 3) and everything is based on same
from/to date. If I leave parametars in queries that add worksheets, user has
to input same from/to dates 3 times. Is it possible for those 2 queries to
pick up value from form, so user can just select from/to dated from DTPickers
on form?
 
G

Guest

Hi Deki,
If I leave parametars in queries that add worksheets, user has
to input same from/to dates 3 times.

Why would they? Can't you use the same logic for your two additional
queries, ie. where the query criteria is filled in by dates entered on your
form, like this:

WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value])



Thank You too for marking my response as an answer.

Tom
__________________________________________

:

Thank you Tom and Nikos, that was helpfull.
However, I'm running a query that creates excel spreadsheet and two
additonal worksheets in it (total of 3) and everything is based on same
from/to date. If I leave parametars in queries that add worksheets, user has
to input same from/to dates 3 times. Is it possible for those 2 queries to
pick up value from form, so user can just select from/to dated from DTPickers
on form?
--
Deki PA
__________________________________________

:

Hi Deki,

Please see my article:
How To Create A Crosstab Query
http://www.access.qbuilt.com/html/crosstab_queries.html

Pay particular attention to paragraph 15.

If my answer has helped you, please answer yes to the question that reads
"Did this post answer the question?" at the bottom of the message thread.


Tom

http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

:

I have a query:

TRANSFORM Count(EscalationTable.EscalationID) AS CountOfEscalationID
SELECT EscalationReasons.EscalationReason,
Count(EscalationTable.EscalationID) AS [TOTALS BY REASON]
FROM EscalationReasons INNER JOIN (Region INNER JOIN EscalationTable ON
Region.RegionID = EscalationTable.RegionID) ON
EscalationReasons.EscalationReasonID = EscalationTable.EscalationReasonID
WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value]) AND
((EscalationTable.ErrorByID) Between 3 And 8))
GROUP BY EscalationReasons.EscalationReason, EscalationTable.ErrorByID
PIVOT Region.RegionName;

This query is called from VBA code:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"q-ExcelExportHEFCOpportunity", "C:\Documents and
Settings\UserName\Desktop\Escalation Data.xls", , "HEFC Opportunities by
Market"

Whe query is run, I get a Error message that Jet Engine does not recognize
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] as a field name or
expression.

What am I doing wrong here?
 
G

Guest

I would love to do it in code instead creating queries, but I dont know how
to add workbooks to spreadsheet using DoCmd.TransferSpreadsheet without
creating a query.
Below is what I have done, but I get error message: "The table name you
entered doesn't follow Error and Escalation database object-naming rules".
"Error and Escalation" is name of my database.

Private Sub cmdExcelSummary_Click()
On Error GoTo Err_cmdExcelSummary_Click

Dim db As Database
Dim Qry As QueryDef
Dim StrSQL As String
Dim ques As Integer
Dim X As String
Dim datFromDate As Date
Dim datToDate As Date
ques = MsgBox("Are you sure you want to export the data to excel? This
may take a few minutes.", vbYesNo, "Export Data?")
If ques = 7 Then
Exit Sub
End If
StrSQL = "SELECT EscalationTable.EscalationDate, NameTable.Name,
EscalationTable.EscalationOwner, EscalationTable.EscalationSourceName1,
Title.Title, EscalationTable.EscalationSourceName2,"
StrSQL = StrSQL + " (Select Title.Title From Title WHERE
Title.TitleID=EscalationTable.TitleID2) AS Title2, Supervisor.SupervisorName,
Manager.ManagerName, ErrorByTable.ErrorBy, EscalationTable.CustomerName,
EscalationTable.ACAPS,"
StrSQL = StrSQL + " EscalationTable.ApplicationDate,
EscalationTable.LoanLineAmt, Product.ProductName,
EscalationReasons.EscalationReason, Region.RegionName,
EscalationTable.Followup, EscalationTable.FollowupDate,
EscalationTable.Resolved, EscalationTable.ResolvedDate,
EscalationTable.CustExpFunds, EscalationTable.Details,
EscalationTable.Training"
StrSQL = StrSQL + " FROM Title INNER JOIN ((Supervisor INNER JOIN
(Manager INNER JOIN NameTable ON Manager.ManagerID = NameTable.ManagerID) ON
Supervisor.SupervisorID = NameTable.SupervisorID) INNER JOIN (Region"
StrSQL = StrSQL + " INNER JOIN (Product INNER JOIN (EscalationReasons
INNER JOIN (ErrorByTable INNER JOIN EscalationTable ON ErrorByTable.ErrorByID
= EscalationTable.ErrorByID) ON EscalationReasons.EscalationReasonID ="
StrSQL = StrSQL + " EscalationTable.EscalationReasonID) ON
Product.ProductID = EscalationTable.ProductID) ON Region.RegionID =
EscalationTable.RegionID) ON NameTable.NameID = EscalationTable.NameID) ON
Title.TitleID = EscalationTable.TitleID"

'Checking for date from/to or gives whole report
If chkFromToDate = True Then
If DTPicker0.Value > DTPicker1.Value Then
MsgBox "Start date is greater then end date. Please input
correct date.", vbCritical, "Error"
Exit Sub
Else
End If
datFromDate = Forms!frmReportselection!DTPicker0
datToDate = Forms!frmReportselection!DTPicker1
X = "(EscalationDate Between #" & datFromDate & "# AND #" &
datToDate & "#)"
Else
varFromDate = 0
varToDate = 0
End If

If X <> "" Then 'If exists, adds where clause to SQL string
StrSQL = StrSQL + " Where " + X
Else
End If
'Debug.Print StrSQL
Set db = CurrentDb 'Checks if "Escalation Data" query exists and deletes
it
For Each Qry In db.QueryDefs
If Qry.Name = "Escalation Data" Then
db.QueryDefs.Delete "Escalation Data"
End If
Next Qry

Set Qry = db.CreateQueryDef("Escalation Data", StrSQL)
Set Qry = Nothing
Set db = Nothing
'Output to Excel first worksheet
DoCmd.OutputTo acOutputQuery, "Escalation Data", acFormatXLS, , False
'Output to Excel second worksheet
StrSQL = "TRANSFORM Count(EscalationTable.EscalationID) AS
CountOfEscalationID"
StrSQL = StrSQL + "SELECT EscalationReasons.EscalationReason,
Count(EscalationTable.EscalationID) AS [TOTALS BY REASON]"
StrSQL = StrSQL + "FROM EscalationReasons INNER JOIN (Region INNER JOIN
EscalationTable ON Region.RegionID = EscalationTable.RegionID) ON
EscalationReasons.EscalationReasonID = EscalationTable.EscalationReasonID"
StrSQL = StrSQL + "WHERE (EscalationTable.ErrorByID=1) AND
EscalationTable." + X
StrSQL = StrSQL + "GROUP BY EscalationReasons.EscalationReason"
StrSQL = StrSQL + "PIVOT Region.RegionName"
'**************This part doesn't work***********************
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, StrSQL,
"C:\Documents and Settings\simurdan\Desktop\Escalation Data.xls", , "HEFC
Opportunities by Market"
'Output to Excel third worksheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"q-ExcelExportBankerOpportunity", "C:\Documents and
Settings\simurdan\Desktop\Escalation Data.xls", , "Banker Opportunities by
Market"
'**********************************************************
exit_cmdExcelSummary_Click:
On Error Resume Next
DoCmd.SetWarnings False
DoCmd.DeleteObject acQuery, "Escalation Data"
DoCmd.SetWarnings True
Exit Sub

Err_cmdExcelSummary_Click:
MsgBox Err.Description
Resume exit_cmdExcelSummary_Click
End Sub

Deki PA



Tom Wickerath said:
Hi Deki,
If I leave parametars in queries that add worksheets, user has
to input same from/to dates 3 times.

Why would they? Can't you use the same logic for your two additional
queries, ie. where the query criteria is filled in by dates entered on your
form, like this:

WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value])



Thank You too for marking my response as an answer.

Tom
__________________________________________

:

Thank you Tom and Nikos, that was helpfull.
However, I'm running a query that creates excel spreadsheet and two
additonal worksheets in it (total of 3) and everything is based on same
from/to date. If I leave parametars in queries that add worksheets, user has
to input same from/to dates 3 times. Is it possible for those 2 queries to
pick up value from form, so user can just select from/to dated from DTPickers
on form?
--
Deki PA
__________________________________________

:

Hi Deki,

Please see my article:
How To Create A Crosstab Query
http://www.access.qbuilt.com/html/crosstab_queries.html

Pay particular attention to paragraph 15.

If my answer has helped you, please answer yes to the question that reads
"Did this post answer the question?" at the bottom of the message thread.


Tom

http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

:

I have a query:

TRANSFORM Count(EscalationTable.EscalationID) AS CountOfEscalationID
SELECT EscalationReasons.EscalationReason,
Count(EscalationTable.EscalationID) AS [TOTALS BY REASON]
FROM EscalationReasons INNER JOIN (Region INNER JOIN EscalationTable ON
Region.RegionID = EscalationTable.RegionID) ON
EscalationReasons.EscalationReasonID = EscalationTable.EscalationReasonID
WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value]) AND
((EscalationTable.ErrorByID) Between 3 And 8))
GROUP BY EscalationReasons.EscalationReason, EscalationTable.ErrorByID
PIVOT Region.RegionName;

This query is called from VBA code:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"q-ExcelExportHEFCOpportunity", "C:\Documents and
Settings\UserName\Desktop\Escalation Data.xls", , "HEFC Opportunities by
Market"

Whe query is run, I get a Error message that Jet Engine does not recognize
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] as a field name or
expression.

What am I doing wrong here?
 
G

Guest

I realized some syntax errors in query and fixed them. Now queries work and,
I guess, I can't pass string variable in DoCmd.TransferSpreadsheet. Is there
any other way to add worksheets without having to create temporary tables or
queries?
--
Deki PA



Deki said:
I would love to do it in code instead creating queries, but I dont know how
to add workbooks to spreadsheet using DoCmd.TransferSpreadsheet without
creating a query.
Below is what I have done, but I get error message: "The table name you
entered doesn't follow Error and Escalation database object-naming rules".
"Error and Escalation" is name of my database.

Private Sub cmdExcelSummary_Click()
On Error GoTo Err_cmdExcelSummary_Click

Dim db As Database
Dim Qry As QueryDef
Dim StrSQL As String
Dim ques As Integer
Dim X As String
Dim datFromDate As Date
Dim datToDate As Date
ques = MsgBox("Are you sure you want to export the data to excel? This
may take a few minutes.", vbYesNo, "Export Data?")
If ques = 7 Then
Exit Sub
End If
StrSQL = "SELECT EscalationTable.EscalationDate, NameTable.Name,
EscalationTable.EscalationOwner, EscalationTable.EscalationSourceName1,
Title.Title, EscalationTable.EscalationSourceName2,"
StrSQL = StrSQL + " (Select Title.Title From Title WHERE
Title.TitleID=EscalationTable.TitleID2) AS Title2, Supervisor.SupervisorName,
Manager.ManagerName, ErrorByTable.ErrorBy, EscalationTable.CustomerName,
EscalationTable.ACAPS,"
StrSQL = StrSQL + " EscalationTable.ApplicationDate,
EscalationTable.LoanLineAmt, Product.ProductName,
EscalationReasons.EscalationReason, Region.RegionName,
EscalationTable.Followup, EscalationTable.FollowupDate,
EscalationTable.Resolved, EscalationTable.ResolvedDate,
EscalationTable.CustExpFunds, EscalationTable.Details,
EscalationTable.Training"
StrSQL = StrSQL + " FROM Title INNER JOIN ((Supervisor INNER JOIN
(Manager INNER JOIN NameTable ON Manager.ManagerID = NameTable.ManagerID) ON
Supervisor.SupervisorID = NameTable.SupervisorID) INNER JOIN (Region"
StrSQL = StrSQL + " INNER JOIN (Product INNER JOIN (EscalationReasons
INNER JOIN (ErrorByTable INNER JOIN EscalationTable ON ErrorByTable.ErrorByID
= EscalationTable.ErrorByID) ON EscalationReasons.EscalationReasonID ="
StrSQL = StrSQL + " EscalationTable.EscalationReasonID) ON
Product.ProductID = EscalationTable.ProductID) ON Region.RegionID =
EscalationTable.RegionID) ON NameTable.NameID = EscalationTable.NameID) ON
Title.TitleID = EscalationTable.TitleID"

'Checking for date from/to or gives whole report
If chkFromToDate = True Then
If DTPicker0.Value > DTPicker1.Value Then
MsgBox "Start date is greater then end date. Please input
correct date.", vbCritical, "Error"
Exit Sub
Else
End If
datFromDate = Forms!frmReportselection!DTPicker0
datToDate = Forms!frmReportselection!DTPicker1
X = "(EscalationDate Between #" & datFromDate & "# AND #" &
datToDate & "#)"
Else
varFromDate = 0
varToDate = 0
End If

If X <> "" Then 'If exists, adds where clause to SQL string
StrSQL = StrSQL + " Where " + X
Else
End If
'Debug.Print StrSQL
Set db = CurrentDb 'Checks if "Escalation Data" query exists and deletes
it
For Each Qry In db.QueryDefs
If Qry.Name = "Escalation Data" Then
db.QueryDefs.Delete "Escalation Data"
End If
Next Qry

Set Qry = db.CreateQueryDef("Escalation Data", StrSQL)
Set Qry = Nothing
Set db = Nothing
'Output to Excel first worksheet
DoCmd.OutputTo acOutputQuery, "Escalation Data", acFormatXLS, , False
'Output to Excel second worksheet
StrSQL = "TRANSFORM Count(EscalationTable.EscalationID) AS
CountOfEscalationID"
StrSQL = StrSQL + "SELECT EscalationReasons.EscalationReason,
Count(EscalationTable.EscalationID) AS [TOTALS BY REASON]"
StrSQL = StrSQL + "FROM EscalationReasons INNER JOIN (Region INNER JOIN
EscalationTable ON Region.RegionID = EscalationTable.RegionID) ON
EscalationReasons.EscalationReasonID = EscalationTable.EscalationReasonID"
StrSQL = StrSQL + "WHERE (EscalationTable.ErrorByID=1) AND
EscalationTable." + X
StrSQL = StrSQL + "GROUP BY EscalationReasons.EscalationReason"
StrSQL = StrSQL + "PIVOT Region.RegionName"
'**************This part doesn't work***********************
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, StrSQL,
"C:\Documents and Settings\simurdan\Desktop\Escalation Data.xls", , "HEFC
Opportunities by Market"
'Output to Excel third worksheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"q-ExcelExportBankerOpportunity", "C:\Documents and
Settings\simurdan\Desktop\Escalation Data.xls", , "Banker Opportunities by
Market"
'**********************************************************
exit_cmdExcelSummary_Click:
On Error Resume Next
DoCmd.SetWarnings False
DoCmd.DeleteObject acQuery, "Escalation Data"
DoCmd.SetWarnings True
Exit Sub

Err_cmdExcelSummary_Click:
MsgBox Err.Description
Resume exit_cmdExcelSummary_Click
End Sub

Deki PA



Tom Wickerath said:
Hi Deki,
If I leave parametars in queries that add worksheets, user has
to input same from/to dates 3 times.

Why would they? Can't you use the same logic for your two additional
queries, ie. where the query criteria is filled in by dates entered on your
form, like this:

WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value])



Thank You too for marking my response as an answer.

Tom
__________________________________________

:

Thank you Tom and Nikos, that was helpfull.
However, I'm running a query that creates excel spreadsheet and two
additonal worksheets in it (total of 3) and everything is based on same
from/to date. If I leave parametars in queries that add worksheets, user has
to input same from/to dates 3 times. Is it possible for those 2 queries to
pick up value from form, so user can just select from/to dated from DTPickers
on form?
--
Deki PA
__________________________________________

:

Hi Deki,

Please see my article:
How To Create A Crosstab Query
http://www.access.qbuilt.com/html/crosstab_queries.html

Pay particular attention to paragraph 15.

If my answer has helped you, please answer yes to the question that reads
"Did this post answer the question?" at the bottom of the message thread.


Tom

http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

:

I have a query:

TRANSFORM Count(EscalationTable.EscalationID) AS CountOfEscalationID
SELECT EscalationReasons.EscalationReason,
Count(EscalationTable.EscalationID) AS [TOTALS BY REASON]
FROM EscalationReasons INNER JOIN (Region INNER JOIN EscalationTable ON
Region.RegionID = EscalationTable.RegionID) ON
EscalationReasons.EscalationReasonID = EscalationTable.EscalationReasonID
WHERE (((EscalationTable.EscalationDate) Between
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] And
[Forms]![frmSelectSummaryExcelReport]![dtpTo].[value]) AND
((EscalationTable.ErrorByID) Between 3 And 8))
GROUP BY EscalationReasons.EscalationReason, EscalationTable.ErrorByID
PIVOT Region.RegionName;

This query is called from VBA code:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"q-ExcelExportHEFCOpportunity", "C:\Documents and
Settings\UserName\Desktop\Escalation Data.xls", , "HEFC Opportunities by
Market"

Whe query is run, I get a Error message that Jet Engine does not recognize
[Forms]![frmSelectSummaryExcelReport]![dtpFrom].[Value] as a field name or
expression.

What am I doing wrong here?
 
G

Guest

Hi Deki,

You should be able to use a string variable in DoCmd.TransferSpreadsheet.
Perhaps your strSQL was not formed correctly. After you have created your
strSQL string in code, use a debug.print statement to print the results to
the immediate window:

Debug.Print strSQL

Run the code. Hit Ctrl G to display the immediate window. Does the query
look like it is formed correctly (ie. not missing spaces between key words,
etc.)? Copy the SQL statement to your clipboard, by selecting it and pressing
Ctrl C. Open a new query and dismiss the table dialog without selecting any
tables. In query design view, click on View > SQL View. Paste your new SQL
statement that you copied from the immediate window into the SQL view. Can
you run the query successfully?

The DoCmd.TransferSpreadsheet will not replace an existing spreadsheet if
you are outputting a new query name. Instead, it will add a new worksheet to
an existing spreadsheet.

Tom
________________________________________

:

I realized some syntax errors in query and fixed them. Now queries work and,
I guess, I can't pass string variable in DoCmd.TransferSpreadsheet. Is there
any other way to add worksheets without having to create temporary tables or
queries?
 
G

Guest

I've done all that already, and query works fine as query, but gives me the
same error message when I use it in DoCmd.TransferSpreadsheet.
 
G

Guest

Deki,

If you can send me a zipped copy of your database, I will take a look at it
for you.

Tom
(e-mail address removed) <---Remove all capitolized letters.
____________________________________

:

I've done all that already, and query works fine as query, but gives me the
same error message when I use it in DoCmd.TransferSpreadsheet.
 
G

Guest

Thank you Tom for all your help.
I read help on DoCmd.TransferSpreadsheet and it says that only query name or
table name can be passed as a parametar, so I did the same thing like with
first query, created temporary query in code and deleted it after and it
works just fine.
Thank you anyway!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top