Hi Guys, so so close with this, there are 2 things, i cant appear to loop through rs3 recordset where I am trying to count the items delivered between the years (LYearStart And LYearEnd) based on combo
the strSQL2 source is copied from a query SQL view that does display the results, they appear in the immediate window with iDelQty being the same number due to not looping correctly, i cant remember how to do multiple loops !!!!
Also i am trying work the calculation our for Price per item ?
strSQL is getting data from expenses
strSQL2 is getting data from assign (delivered items)
Code:
strSQL = "SELECT tblExpenses.ItemRequired, tblExpenses.Year, tblExpenses.MonthNo, Sum(tblExpenses.TotalAmount) AS SumOfTotalAmount, Sum(tblExpenses.Litres) AS SumOfLitres, " _ & "Avg(tblExpenses.PricePerLitre) AS AvgOfPricePerLitre " _
& "From tblExpenses " _
& "GROUP BY tblExpenses.ItemRequired, tblExpenses.Year, tblExpenses.MonthNo " _
& "HAVING (((tblExpenses.ItemRequired) = '" & strItem & "') AND ((tblExpenses.Year) Between " & lYearStart & " And " & lYearEnd & ")) " _
& "ORDER BY Avg(tblExpenses.PricePerLitre) DESC;"
strSQL2 = "SELECT tblAssign.Year, tblAssign.Month, Count(tblAssign.ItemType) AS CountOfItemType, tblAssign.Source " _
& "From tblAssign " _
& "GROUP BY tblAssign.Year, tblAssign.Month, tblAssign.Source " _
& "HAVING (((tblAssign.Year) Between " & lYearStart & " And " & lYearEnd & ") AND ((tblAssign.Source) = '" & strSrc & "')) " _
& "ORDER BY tblAssign.Year DESC , tblAssign.Month DESC;"
Set rs3 = CurrentDb.OpenRecordset(strSQL2)
Do While Not rs3.EOF
iDelQty = rs3("CountOfItemType")
rs3.MoveNext
Loop
strHTML = "<HTML><Body><table border='3' width='70%'><tr><th>Item Required</th><th>Month-Year</th>" & _
"<th>Total Fuel Cost</th><th>Total Litres</th><th>Avg Price Per Litre</th><th>Total Items Delivered</th><th>Fuel Cost Per Item</th></tr>"
strMailBody = strHTML
Set rs = CurrentDb.OpenRecordset(strSQL)
Do While Not rs.EOF
lMonthNo = rs.Fields("MonthNo")
iDelQty = DCount("*", "tblAssign", "[Year] Between " & lYearStart & " And " & lYearEnd) ' & " And [MonthNo] = " & lMonthNo & " And [Source] = '" & strSrc & "'")
strMailBody = strMailBody & "<tr><td>" & rs("ItemRequired") & "</td><td>" & _
MonthName(rs("MonthNo")) & " - " & rs("Year") & "</td><td>" & Format(rs("SumOfTotalAmount"), "Currency") & "</td><td>" & _
rs("SumOfLitres") & "</td><td>" & Format(rs("AvgOfPRicePerLitre"), "Currency") & "</td><td>" & _
iDelQty & "</td><td>" & iDelQty / rs("SumOfTotalAmount") & "</td></tr>" & "|"
rs.MoveNext
Loop
Debug.Print strSQL2
iDelQty is not looping
My 2nd mind boggle is iDelQty / rs("SumOfTotalAmount") I think i am not calculating correctly
How do i calculate How much fuel per single item and not the sum of all items
example: Ideally delivery qty is 600 then total fuel cost is 1200, fuel cost per item is what i am trying to return
Not pasted full code here as its large
Any help to retrieve the correct iDelQty would be very kind