Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a variable in a query/SQL statement

Status
Not open for further replies.

mtwildtrout

Technical User
Oct 1, 2003
23
US
I have a query that needs to select information based on a field entered by the user at the time of data entry. I receive the same error message if I use the query builder or format the SQL as a string. Can a variable be used in a query/SQL statement or is there another way to accomplish this? Here is the query in SQL:

SELECT TimeCard.WorkDate, TimeCard.EmployeeID
FROM TimeCard
WHERE (((TimeCard.WorkDate)>=Date()-Weekday(Date())+2
AND (TimeCard.WorkDate)<=Date()+(8-Weekday(Date()))))
AND ((TimeCard.EmployeeID) = X);

X is the variable I'm trying to use for the EmployeeID
 
The syntax is a little different depending on whether you're using this in a saved query or if it's a SQL string in code. In a query, it would look something like:

Code:
SELECT TimeCard.WorkDate, TimeCard.EmployeeID
FROM TimeCard
WHERE (((TimeCard.WorkDate)>=Date()-Weekday(Date())+2 
     AND (TimeCard.WorkDate)<=Date()+(8-Weekday(Date()))))
     AND ((TimeCard.EmployeeID) = [Forms]![frmMyForm]![fldEmployeeID]);

As a SQL string it would look something like:

Code:
&quot;SELECT TimeCard.WorkDate, TimeCard.EmployeeID &quot; _
& &quot;FROM TimeCard &quot; _
& &quot;WHERE (((TimeCard.WorkDate)>=Date()-Weekday(Date())+2 &quot; _
& &quot;AND (TimeCard.WorkDate)<=Date()+(8-Weekday(Date())))) &quot; _
& &quot;AND ((TimeCard.EmployeeID) = &quot; & Me![fldEmployeeID] & &quot;);&quot;

HTH...

Ken S.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top