How to filter a crosstab query?. (1 Viewer)

ebs17

Well-known member
Local time
Today, 19:44
Joined
Feb 7, 2020
Messages
1,949
Variant 3: Use of a parameter table
SQL:
SELECT D.*
FROM tblData AS D, ptblParameters AS P
WHERE D.Anydate = P.parDate
The parameter table is introduced via CROSS JOIN. However, since it usually only has one record, no unwanted data duplication takes place.
You just have to reset the value in the table before calling the query.
Advantage here too: This can also be used in deeper query levels.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:44
Joined
Feb 19, 2002
Messages
43,308
We are at post #23. I told you in #6 that you needed to declare the parameters. Several others have mentioned it. You didn't post SQL that included the Parm definition. June just posted a link that will help if you couldn't figure out how to declare the parameters.
 

ebs17

Well-known member
Local time
Today, 19:44
Joined
Feb 7, 2020
Messages
1,949
Variant 4. Dynamic SQL (composing the SQL string using VBA)
Code:
sSQL = "TRANSFORM ..." & _
   " WHERE P.PDATE = " & Format(Forms.AnyForm.txtAnyDate, "\#yyyy\-mm\-dd\#") & _
   " ..."

Variant 5: Replacing a wildcard from the template query
Code:
csSQL = "TRANSFORM ..." & _
   " WHERE P.PDATE = [$1Date]" & _
   " ..."
sSQL = Replace(csSQL, "$1Date", Format(Forms.AnyForm.txtAnyDate, "\#yyyy\-mm\-dd\#"))
 
Last edited:

ebs17

Well-known member
Local time
Today, 19:44
Joined
Feb 7, 2020
Messages
1,949
Form references and TempVar have problems as parameter passers when they are used at a deeper level (subquery). I don't know whether this changes through the parameter listing. I don't check something like that, as I said, for style reasons.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:44
Joined
Feb 19, 2002
Messages
43,308
Form references and TempVar have problems as parameter passers when they are used at a deeper level (subquery)
I use them all the time. But, I have no doubt that some Access version or other might have a problem
 

ebs17

Well-known member
Local time
Today, 19:44
Joined
Feb 7, 2020
Messages
1,949
TempVars are only available with Acc2007. Otherwise nothing has changed regarding queries in Access since Acc2000, so I see no reason for a version dependency.
Did you notice the word subquery?
 

moi

Member
Local time
Tomorrow, 01:44
Joined
Jan 10, 2024
Messages
212
I inserted the SQL string from one query into the crosstab query. Passing parameters to ONE object is clearer.

To pass parameters, I have chosen a public function as a variant. This function takes the (validated) Date value from a form text field.
Code:
Public Function fctparDate() As Date
   fctparDate = Forms.AnyForm.txtAnyDate
End Function
SQL:
TRANSFORM
   SUM(Q.AMOUNTPAID) AS SumOfAMOUNTPAID
SELECT
   Q.BUYERID,
   B.byrFName,
   B.byrLName,
   B.propID,
   B.propBlk,
   B.propLot,
   B.propSQM,
   Q.PDATE,
   Q.ARNO,
   Q.BankName,
   Q.AccNo,
   Q.AccName,
   SUM(Q.AMOUNTPAID) AS [Total Of AMOUNTPAID]
FROM
   tblBuyer AS B
      INNER JOIN
         (
            SELECT
               P.PAYID,
               P.BUYERID,
               P.PDATE,
               P.ARNO,
               P.AMOUNTPAID,
               P.MOPID,
               M.MOP,
               M.BankName,
               M.AccNo,
               M.AccName,
               P.REFTRANSNMBR,
               P.REMARKS
            FROM
               tblMOP AS M
                  INNER JOIN tblPayment AS P
                  ON M.MopID = P.MOPID
            WHERE
               P.PDATE = fctparDate()
         ) AS Q
         ON B.byrID = Q.BUYERID
