我在Access 2010中有一个支出子表单,其中列出了与每年项目相关的预测成本。大多数项目只有一年,但有些项目不止一个。每个成本旁边都有一个Final复选框,应该在确认金额时进行检查,即。在每年年底。
它基本上看起来像这样:
Year | Cost | Final
--------+-----------+--------------------
2017 | $100 | [checked box]
2018 | $200 | [unchecked box]
| | [unchecked box]
我在桌子外面有另一个字段FinalCost,它将Cost字段中的所有内容相加。现在,它填写了任何一年有一个已检查的最终框的金额。当 all 选中最终框时,应仅填充。
实施例。现在,即使选中2017年的Final,它也应该没有显示任何内容。选中2018后,它应显示300美元。相反,它显示100美元,即使它仍然是一个空的复选框。
这是此表单的代码。
Private Sub Form_AfterUpdate()
Dim rs1, rs2 As Recordset
Dim sql, sql2 As String
sql = "SELECT Sum(Amount) as Final From Expenditures " & _
"Where ProjNo = '" + Me.ProjNo + "' And Final = True Group by ProjNo"
sql2 = "SELECT FinalExpenditure From ActivityCash " & _
"Where ProjNo = '" + Me.ProjNo + "'"
Set rs1 = CurrentDb.OpenRecordset(sql, dbOpenDynaset, dpinconsistent)
Set rs2 = CurrentDb.OpenRecordset(sql2, dbOpenDynaset, dpinconsistent)
If rs1.RecordCount > 0 Then
If rs2.RecordCount > 0 Then
Do While Not rs2.EOF
rs2.Edit
rs2!FinalExpenditure = rs1!Final
rs2.Update
rs2.MoveNext
Loop
End If
End If
rs2.Close
rs1.Close
Set rs1 = Nothing
Set rs2 = Nothing
End Sub
这样做最好的方法是什么?
编辑:当选中最后一个框时,会自动添加一个新行,其中包含一个未锁定的复选框,但没有信息。
答案 0 :(得分:1)
因此,在sql
之前,我会验证所有记录在True
字段中是否有Final
。
要做到这一点,我们只需返回COUNT()
个Final = False
个Dim Test as Integer
test = DCount("*", "YourTableName", "Final = False AND ProjNo = " & Me.ProjNo &"")
If test > 0 Then
'Don't fill the box
Else
'Fill the box, everything is True
'Read through your recordsets or whatever else you need to do
End If
的记录,然后我们就可以决定做我们想做的事了。
所以,像,
Dcount()
要使用查询,我们基本上需要复制Recordset
功能。
为此,我们需要另一个Count()
变量,我们需要从查询中检查SELECT COUNT(*) As CountTest
FROM YourTable
HAVING Final = False
AND ProjNo = whateverprojectnumberyou'reusing
字段的值。
创建一个模仿这个的查询:
DCount()
保存,并记住该查询的名称。
与Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("YourQuery'sNameHere")
If rst!CountTest > 0 Then
'They are not all Checked (aka True)
Else
'Supply the value to the FinalCost
End If
Set rst = Nothing
非常相似,我们需要做出这个"检查"确定代码的路由。
{
"type":"page",
"ancestors":
[
{
"type":"page",
"id":12355342
}
],
"title":"Page Title 1",
"space":
{
"key":"DAT"
},
"body":
{
"storage":
{
"value":"<ac:structured-macro ac:name=\"attachments\">
<ac:parameter ac:name=\"old\">false<\/ac:parameter> <ac:parameter ac:name=\"patterns\">*<\/ac:parameter>
<ac:parameter ac:name=\"sortBy\">name<\/ac:parameter> <ac:parameter ac:name=\"sortOrder\">ascending<\/ac:parameter>
<ac:parameter ac:name=\"labels\"><\/ac:parameter> <ac:parameter ac:name=\"upload\">false<\/ac:parameter> <\/ac:structured-macro>",
"representation":"storage"
}
}
}
答案 1 :(得分:1)
改变这个:
sql = "SELECT Sum(Amount) as Final From Expenditures " & _
"Where ProjNo = '" + Me.ProjNo + "' And Final = True Group by ProjNo"
为此:
"SELECT SUM(Amount) - SUM(IIF(Final,1,0)*Amount) as YetToConfirm, SUM(Amount) as Confirmed From Expenditures " & _
"Where ProjNo = '" + Me.ProjNo + "' Group by ProjNo"
rs1 将返回两个值,即rs1!Confirmed
中确认所有费用的总值,以及rs1!YetToConfirm
然后在这里:
Do While Not rs2.EOF
rs2.Edit
rs2!FinalExpenditure = rs1!Final
rs2.Update
rs2.MoveNext
Loop
将其更改为:
Do While Not rs2.EOF
rs2.Edit
rs2!FinalExpenditure = Iif(rs1!YetToConfirm = 0, rs1!Confirmed, 0)
rs2.Update
rs2.MoveNext
Loop
答案 2 :(得分:1)
将以sql = ...
开头的语句替换为:
sql = "SELECT SUM(e1.Amount) AS Final " & _
" FROM Expenditures AS e1 " & _
" WHERE NOT EXISTS (SELECT 'x' FROM Expenditures e2 WHERE e2.Final=0 AND e1.ProjNo = e2.ProjNo) " & _
" AND e1.ProjNo = '" & Me.ProjNo & "'"
仅当项目的所有项目都标记为最终时,此查询才会返回数据。检查rs1.RecordCount > 0
时,如果此查询未返回任何记录,则不会更新。
答案 3 :(得分:1)
处理此问题的一种方法是使用子查询检查是否在最后一列中检查了每个项目中的去年(使用dmax函数验证),如果这是真的,则获取已检查金额的总和,否则不计算总和。
我已经修改了你的sql字符串以包含这个,我根据你给出的例子对它进行了测试,以确认它显示的是300美元或者什么都没有。
SQL = ""
SQL = SQL & " SELECT Sum(Amount) as Final From Expenditures "
SQL = SQL & " Where ProjNo = '" & Me.ProjNo & "' And Final = True "
SQL = SQL & " And (SELECT Expenditures.Final FROM Expenditures where year = ( "
SQL = SQL & " DMax('Year','Expenditures','ProjNo= " & Chr(34) & Me.ProjNo & Chr(34) & "'))) = true "
SQL = SQL & " Group by ProjNo "