SQL Multiple WHERE Statements with Date Variables Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JaketheSnake27
    New Member
    • Sep 2010
    • 6

    SQL Multiple WHERE Statements with Date Variables Problem

    I'm trying to run a query using SQL in VBA. I want the query to only show data that matches 3 criteria. I'm using a WHERE statement with 2 AND's and my database doesn't seem to like that. I am also using date variables (startdate and enddate) in my SQL statement. My query code is below:

    Code:
    Set employeeOEE = db.OpenRecordset("SELECT [Query-OEE].[Machine Type], [Query-OEE].[Date], [Query-OEE].[Employee Name] FROM [Query-OEE] WHERE [Query-OEE].[Machine Type]= " & departtype & " AND [Query-OEE].[Date] BETWEEN " & startdate & " AND " & enddate & " AND [Query-OEE].[Employee Name]= " & empname & ";", dbOpenDynaset)
    The error I'm getting is "Run-Time Error 3075" Syntax error (missing operator) in Query expression. [Query-OEE].[Machine Type]= " & departtype & " AND [Query-OEE].[Date] BETWEEN " & startdate & " AND " & enddate & " AND [Query-OEE].[Employee Name]= " & empname & ";", dbOpenDynaset)

    If I take out '"[Query-OEE].[Date] BETWEEN " & startdate & " AND " & enddate & "' I don't get an error. This is my first time using a date variable (which are entered in a form before this part of the code) so I may not have the syntax right on that portion.

    I'm sure I just have something minor messed up. Can somebody help me out please?
  • liimra
    New Member
    • Aug 2010
    • 119

    #2
    Solution/

    Try this
    Code:
    Between #" & [startdate] & "# And #" & [endDate] & "#"
    Regards,
    Ali

    Comment

    • JaketheSnake27
      New Member
      • Sep 2010
      • 6

      #3
      That was it! Thank you!

      Comment

      • liimra
        New Member
        • Aug 2010
        • 119

        #4
        You are most welcome.

        Regards,
        Ali

        Comment

        Working...