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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateDiff and SQL Statement

Status
Not open for further replies.

JDU

Technical User
Dec 23, 2002
182
US
I have an input box (nbtInputBox) whose value I want to include in an SQL statement. It seems that the value is being evaluated properly from the inputbox; however, the value from the field in the table is not (see immediate window result below) Help.

strGetFireSafety = "Select * from tblCompetencyData where txtLogin = " & Chr(34) & rsGetName!txtLogin & Chr(34) _
& " and txtProcedureNo = 'MAN-001'" _
& " and datediff('yyyy',tblCompetencyData!nbtEvalDate," & nbtInputBox & ") = 0"

Set rsGetFireSafety = db.OpenRecordset(strGetFireSafety)

Immediate Window:
?strGetFireSafety
Select * from tblCompetencyData where txtLogin = "CB415" and txtProcedureNo = 'MAN-001' and datediff('yyyy',tblCompetencyData!nbtEvalDate,1/1/00) = 0
 
You need to surround the last argument (date) with #:
try this as the last line:
& " and datediff('yyyy',tblCompetencyData!nbtEvalDate,#" & & nbtInputBox & "#) = 0"
 
I thought we had this one. What is "nbtInputBox" ? Is it a field in the table, or a variable in your program ?

 
Thank you so much, that seemed to fix the problem.

I didn't even think to put # in front of the variable nbtInputBox because I had defined it as a date variable. Can somebody explain why even when you define a variable as date, you still need to enclose the variable in # #.

Thanks
 
That defines a variable as a date to the SQL processor, just like the ' or " defines a string to it. If it knows it is a date, it can call built in functions to do date arithmetic on the value.
 
I understand what you are saying. What I mean is this; in my declarations I declared

Dim nbtInputBox as Date

Why then the need for #nbtInputBox# in the SQL statement.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top