Thanks, when I get the code to work I will deal with putting the records in a order that I want. The data contains information from several different cars in our fleet, so order will be an important issue, later. I have had message boxes throughout earlier versions of code, these messages should that the code is looping through, the mileage entered was being read and manipulated, but instead of inserting it into the next record, it just restarts the loop from the top over and over again. I know I am missing something, just can see it. Thanks for the info on the code box, did not know that.Tables are unordered and you have not ordered the recordset so it may be different from what you expect.
Also you don't appear to be reading any values from the recordet.
BTW Please post code in a code box. This will allow you to indent it too.
Open the post in the Advanced Editor, select the code and hit the # button.
Private Sub Command149_Click()
Dim dbs As DAO.Database
Dim newKM As DAO.Recordset
Dim strSQL As String
Dim OldMileage As Double
Dim CurrentMileage As Double
Dim Dif As Double
Dim Record As Double
Dim intRet As Integer
Dim strMsg As String
Dim Msg, Style, Response, Mystring
Set dbs = CurrentDb
strSQL = "SELECT CarMileage.Date, CarMileage.Litres, CarMileage.[Price/L], CarMileage.[Bill Cost], CarMileage.[Car Name], CarMileage.Mileage, CarMileage.Currency_ID, CarMileage.[Cal Cost], CarMileage.KM, CarMileage.[KM/L], CarMileage.[KM/Cost], CarMileage.[Cents/KM] FROM CarMileage ORDER BY CarMileage.[Car Name], CarMileage.Mileage;"
Set newKM = dbs.OpenRecordset(strSQL)
OldMileage = 0
Record = 0
With newKM
If .EOF Then
GoTo ErrorHandler:
Else
strMsg = "Processing Mileage ..."
intRet = SysCmd(acSysCmdInitMeter, strMsg, 100)
End If
Do While Not .EOF
CurrentMileage = [Mileage]
Dif = OldMileage - CurrentMileage
MsgBox (CurrentMileage & ", " & OldMileage & ", " & Dif & ", " & CurrentRecord)
If Dif > 0 Then
.Edit
!KM = Dif
.Update
OldMileage = Mileage
MsgBox (CurrentMileage & OldMileage & Dif)
Else
Dif = 0
OldMileage = 0
End If
If .PercentPosition <> 0 Then
intRet = SysCmd(acSysCmdUpdateMeter, .PercentPosition)
End If
Record = Record + 1
.MoveNext
Loop
End With
MsgBox (Record)
ErrorHandler:
intRet = SysCmd(acSysCmdRemoveMeter)
newKM.Close
End Sub
Next time, please do NOT do that. It makes it difficult for anyone to see what the original problem was and then how to fix it. Remember, the forums are not just for the original poster's benefit. They are there for others who may be searching for a solution.Code was changed according to suggestions so far. So I have deleted the original post to save screen space.
Thanks for your help. The "!" on the front of the field made a difference. Not sure how, but it did. Got some reading to do, I guess. Regarding the comment "One of [Litres], [Price/L] and [Bill Cost] should be removed." Yes [Bill Cost] is not really needed, but it was a test field for the code. Yes the 4 fields are derived, but there is a reason for the this. The big boss wants this in excel, it is a lot easier to export a database table. Also if it works (and seems to have) it will not be just mileage but also all cost. Repairs and more. For this reason the Car name and Currency is a separate table. This way I can easily create another table for repairs and I will not be doubling up the values.[Mileage] is an unqualified reference to an object. Access will take a guess (usually) starting with the name of a control on the form belonging to the module.
If you want to refer to the recordset yo must use:
recordsetname!fieldname
or if using the With Block:
With recordsetname
!fieldname
End With
However you need to start by attending to your data structure which is denormalized for severaL reasons.
Recording derived values. One of [Litres], [Price/L] and [Bill Cost] should be removed. The last four fields are all derived and represent different versions of the same information.
Storing the CarName in this table rather than an ID as a key to a Cars table.
Storing currency as text rather than an ID from a Currency table.
(BTW Special characters and spaces should not be used in field names.)
I would use a query to make the mileage calculation and would not store the results.
Thanks for your help. The "!" on the front of the field made a difference. Not sure how, but it did. Got some reading to do, I guess.
Regarding the comment "One of [Litres], [Price/L] and [Bill Cost] should be removed." Yes [Bill Cost] is not really needed, but it was a test field for the code.
Yes the 4 fields are derived, but there is a reason for the this. The big boss wants this in excel, it is a lot easier to export a database table.