访问查询参数,该参数使用单独的表来消除或包括记录



我有一个表格,这是所有交易的主会计表。我有第二个桌子,付款,我会跟踪付款,特别是给定付款的发票已得到回报。

我需要一个动态和灵活的报告;真的是我的会计主力。我需要的报告将在日期,客户ID,记录状态,帐户1,帐户2和付款状态之间进行搜索。

因此,我有一个用于处理这些输入的表单,但是我在"付费状态"中遇到麻烦,例如如何与之形成参数。我当前的代码

SELECT Main.Invo, Main.InvoDate, Main.Amt, Main.PartyId, Main.TboInvoRloc, Main.TboDocNo, Main.TboPax
FROM Main
WHERE Main.RecSrce<>"Accounts"
AND Main.InvoDate BETWEEN [Forms]![GeneralReport]![startDate] AND [Forms]![GeneralReport]![endDate]
AND Main.PartyId =IIF([Forms]![GeneralReport]![PartyID] IS NULL, PartyID, [Forms]![GeneralReport]![PartyID])
AND Main.Status = IIF([Forms]![GeneralReport]![Status] IS NULL, Status, [Forms]![GeneralReport]![Status])
AND Main.Ac1 = IIF([Forms]![GeneralReport]![Ac1] IS NULL, Ac1, [Forms]![GeneralReport]![Ac1])
AND Main.Ac2 = IIF([Forms]![GeneralReport]![Ac2] IS NULL, Ac2, [Forms]![GeneralReport]![Ac2])
;

除了"薪酬状态"之外,还涵盖了所有内容。我想做

之类的事情
If checkbox=true, then include paid items, else exclude items where Main.Invo = Payments.DueInvo

我的另一个想法是将付费项目排除在初始查询中,然后在检查复选框时将它们包含在随后的联合查询中。任何帮助或想法都将不胜感激。

设置tripleastate的未结合复选框是。或使用3个选项(付费,未付款,全部(的Combobox,因为用户可能会被三重状态复选框混淆。

在付款表上构建一个总查询,该查询按发票总计付款。将该查询加入Main(假设发票在Main中是唯一的(。

计算一个字段并在该字段下设置一个参数。

如果使用复选框:
IsNotPaid: Amt <> Nz(SumOfPayments,0)
LIKE Forms![GeneralReport]!checkboxname & "*"

如果使用combobox:
PaidStatus: IIf(Amt = Nz(SumOfPayments,0), "Paid", "Unpaid")
LIKE IIf(Forms![GeneralReport]!comboboxname = "All", "*", Forms![GeneralReport]!comboboxname)

聚合查询的一种替代方案是dsum((域聚集函数,但可能会执行较慢。

考虑将付款详细信息列设置为NULL,具体取决于复选框。具体来说,加入付款表,并在分配NULLSELECT中的字段上运行逻辑条件。

SELECT m.Invo, m.InvoDate, m.Amt,
       m.PartyId, m.TboInvoRloc, 
       m.TboDocNo, m.TboPax,
       IIF(Forms]![GeneralReport]![myCheckBox] = True, 
           p.PaymentDetailColumn1, NULL) AS PayColumn1,
       IIF(Forms]![GeneralReport]![myCheckBox] = True, 
           p.PaymentDetailColumn2, NULL) AS PayColumn2,
       IIF(Forms]![GeneralReport]![myCheckBox] = True, 
           p.PaymentDetailColumne, NULL) AS PayColumn3
       ...
FROM Main m
LEFT JOIN Payments p ON m.Invo = p.DueInvo
WHERE m.RecSrce <> 'Accounts'   
  AND m.InvoDate BETWEEN [Forms]![GeneralReport]![startDate] 
                     AND [Forms]![GeneralReport]![endDate]
  AND m.PartyId = NZ([Forms]![GeneralReport]![PartyID], m.PartyID)
  AND m.Status = NZ([Forms]![GeneralReport]![Status], m.Status)
  AND m.Ac1 = NZ([Forms]![GeneralReport]![Ac1], m.Ac1)
  AND m.Ac2 = NZ([Forms]![GeneralReport]![Ac2], m.Ac2);

如果您需要动态列(即付款详细信息(出现或不取决于条件,则不能直接来自SQL,而是一些连接应用程序层代码(VBA,Python等(来构建动态查询。召回SQL是一种声明性的语言,一旦分配了标识符,它们就不可能。但是,如果使用报告,则您 do 想要从RecordsOurce查询明确定义的所有列。

最新更新