当只有一条记录时,此表达式给出正确的计算,但是当有多条记录时,计算的最后一条记录的值会反映在所有Total_Time
(未绑定的文本框)中。我已经加载了以及报告的公开代码。请帮帮我。
Private Sub Report_Load()
strSQL = "SELECT * FROM [q_1ltduty]"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
rs.MoveFirst
Do While Not rs.EOF
'Assigning values of fields to varia
strtime1 = Op_Time
strtime2 = Cl_Time
'This is a simple expression my code has some more detailed calculations
strhrs = strtime2 - strtime1
strtotalhrs = strhrs
'Printing the variable in Total_Time textbox(unbound)
Me.Total_Time.Value = strtotalhrs
rs.MoveNext
Loop
rs.Close
db.Close
Set db = Nothing
Set rs = Nothing
End Sub
Private Sub Report_Open(Cancel As Integer)
strSQL = "SELECT * FROM [q_1ltduty]"
Me.RecordSource = strSQL
Debug.Print strSQL
Exit Sub
ErrHandler:
MsgBox Err.Description
End Sub
答案 0 :(得分:0)
您的变量strtotalhrs
仅包含一条记录的strhrs
值,因此每次进行循环时,当前记录的值都会删除上一条记录的值。你应该做的不是删除它,而是要添加它。
在循环之前(如果尚未完成):
strtotalhrs = 0
然后在循环中,而不是strtotalhrs = strhrs
:
strtotalhrs = strtotalhrs + strhrs
这应该给你总和而不是最后一个值。