I created the following query in MS ACCESS:
"UPDATE dbo_PricingFutures
SET dbo_PricingFutures.ttm =
DateDiff('m',dbo_PricingFutures.PricingDate,DateSerial(dbo_PricingFutures.PricingYear,dbo_PricingFutures.PricingMonth,14));"
When I run the query, which should calculate the difference between dates. It says it is going to update the records:
However the fields in the column stay empty. I don't understand why?
By a mistake, the column pricingdate was empty. I ran the following vba code:
Sub ttm()
Dim db As Database, rs As DAO.Recordset, strsql As String
Dim startdate As Date, maturity As Date, maturity_month As Double
Dim maturity_year As Double
Set db = CurrentDb()
strsql = "SELECT * FROM dbo_PricingFutures ORDER BY PricingDate;"
Set rs = db.OpenRecordset(strsql, dbOpenDynaset)
rs.MoveFirst
While (Not rs.EOF())
rs.Edit
startdate = rs.Fields("PricingDate")
maturity_month = rs.Fields("PricingMonth")
maturity_year = rs.Fields("PricingYear")
maturity = DateSerial(maturity_year, maturity_month, 14)
rs.Fields("ttm") = DateDiff("d", startdate, maturity)
rs.Update
rs.MoveNext
Wend
rs.Close
db.Close
End Sub
Then I got an error on the following line:
startdate = rs.Fields("PricingDate")
It showed that the column PricingDate was empty. Then, I took data from an old back up, copy/pasting in the column PricingDate and it worked perfectly.
The SQL code was running without showing error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.