Results 1 to 4 of 4
  1. #1
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43

    Syntax error in SQL string?

    Hello, all!



    The purpose of what I'm working on is to be able to audit machine preventative maintenance logs. People fill out a form saying they performed certain tasks maintaining a machine, and later someone else needs to see if these tasks were done and when. The process I am having problems with involves programmatically identifying weekends and holidays out of a range of dates.

    The below code works fine (will label weekend dates as such or leave the field blank with the Comment variable) until I un-comment the if/then statement that includes "New Year's Day". At that point I get an error '3075' "Syntax error (missing operator) in query expression "New Year's Day')', and the debugger hilights the DoCmd.RunSQL strSQL2 line of code. I'm thinking there's a formatting/syntax error in the strSQL statement (probably at the end). I'm horrible with single/double quotation marks so I'm currently staring blankly at them.

    Code:
    Private Sub Command4_Click()
    
    Dim MyDate, MyWeekday, MyYear
    Dim StartDayOfYear As Integer
    Dim EndDayOfYear As Integer
    Dim StartDayEndDayDifference As Integer
    Dim DayOfYear As Integer
    Dim DateToCheck As Date
    Dim Comment As String
    
    
    Dim strSQL As String
    Dim strSQL2 As String
    
    
    
    
    MyDate = Date 'Establishes MyDate as the current system date
    MyYear = year(MyDate) 'Returns the 4 digit value of the current year
    StartDayOfYear = DatePart("y", StartDateTextBox) 'Returns the numeric day-of-year value of the StartDateTextBox control
    EndDayOfYear = DatePart("y", EndDateTextBox) 'Returns the numeric day-of-year value of the EndDateTextBox control
    StartDayEndDayDifference = DateDiff("d", StartDateTextBox, EndDateTextBox) 'Calculates the difference, in days, between StartDateTextBox and EndDateTextBox
    DateToCheck = DateSerial(MyYear, 1, StartDayOfYear) 'Finds the traditional date expression that represents the numeric day-of-the-year value of the StartDayOfYear variable
    'MyWeekday = Weekday(DateToCheck) 'Returns the numeric value where Sunday = 1 and so on until Saturday = 7 and assigns that value to the Myweekday variable
    
    
    strSQL = "DELETE FROM MissingDatesTable;"
    
    
    
    
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    
    
    For x = StartDayOfYear To EndDayOfYear
        MyWeekday = Weekday(DateToCheck)
        DayOfYear = DatePart("y", DateToCheck)
        If MyWeekday = 1 Or MyWeekday = 7 Then
            Comment = "Weekend"
        Else
            Comment = ""
        End If
        
        'If DayOfYear = 1 Then
        '    Comment = "New Year's Day"
        'Else
        '    Comment = ""
        'End If
        
        If DCount("[ID]", "DoosanPMLogData", "[PerformedOn] = #" & DateToCheck & "#") = 0 Then
            strSQL2 = "INSERT INTO MissingDatesTable (MissingDates, Comment) VALUES ('" & DateToCheck & "', '" & Comment & "')"
            MsgBox strSQL2
            DoCmd.SetWarnings False
            DoCmd.RunSQL strSQL2
            DoCmd.SetWarnings True
        End If
        Comment = ""
        DateToCheck = DateToCheck + 1
    Next x
    
    
    Me.MissingDatesListBox.Requery


    Any help would be greatly appreciated!


    End Sub

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Here's your problem
    Code:
    Comment = "New Year's Day"
    ...
    strSQL2 = "INSERT INTO MissingDatesTable (MissingDates, Comment) VALUES ('" & DateToCheck & "', '" & Comment & "')"
    You need to escape the single quote in the New Year's Day string OR use double quotes in the sql statement instead of the single quotes.

    Try this instead:
    Code:
    strSQL2 = "INSERT INTO MissingDatesTable (MissingDates, Comment) VALUES ('" & DateToCheck & "', """ & Comment & """)"
    [EDIT]
    Also, when you're using dates in a ms access sql you surround them with # rather than quotes
    Code:
    strSQL2 = "INSERT INTO MissingDatesTable (MissingDates, Comment) VALUES (#" & DateToCheck & "#, """ & Comment & """)"

  3. #3
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    kd2017;

    Worked like a charm, thank you!

    I really should work more at this rather than avoid it or I'll never be comfortable with it. Also, good tip on the pound signs in SQL queries as well. I'm used to using them in domian and Access queries, but never occurred to me to use them in SQL queries as well.

    Thank you!

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by smaier View Post
    kd2017;

    Worked like a charm, thank you!
    Happy to help!

    Also, good tip on the pound signs in SQL queries as well. I'm used to using them in domian and Access queries, but never occurred to me to use them in SQL queries as well.
    Note that the # notation is specific to microsoft access's flavor of sql.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 01-05-2019, 11:17 AM
  2. Replies: 11
    Last Post: 05-01-2014, 11:56 AM
  3. Dim As String syntax help
    By Kirsti in forum Programming
    Replies: 11
    Last Post: 07-29-2013, 05:29 PM
  4. MySQL Syntax Error from Query String in VBA
    By raynman1972 in forum Programming
    Replies: 2
    Last Post: 09-17-2012, 05:59 PM
  5. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums