Dlookup syntax and datatype

B

Bonnie

I am getting a datatype mismatch error on the statement
below. The datatypes are text for both [StudentNo] and
[YearEnd] but the [SuspensionDate] is a date field. I
have tried to change the quotes and variable types but end
up with syntax errors. Can datatypes be mixed in dlookups

varSuspensionDate = DLookup
("[SuspensionDate]", "tblSuspension", "[StudentNo] = " &
strStudentNo & _
" And " & "[YearEnd] = " & strYear & " And "
& "[SuspensionDate] = " & strTxtSuspensionDate)
 
J

John Vinson

I am getting a datatype mismatch error on the statement
below. The datatypes are text for both [StudentNo] and
[YearEnd] but the [SuspensionDate] is a date field. I
have tried to change the quotes and variable types but end
up with syntax errors. Can datatypes be mixed in dlookups

varSuspensionDate = DLookup
("[SuspensionDate]", "tblSuspension", "[StudentNo] = " &
strStudentNo & _
" And " & "[YearEnd] = " & strYear & " And "
& "[SuspensionDate] = " & strTxtSuspensionDate)

You do need either ' or " delimiters for the Text fields, and # as a
delimiter for the Date/Time field. Try:

varSuspensionDate = DLookup
("[SuspensionDate]", "tblSuspension",
"[StudentNo] = '" & strStudentNo & _
"' And [YearEnd] = '" & strYear & _
"' And [SuspensionDate] = #" & strTxtSuspensionDate & "#")

Note that also don't need to pull each little word out as a separate
string constant; you want the final string to contain (say)

And [SuspensionDate] = #3/18/2003#

but the " And [SuspensionDate] = #" portion doesn't change so it can
all be one string constant.
 
A

Arni Laugdal

Dear Bonnie,

You must have # around the date (if you store the date as
text must you format the string as valid date within the
##):
varSuspensionDate = DLookup
("[SuspensionDate]", "tblSuspension", "[StudentNo] = " &
strStudentNo & _
" And [YearEnd] = " & strYear & " And [SuspensionDate] =
#" & strTxtSuspensionDate & "#")

If you store the StudentNo as string then must you set ''
around it. Same if YearEnd is stored as string as I have
done in following example:
varSuspensionDate = DLookup
("[SuspensionDate]", "tblSuspension", "[StudentNo] = '" &
strStudentNo & _
"' And [YearEnd] = '" & strYear & "' And [SuspensionDate]
= #" & strTxtSuspensionDate & "#")

Enjoy!
Arni Laugdal, MMI
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top