Hi there.
I'm trying to write vba code to loop through a table and add to the total wherever a certain condition is met, based on the form input. Here is the code:
I don't see anything wrong with the code, but it doesn't work. Anybody with the idea of what am I missing?
I'm trying to write vba code to loop through a table and add to the total wherever a certain condition is met, based on the form input. Here is the code:
Code:
'Assign date values(form input) into variables for easy manipulation
Dim StartDate As Date, EndDate As Date
StartDate = Me.datStart.Value
EndDate = Me.datEnd.Value
'Create recordsets for storing database objects
Dim myRevenueSet As DAO.Recordset
'Declare variable to store amounts
Dim curRevenueSum As Currency
curRevenueSum = 0
'Create SQL statement to be used during looping
Dim myRevenueSQL As String
myRevenueSQL = "SELECT datRevenue, curRevenueAmount FROM tblRevenue;"
Set myRevenueSet = DBEngine(0)(0).OpenRecordset(myRevenueSQL, dbOpenDynaset)
'Start to loop in a table
Do While Not myRevenueSet.EOF
'Check to see if the current record is within date range and if so add its value to a total
If DateDiff("d", StartDate, myRevenueSet!datRevenue) <= 0 And DateDiff("d", EndDate, myRevenueSet!datRevenue) >= 0 Then
curRevenueSum = curRevenueSum + myRevenueSet!curRevenueAmount
End If
'Move to the next record within the table
myRevenueSet.MoveNext
Loop
'Assign totals in specified textbox in the form
Me.curSales.Value = curRevenueSum
'Reset the recordset object
myRevenueSet.Close
Set myRevenueSet = Nothing
Comment