当所有复选框都切换时填写字段 Access 2010
Fill Field When All Checkboxes Toggled Access 2010
我在 Access 2010 中有一个支出子表单,其中列出了每年与项目相关的预测成本。大多数项目只有一年,但有些项目不止一年。每个成本旁边都有一个最终复选框,在确认金额时应选中该复选框,即。每年年底。
基本上是这样的:
Year | Cost | Final
--------+-----------+--------------------
2017 | 0 | [checked box]
2018 | 0 | [unchecked box]
| | [unchecked box]
我在 table 之外还有另一个字段 FinalCost,它将成本字段中的所有内容相加。现在,它填写了选中最终框的任何年份的金额。当所有检查最终框时,应该填充 。
例如。现在,即使选中了 Final for 2017,它也应该什么都不显示。选中 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
执行此操作的最佳方法是什么?
编辑:选中最后一个框时,会自动添加一个新行,其中未选中复选框但没有信息。
因此,在 sql
之前,我会验证 所有 记录在您的 Final
字段中有 True
。
为此,我们只需 return 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()
,我们需要让这个"check"确定你代码的路由。
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
改变这个:
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会return两个值,如果所有费用都在rs1!Confirmed
里确认的总值,还有在[=里还未确认的值13=]
然后这里:
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
将以 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 & "'"
只有当项目的所有费用都标记为最终时,此查询才会 return 数据。当您检查 rs1.RecordCount > 0
时,如果此查询 return 没有记录,则不会有任何更新。
处理此问题的一种方法是使用子查询检查是否在最后一列中检查了每个项目的去年(使用 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 "
我在 Access 2010 中有一个支出子表单,其中列出了每年与项目相关的预测成本。大多数项目只有一年,但有些项目不止一年。每个成本旁边都有一个最终复选框,在确认金额时应选中该复选框,即。每年年底。
基本上是这样的:
Year | Cost | Final
--------+-----------+--------------------
2017 | 0 | [checked box]
2018 | 0 | [unchecked box]
| | [unchecked box]
我在 table 之外还有另一个字段 FinalCost,它将成本字段中的所有内容相加。现在,它填写了选中最终框的任何年份的金额。当所有检查最终框时,应该填充 。
例如。现在,即使选中了 Final for 2017,它也应该什么都不显示。选中 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
执行此操作的最佳方法是什么?
编辑:选中最后一个框时,会自动添加一个新行,其中未选中复选框但没有信息。
因此,在 sql
之前,我会验证 所有 记录在您的 Final
字段中有 True
。
为此,我们只需 return 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()
,我们需要让这个"check"确定你代码的路由。
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
改变这个:
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会return两个值,如果所有费用都在rs1!Confirmed
里确认的总值,还有在[=里还未确认的值13=]
然后这里:
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
将以 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 & "'"
只有当项目的所有费用都标记为最终时,此查询才会 return 数据。当您检查 rs1.RecordCount > 0
时,如果此查询 return 没有记录,则不会有任何更新。
处理此问题的一种方法是使用子查询检查是否在最后一列中检查了每个项目的去年(使用 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 "