0

I think I've got a syntax error with my query I cant find the error. I get the error at the "1/1/1990". Anyone know hat i'm doing wrong here?

  Set rsCanCounter = db.OpenRecordset("Select * from tblActionLog Where groupNum = '" & txtGroupNr.Value & "' And DateClosed < '" & 1/1/1900, dbOpenDynaset)
2
  • The syntax highlighting in the question shows mis-matched quotes. Pay close attention to your single-quotes and double-quotes. This query is never even going to reach the database because the code won't compile like this. Commented Jan 29, 2015 at 18:40
  • Sorry! I was trying to fix it the way David suggested. The quotes are confusing me. Commented Jan 29, 2015 at 18:49

1 Answer 1

2

Use a string variable to hold your SELECT statement. After you overcome the quoting issues so you can create a valid string in VBA, then use that variable with OpenRecordset.

For example, if the datatypes of your groupNum and DateClosed fields are both text ...

Dim strSelect As String
strSelect = "Select * from tblActionLog Where groupNum ='" & _
    Me.txtGroupNr.Value & "' And DateClosed < '1/1/1900'"
Debug.Print strSelect '<-- Ctrl+g to go to Immediate window and see the statement text
Set rsCanCounter = db.OpenRecordset(strSelect, dbOpenDynaset)

If DateClosed is Date/Time datatype, use # instead of quotes to delimit the date value:

strSelect = "Select * from tblActionLog Where groupNum ='" & _
    Me.txtGroupNr.Value & "' And DateClosed < #1900-1-1#"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help! "Select * from tblActionLog Where groupNum ='" & Me.txtGroupNr.Value & "' And DateClosed < #'1-1-1900# And DateClosed is not Null" I had to add the null exception. is this correct. Am I doing the quotations wrong again
What? DateClosed can not be both < 1/1/1900 and Null at the same time. Those conditions contradict each other. That WHERE clause should not return any rows.
Sorry I fix it. Actually the comment got added before I was finished with it : /
Aha! :-) Well, with the condition DateClosed < #'1-1-1900#, that makes DateClosed is not Null superfluous. To see if the latest version of the statement is correct, run your code, go to the Immediate window (Ctrl+g), copy the statement text, open a new query in the query designer, switch it to SQL View, paste in the statement text. Does it work when you then try to run it from the query designer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.