typemismatch error in vba code

swarv

Registered User.
Local time
Today, 16:57
Joined
Dec 2, 2008
Messages
196
Hi all,

I have the following code:
Code:
LTotal3 = DCount("[Name]", "absent", "[Name] = " & Chr(34) & Combo136.Value & Chr(34) & " And [absentcode]='countme'" And [Start_Date] > ((DateAdd("m", -12, Date))))
LTotal4 = DSum("[Days]", "absent", "[Name] = " & Chr(34) & Combo136.Value & Chr(34) & " And [absentcode]='countme'" And ((DateAdd("m", -12, Date)) < [Start_Date]))

In the first line it comes back with type mismatch error.
Is there a quick reason for this?

Notes:
absent is a table name
Start_Date is a field name

I think the error is with the bit that goes:
And [Start_Date] > ((DateAdd("m", -12, Date)))

Cheers
 
this is the problem

And [Start_Date] > ((DateAdd("m", -12, Date))))

with dates the string needs to look like this, when its finished

[start_date] > #12/2/09#

so

"And [Start_Date] > #" & ((DateAdd("m", -12, Date))) & #"

so you need to structure the statement to include the # characters.

note also that the date in this case will ALWAYS be treated as a US date (ie Dec 2nd), so if you are in UK you need to add further formatting instructions to prevent this (although I am not 100% sure with the way you are manipulating the date, as it stands)
 
hi gemma,


thanks for your answer,

I now have the following:

Code:
LTotal6 = DSum("[Days]", "absent", "[Name] = " & Chr(34) & Combo136.Value & Chr(34) & " And [absentcode]='countme'" And [Start_Date] > "# & ((DateAdd("m", -12, Date))) & #"

it is erroring on the m just after the DataAdd.
I have changed the " and #'s around.

Any ideas?

Thanks again
 
I have even tried this:

Code:
LTotal6 = DSum("[Days]", "absent", "[Name] = " & Chr(34) & Combo136.Value & Chr(34) & " And [absentcode]='countme'" And "Start_Date >#" & DateAdd("m", -12, Date) & "#")

cheers
 
I got round the DCount line by running this is a query and then calling it with the Dcount function
Code:
SELECT *
FROM absent
WHERE (DateAdd("m",-12,Date())) <  (((absent.Start_Date)));

But I cant do that with the DSum function so any ideas are welcome as im getting stuck


cheers
 
can somebody help or can i use dsum in the way as i did that query above? cheers
 

Users who are viewing this thread

Back
Top Bottom