Access Dlookup Multiple Criteria Error

AndrewKent

Well-known Member
Joined
Jul 26, 2006
Messages
889
Hi there,

I have the following Access query that I have put into Excel:

Code:
    If strQueryType = "Download_Headcount_WL5" Then
        strQuery = "SELECT [ENMatrix_Forenames] & ' ' & [ENMatrix_Surname] AS WL5, tblData_ChangeLog.CL_Week AS WEEK, DLookUp('[DateMatrix_Period]','[tblMatrix_DateMatrix]','[tblMatrix_DateMatrix]![DateMatrix_Week] = 19' And '[tblMatrix_DateMatrix]![DateMatrix_Year]='2011-2012'') AS PERIOD, tblData_ChangeLog.CL_Year AS [YEAR], Count(tblData_ChangeLog.CL_EmployeeNumber) AS [TOTAL VOLUME], Sum(tblData_ChangeLog.CL_FTE) AS [TOTAL FTE] " _
        & "FROM tblDefinitions_OverallArea RIGHT JOIN (tblMatrix_EmployeeNumbers RIGHT JOIN (tblDefinitions_CCNumbers RIGHT JOIN tblData_ChangeLog ON tblDefinitions_CCNumbers.CCData_OracleCCNumber = tblData_ChangeLog.CL_OracleCCNumber) ON tblMatrix_EmployeeNumbers.ENMatrix_EmployeeNumber = tblDefinitions_CCNumbers.CCData_WL5Head) ON tblDefinitions_OverallArea.OAD_ExecArea = tblDefinitions_CCNumbers.CCData_ExecArea " _
        & "GROUP BY [ENMatrix_Forenames] & ' ' & [ENMatrix_Surname], tblData_ChangeLog.CL_Week, DLookUp('[DateMatrix_Period]','[tblMatrix_DateMatrix]','[tblMatrix_DateMatrix]![DateMatrix_Week] = 19' And '[tblMatrix_DateMatrix]![DateMatrix_Year]='2011-2012''), tblData_ChangeLog.CL_Year, tblData_ChangeLog.CL_CurrentStatus " _
        & "HAVING (((tblData_ChangeLog.CL_Week) = 19) And ((tblData_ChangeLog.CL_Year) = '2011-2012') And ((tblData_ChangeLog.CL_CurrentStatus) = 'STAFF')) " _
        & "ORDER BY [ENMatrix_Forenames] & ' ' & [ENMatrix_Surname];"
        Exit Sub
    End If

When I run the query in Access it works fine, however when I copy and paste the SQL into Excel I get the error message:

Syntax error (missing operator) in query expression

DLookUp('[DateMatrix_Period]','[tblMatrix_DateMatrix]','[tblMatrix_DateMatrix]![DateMatrix_Week] = 19' And '[tblMatrix_DateMatrix]![DateMatrix_Year]='2011-2012'')

Can anyone help me figure out why this works in Access but not when I copy it into Excel?

Andy
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I think the quotes are the issue. If you used a combo of " and ', I think it would work.
I'd try
DLookUp("[DateMatrix_Period]","[tblMatrix_DateMatrix]","[tblMatrix_DateMatrix]![DateMatrix_Week] = '19' And [tblMatrix_DateMatrix]![DateMatrix_Year]='2011-2012'")
 
Upvote 0
Is [DateMatrix_Week] = 19 suppose to be numeric or text?
 
Upvote 0
in VBA you must change all " to '. It doesn't seem to allow the use of " in a SQL?
You change all double quotes for criteria but not around the field names nor the table/queryname. It is EXACTLY as jackd has said. That is the correct way to do it in VBA.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top