So I'm using a VBA generated query to insert a bunch of records from an Excel spreadsheet into an Access Database.
My query basically looks like this:
accName = r.Offset(x, 0)
AccNum = r.Offset(x, 1)
sector = r.Offset(x, 2)
holding = r.Offset(x, 3)
holdingvalue = r.Offset(x, 4)
holdingdate = CDate(r.Offset(x, 5))
sSQL = "INSERT INTO Holdings (AccName, AccNum, Sector, Holding, HoldingValue, HoldingDate)" & _
" VALUES ('" & Replace(accName, "'", "''") & "', '" & AccNum & "', '" & sector & "', '" & Replace(holding, "'", "''") & "', '" & holdingvalue & "', #" & holdingdate & "#)"
An example of the generated query looks like this:
INSERT INTO Holdings (AccName, AccNum, Sector, Holding, HoldingValue, HoldingDate)
VALUES ('Account 123', '472700', '', 'IShares S&P 500', '54379.15', #03/12/2012#)
The wrongly-functioning bit is that the date in the query has gone from a UK date (dd/mm/yyyy) to a US date (mm/dd/yyyy). I have tried also formatting the date on the spreadsheet as an ISO date (yyyy-mm-dd) but is always goes as a US date, even when I don't use cdate. Any idea what's going on here?
Edit: When I step through the code and hover on the holdingdate variable it says "03/12/2012" and the query is the same as above (ie with the date in dd/mm/yyy format, despite me defining holdingdate as holdingdate = Format(r.Offset(x, 5), "yyyy-mm-dd"). The dates in the cells are also serials (i.e. 41246 when displayed as a number).
When I look in the access database after the data has been input the date is shown as 12/03/2012. I just don't understand....
CDate(Format(r.Offset(x,5), "dd/mm/yyyy")Might have something to do with your excel settings too. - Bmoholdingdate = Format(CDate(r.Offset(x, 5).Value), "dd mmm yyyy")holdingdate is still returned as 03/12/2012. The format function seems to do nothing - harryg