I have the following code which is creating records into a table to 7 days in the future. The code gets todays date, checks the latest date on the table and creates the relevant records. I know that the price_history!date_entered field is defined as a short date but the highlighted line does not retrieve any records and I have no idea why, everything seems ok to me. p.s. I really, really hate date fields (not the first time I have had problems).
Thanks
Graham
Sub maintainpricehistory()
Dim myDB As Database
Dim rsPricehistoryread As Recordset
Dim rsPricehistoryone As Recordset
Dim rsPricehistoryadd As Recordset
Dim rsLastrun As Recordset
Dim vaCount As Long
Dim vaMydate As Date
Dim vaDateto As Date
Dim vaDate As Date
Set myDB = CurrentDb()
Set rsPricehistoryadd = myDB.OpenRecordset("price_history"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
vaMydate = Format(Date, "short date"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
vaDateto = vaMydate + 7
' check for multiple runs in a day
Set rsLastrun = myDB.OpenRecordset("SELECT * FROM [parameters]"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
If rsLastrun!pricehistory_date = vaMydate Then
GoTo endofproc
End If
'add records for 7 days in advance
' select highest date on table
Set rsPricehistoryone = myDB.OpenRecordset("SELECT * FROM [price_history] _ ORDER BY [date_entered] DESC ", dbOpenDynaset, dbReadOnly)
rsPricehistoryone.MoveFirst
' find difference between now and highest date
vaCount = DateDiff("d", rsPricehistoryone!Date_entered, vaDateto)
vaMydate = Format(rsPricehistoryone!Date_entered, "short date"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
' select all records for basis of new ones to be entered
Set rsPricehistoryread = myDB.OpenRecordset("SELECT * FROM [price_history] _ WHERE [date_entered] = " & vaMydate & "", dbOpenDynaset, dbReadOnly)
rsPricehistoryread.MoveLast ' populate recordset
rsPricehistoryread.MoveFirst
Do Until vaCount = 0
vaMydate = vaMydate + vaCount
Do Until rsPricehistoryread.EOF
rsPricehistoryadd.AddNew
rsPricehistoryadd!Site_id = rsPricehistoryread!Site_id
rsPricehistoryadd!Date_entered = Format(vaMydate, "DD/MM/YYYY"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
rsPricehistoryadd!Leaded_price = rsPricehistoryread!Leaded_price
rsPricehistoryadd!Unleaded_price = rsPricehistoryread!Unleaded_price
rsPricehistoryadd!super_price = rsPricehistoryread!super_price
rsPricehistoryadd!Derv_price = rsPricehistoryread!Derv_price
rsPricehistoryadd!type5_price = rsPricehistoryread!type5_price
rsPricehistoryadd!type6_price = rsPricehistoryread!type6_price
rsPricehistoryadd.Update
rsPricehistoryread.MoveNext
Loop
vaCount = vaCount - 1
rsPricehistoryread.MoveFirst
Loop
rsLastrun.Edit
rsLastrun!pricehistory_date = vaMydate
rsLastrun.Update
endofproc:
Set myDB = Nothing
Set rsPricehistoryread = Nothing
Set rsPricehistoryadd = Nothing
Set rsPricehistoryone = Nothing
Set rsLastrun = Nothing
End Sub
Thanks
Graham
Sub maintainpricehistory()
Dim myDB As Database
Dim rsPricehistoryread As Recordset
Dim rsPricehistoryone As Recordset
Dim rsPricehistoryadd As Recordset
Dim rsLastrun As Recordset
Dim vaCount As Long
Dim vaMydate As Date
Dim vaDateto As Date
Dim vaDate As Date
Set myDB = CurrentDb()
Set rsPricehistoryadd = myDB.OpenRecordset("price_history"
vaMydate = Format(Date, "short date"
vaDateto = vaMydate + 7
' check for multiple runs in a day
Set rsLastrun = myDB.OpenRecordset("SELECT * FROM [parameters]"
If rsLastrun!pricehistory_date = vaMydate Then
GoTo endofproc
End If
'add records for 7 days in advance
' select highest date on table
Set rsPricehistoryone = myDB.OpenRecordset("SELECT * FROM [price_history] _ ORDER BY [date_entered] DESC ", dbOpenDynaset, dbReadOnly)
rsPricehistoryone.MoveFirst
' find difference between now and highest date
vaCount = DateDiff("d", rsPricehistoryone!Date_entered, vaDateto)
vaMydate = Format(rsPricehistoryone!Date_entered, "short date"
' select all records for basis of new ones to be entered
Set rsPricehistoryread = myDB.OpenRecordset("SELECT * FROM [price_history] _ WHERE [date_entered] = " & vaMydate & "", dbOpenDynaset, dbReadOnly)
rsPricehistoryread.MoveLast ' populate recordset
rsPricehistoryread.MoveFirst
Do Until vaCount = 0
vaMydate = vaMydate + vaCount
Do Until rsPricehistoryread.EOF
rsPricehistoryadd.AddNew
rsPricehistoryadd!Site_id = rsPricehistoryread!Site_id
rsPricehistoryadd!Date_entered = Format(vaMydate, "DD/MM/YYYY"
rsPricehistoryadd!Leaded_price = rsPricehistoryread!Leaded_price
rsPricehistoryadd!Unleaded_price = rsPricehistoryread!Unleaded_price
rsPricehistoryadd!super_price = rsPricehistoryread!super_price
rsPricehistoryadd!Derv_price = rsPricehistoryread!Derv_price
rsPricehistoryadd!type5_price = rsPricehistoryread!type5_price
rsPricehistoryadd!type6_price = rsPricehistoryread!type6_price
rsPricehistoryadd.Update
rsPricehistoryread.MoveNext
Loop
vaCount = vaCount - 1
rsPricehistoryread.MoveFirst
Loop
rsLastrun.Edit
rsLastrun!pricehistory_date = vaMydate
rsLastrun.Update
endofproc:
Set myDB = Nothing
Set rsPricehistoryread = Nothing
Set rsPricehistoryadd = Nothing
Set rsPricehistoryone = Nothing
Set rsLastrun = Nothing
End Sub