Problem looking up a value in a query

Posted on 2013-10-22
Last Modified: 2013-10-22
Hello Experts
I am having a bit of a problem getting a query to work.  I am looking for a list of records where the Renewal Date value is within a certain number of weeks.
I have the query working fine with a hardcoded value for weeks.  When I add a lookup for the value I get:
Syntax error (missing operator) in query expression 'Membership warning weeks'

This is the query that I am trying:

SELECT tblClients.[Client ID], tblClients.RenewalDate, tblClients.NextRenewalDate, [tblClients.Title] & " " & [tblClients.Firstname] & " " & [tblClients.Lastname] & " - " & [tblClients.Address 1] & " " & [tblClients.Address 2] & " " & [tblClients.Town] AS ClientFullnameAddress
FROM tblClients
WHERE (((tblClients.RenewalDate)<DateAdd("w",DLookUp("Membership warning weeks","tblParameters"),Date())) AND ((tblClients.NextRenewalDate) Is Null));

Hope someone can give me a clue.


Question by:rltomalin
LVL 61

Accepted Solution

mbizup earned 300 total points
ID: 39590670
Since the field name has spaces in it, try using square brackets around it:

SELECT tblClients.[Client ID], tblClients.RenewalDate, tblClients.NextRenewalDate, [tblClients.Title] & " " & [tblClients.Firstname] & " " & [tblClients.Lastname] & " - " & [tblClients.Address 1] & " " & [tblClients.Address 2] & " " & [tblClients.Town] AS ClientFullnameAddress
FROM tblClients
WHERE (((tblClients.RenewalDate)<DateAdd("w",DLookUp("[Membership warning weeks]","tblParameters"),Date())) AND ((tblClients.NextRenewalDate) Is Null));

Open in new window

LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 200 total points
ID: 39590690
Are you doing this in code or in the query designer.  If code, should be:

"WHERE tblClients.RenewalDate) < #" & Format(DateAdd("w",DLookUp('Membership warning weeks','tblParameters'),Date()),'mm/dd/yy') & "# AND tblClients.NextRenewalDate Is Null;"

If the query designer, define a column as:

DateAdd("w",DLookUp("Membership warning weeks","tblParameters"),Date())

and then put the criteria on it.

As an aside, you really should not be using a Dlookup() inside a SQL statement.   THe domain functions were intended to be used in places where SQL statements are not allowed.  

Using one will always give you poor performance on your query as the query parser cannot optimize the execution.


Author Closing Comment

ID: 39590753
I used mbizup's solution and it worked fine.  I had actually moved ahead when the second solution came in, but gave it some points because I am sure it would have been OK and the expert took the trouble to give some advice.



Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question