當只有一條記錄時,這個表達式給出了正確的計算,但是當有多條記錄時,計算的最後一條記錄的值反映在所有的Total_Time
(未綁定的文本框)中。我已經給出了加載和打開代碼那個報告。請幫幫我。用於MS Access報表的VBA代碼中的表達式。
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
所以你的問題是......?我不明白你在問什麼。請說明你需要什麼 – Barranka 2013-04-06 16:38:48
每個記錄的值不會改變,文本框只顯示最後的記錄值。 – user2252564 2013-04-07 12:27:28
好吧,你的代碼被定義的方式,正是它應該是。如果你想要所有記錄的* total *(sum),那麼你必須在循環中創建一個「運行總和」:'strtotalhrs = strtotalhrs + strhrs' – Barranka 2013-04-07 20:15:33