GROUP BY
   Q.BUYERID,
   B.byrFName,
   B.byrLName,
   B.propID,
   B.propBlk,
   B.propLot,
   B.propSQM,
   Q.PDATE,
   Q.ARNO,
   Q.BankName,
   Q.AccNo,
   Q.AccName
PIVOT
   Q.MOPID

There are at least three other variants.
Hi ebs17,

Thank you for helping me to my query and crosstab.. I pasted your sql statement into the sql design view of my crosstab, then I copied your fctparDate function into module, and save as "fctparDate"..but when open/run my crosstab, I got the attached msg..

Can you please guide me on how to use your sql and function.. I am new to advance queries/crosstab pls need more help.

Thank you.
 

Attachments

  • Screenshot (86).png
    Screenshot (86).png
    28.1 KB · Views: 14

moi

Member
Local time
Tomorrow, 01:44
Joined
Jan 10, 2024
Messages
212
So where in that sql have you assigned the parameter - see post #5

I would have expected to see something like
Code:
PARAMETERS [Enter Date] Date;
SELECT tblPayment.PAYID, tblPayment.BUYERID, tblPayment.PDATE, tblPayment.ARNO, tblPayment.AMOUNTPAID, tblPayment.MOPID, tblMOP.MOP,
...
...
Hi CJ_London,

Can you please expand the above, I am new to advance queries/crosstab, I can do a simple query as of now, please need more help..

Thank you.
 

June7

AWF VIP
Local time
Today, 09:44
Joined
Mar 9, 2014
Messages
5,477
Did you read Allen Browne article linked in post 21?

Exactly what do you not understand?

Build crosstab query - use the build wizard if that helps. Modify design - open Parameters dialog with button on ribbon.
 

moi

Member
Local time
Tomorrow, 01:44
Joined
Jan 10, 2024
Messages
212
Did you read Allen Browne article linked in post 21?

Exactly what do you not understand?

Build crosstab query - use the build wizard if that helps. Modify design - open Parameters dialog with button on ribbon.
Yes.. i did read it.. the problem i am struggling to get work is the date range filter..
 

June7

AWF VIP
Local time
Today, 09:44
Joined
Mar 9, 2014
Messages
5,477
So you tried Parameters? Exactly what issue did you encounter - error message, wrong result, nothing happens? Post your attempted SQL as text, not image. Are you outside U.S.A. using non-US date?
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 18:44
Joined
Sep 21, 2011
Messages
14,326
Hi ebs17,

Thank you for helping me to my query and crosstab.. I pasted your sql statement into the sql design view of my crosstab, then I copied your fctparDate function into module, and save as "fctparDate"..but when open/run my crosstab, I got the attached msg..

Can you please guide me on how to use your sql and function.. I am new to advance queries/crosstab pls need more help.

Thank you.
You do not name modules the same as sub or function names.
 

ebs17

Well-known member
Local time
Today, 19:44
Joined
Feb 7, 2020
Messages
1,949
I copied your fctparDate function into module
It must be a standard module, not a form's module. And of course the form being accessed must also be open at this moment and contain a valid value.
Additionally, @Gasman is right again.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:44
Joined
Feb 19, 2013
Messages
16,618
Can you please expand the above,
not sure how much easier it could be - in the query window on the ribbon
1712735661889.png


Here there are two parameters specified - the first provides a prompt for the user to enter a date, the second to a control on a form (which as others have said must be open at the time the query is run - and populated).

This will duly add the required code to the sql

PARAMETERS [Enter Date] DateTime, [ [forms]]![myform]![mycontrol] DateTime;
SELECT .......


Note this is greyed out in the SQL window but you can of course add the line if required.
 

June7

AWF VIP
Local time
Today, 09:44
Joined
Mar 9, 2014
Messages
5,477
Advise not to use popup input parameters as cannot validate input. Refer to control on form.
 

Users who are viewing this thread

Top Bottom