带有日期条件的交叉表查询

时间:2013-06-10 14:17:29

标签: ms-access ms-access-2007 ms-access-2010

我创建了一个交叉表查询,如下所示

TRANSFORM Nz(Sum([debit]*[GeneralExpenses]),0) AS mOmomeya
SELECT Expenses.sName, Expenses.ArName
FROM (GL INNER JOIN Expenses ON GL.ID = Expenses.glID) INNER JOIN Transactions ON GL.GL = Transactions.GL
GROUP BY Expenses.sName, Expenses.ArName
PIVOT Month([ddate]);

我想要做的是从表单文本输入中为[dDate]字段添加一个条件以匹配我输入的日期,但是我在执行此操作时收到错误

enter image description here

知道什么是错的?

2 个答案:

答案 0 :(得分:1)

嗯,关于交叉表查询肯定有点不同。直接的SELECT查询...

SELECT expenses.*
FROM expenses
WHERE (((expenses.ddate) Between [Forms]![main]![DateFrom] And [Forms]![main]![DateTo]));

...工作正常,但交叉表查询...

TRANSFORM Sum(expenses.expenses) AS SumOfexpenses
SELECT expenses.onduty
FROM expenses
WHERE (((expenses.ddate) Between [Forms]![main]![DateFrom] And [Forms]![main]![DateTo]))
GROUP BY expenses.onduty
PIVOT Month(ddate);

...因您引用的错误消息而失败。一种解决方法是在使用之前重建查询的SQL:

Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("yourCrosstab")
qdf.SQL = _
        "TRANSFORM Nz(Sum([debit]*[GeneralExpenses]),0) AS mOmomeya " & _
        "SELECT Expenses.sName, Expenses.ArName " & _
        "FROM (GL INNER JOIN Expenses ON GL.ID = Expenses.glID) INNER JOIN Transactions ON GL.GL = Transactions.GL " & _
        "WHERE (((ddate) " & _
            "Between #" & Format(CDate([Forms]![main]![DateFrom]), "yyyy-mm-dd") & "# " & _
            "And #" & Format(CDate([Forms]![main]![DateTo]), "yyyy-mm-dd") & "#)) " & _
        "GROUP BY Expenses.sName, Expenses.ArName " & _
        "PIVOT Month(ddate);"
Set qdf = Nothing
DoCmd.OpenQuery "yourCrosstab"  '' or whatever you want to do with it

答案 1 :(得分:0)

问题是交叉表查询需要每个表单的表单参数。右键单击灰色空间并选择参数。输入表单和数据类型。