boxes stay empty after UPDATE sql query in ms-access

69 views Asked by At

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:

updating rows

However the fields in the column stay empty. I don't understand why?

1

There are 1 answers

0
Etienne On BEST ANSWER

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.