Question

I am trying to join a table to a query in MS Access. The field for the query is "LineoffMonth", and the field for the table is "Production Month". LineoffMonth looks like this:

LineoffMonth: CDate(Month([Line_off_Date]) & "/1/" & Year([Line_off_Date]))

Production Month is a date/time data type. I keep getting "Data type mismatch error in expression". I do not understand why it will not let me join the two fields.

Was it helpful?

Solution

You will get that error when [Line_off_Date] is Null as this Immediate window session illustrates.

Line_off_Date = Null
? Month(Line_off_Date) & "/1/" & Year(Line_off_Date)
/1/
? CDate("/1/") ' throws Type mismatch (Error 13)

You can update the table to replace Nulls in [Line_off_Date]. Or you can add a WHERE clause to exclude rows which have Null for [Line_off_Date]. Or you can use an IIf expression which returns the CDate expression when [Line_off_Date] is not Null, but something else (perhaps Null?) when [Line_off_Date] is Null.

As a side point, consider a DateSerial expression in place of what you were using.

Line_off_Date = Date()
? CDate(Month(Line_off_Date) & "/1/" & Year(Line_off_Date))
12/1/2013
? DateSerial(Year(Line_off_Date), Month(Line_off_Date), 1)
12/1/2013 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top