我正在使用查询中的数据填充Access中的TempTable。到目前为止,我已经填充了3列(添加,删除,购买),我试图找到其中三列的总和。事实是,很多人没有价值观,其中一些是负面价值观。我怎么去addnig他们?我有rs2(“添加”),rs2(“删除”)和rs2(“购买”)中的数据。因此所有ADD值(如果有正数),所有DELETE都是负数(如果有的话),所有买入都是正数。
我想进入一个循环......
我把记录数作为intRsCount = 25
With rs2
While Not .EOF
lngCompanyID = rs2("CompanyID")
lngUnitPrice = rs2("UnitPrice")
strSQL2 = "SELECT AddQty from qryAddx WHERE InvValue = " & lngUnitPrice & " And CompanyId = " & lngCompanyId
strSQL3 = "SELECT DeleteQty from qryUsedx where UnitValue=" & lngUnitPrice & " And CompanyId = " & lngCompanyId
strSQL4 = "SELECT BuyQty from qryVoidx where InvValue =" & lngUnitPrice & " And CompanyId = " & lngCompanyId
Set rs3 = CurrentDb.OpenRecordset(strSQL2)
Set rs4 = CurrentDb.OpenRecordset(strSQL3)
Set rs5 = CurrentDb.OpenRecordset(strSQL4)
If rs3.recordCount > 0 Then
rs2.Edit
rs2("Added") = rs3("Addqty")
rs2.Update
End If
If rs4.recordCount > 0 Then
rs2.Edit
rs2("Delete") = rs4("DelQty")
rs2.Update
End If
If rs5.recordCount > 0 Then
rs2.Edit
rs2("Buy") = rs5("BuyQty")
rs2.Update
End If
rs2.MoveNext
Set rs3 = Nothing
Set rs4 = Nothing
Set rs5 = Nothing
Wend
End With
这是我的代码。我正在尝试添加ADDED,DELETED和BUY的SUM并将其放在rs2(“Subtotal”)中并对所有25行执行此操作。我不是很熟悉ACCESS以及它如何处理NULL值和负值(所有DELETE值都是负数)。感谢。
答案 0 :(得分:2)
Dim strCriteria as String
With rs2
While Not .EOF
strCriteria = "InvValue = " & !UnitPrice & " And CompanyId = " & !CompanyID
.Edit
!Added = Nz(DSum("Addqty", "qryAddx", strCriteria), 0)
!Delete = Nz(DSum("DeleteQty", "qryUsedx", strCriteria), 0)
!Buy = Nz(DSum("BuyQty", "qryVoidx", strCriteria), 0)
.Update
Wend
End With
DSum()
效率低下。如果你正在运行许多操作,它可能会很慢,但除此之外它很好。 Nz()
将Null值转换为零。答案 1 :(得分:0)
如果rs2是记录集,您可以参考如下列:rs2.fields("Add").Value
循环遍历行,使用
if rs2.recordcount > 0 then
rs2.movefirst
while not rs2.eof
'put your commands here
wend
end if