Results 1 to 13 of 13
  1. #1
    B00592816 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Posts
    6

    MS Access Compile Error

    I am a newbie to MS Access and vba coding. When running a module a Microsoft Visual Basic message box appears telling me Compile error: Syntax Error.



    Can someone please help me with the following piece of code that I have written and tell me what is wrong with it?

    Set rst = CurrentDb.OpenRecordset("SELECT * FROM tblAppointments WHERE " _
    & "DateValue(ApptStart) <= #" & Format(vDate, "yyyy/m/d") & "# AND " _
    & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND ApptLocation = "RSU1" & ORDER BY ApptStart") 'fetch appts for selected day

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    One way to pin it down is to put your SELECT in a string variable first and then display it either with a MSGBOX or just with debug.

  3. #3
    B00592816 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Posts
    6
    Thanks for your help. I removed the AND ApptLocation = "RSU1" and the code it seems to work however I need this information in my code. Have I written that specific part properly?

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It looks you don't quite have your double-quotes correct at the end of your statement, specifically, this part:
    Code:
     & "# & AND ApptLocation = "RSU1" & ORDER BY ApptStart"
    Note that to get literal double-quotes in the statement itself, you cannot do it that way. People often double/triple up the double-quotes to get them to appear. I find that kind of messy, and choose to use Chr(34) instead, which returns literal double quotes, i.e.
    Code:
    ... "# & AND ApptLocation = " & Chr(34) & "RSU1" & Chr(34) & ...
    Note, here is a little trick you may want to use. Create your SQL statement as a string. Then you can use a MsgBox to inspect it before you try to run it to easily check for errors, i.e.
    Code:
    Dim strSQL as String
    strSQL = "SELECT * FROM ..."
    
    'Temporary message box to inspect building of SQL string
    MsgBox strSQL
    
    Set rst = CurrentDb.OpenRecordset(strSQL)

    Edit: I see ruralguy posted the same MsgBox suggestion too. That's what happens when one is a real slow typist and people respond in the time it takes me to type!

  5. #5
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    First guess,

    .
    .
    & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND ApptLocation = ""RSU1"" & ORDER BY ApptStart") 'fetch appts for selected day

  6. #6
    B00592816 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Posts
    6
    I tried the above code and I am told there is a syntax error (missing operator) in query expression.

    Set rst = CurrentDb.OpenRecordset("SELECT * FROM tblAppointments WHERE " _
    & "DateValue(ApptStart) <= #" & Format(vDate, "yyyy/m/d") & "# AND " _
    & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND ApptLocation = ""RSU1"" & ORDER BY ApptStart") 'fetch appts for selected day

  7. #7
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    I broke the code into manageable piece.

    Dim tmpStr As String

    tmpStr = "SELECT * FROM tblAppointments WHERE "
    tmpStr = tmpStr & "DateValue(ApptStart) <= #" & Format(vDate, "yyyy/m/d") & "# AND "
    tmpStr = tmpStr & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND "
    tmpStr = tmpStr & "ApptLocation = ""RSU1""" & " ORDER BY ApptStart"

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Did you try the MsgBox suggestions RuralGuy and I proposed to see if your syntax looks correct after building it?

  9. #9
    B00592816 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Posts
    6
    Can anyone identify what the missing operator is?

    Set rst = CurrentDb.OpenRecordset("SELECT * FROM tblAppointments WHERE " _
    & "DateValue(ApptStart) <= #" & Format(vDate, "yyyy/m/d") & "# AND " _
    & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND ApptLocation = ""RSU1"" & ORDER BY ApptStart") 'fetch appts for selected day

  10. #10
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    "ApptLocation = ""RSU1""" & " ORDER BY ApptStart"

    & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND ApptLocation = ""RSU1""" & " ORDER BY ApptStart") 'fetch appts for selected day

    or you can just substitue what I have in post #7

    Dim tmpStr As String

    tmpStr = "SELECT * FROM tblAppointments WHERE "
    tmpStr = tmpStr & "DateValue(ApptStart) <= #" & Format(vDate, "yyyy/m/d") & "# AND "
    tmpStr = tmpStr & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND "
    tmpStr = tmpStr & "ApptLocation = ""RSU1""" & " ORDER BY ApptStart"

    Set rst = CurrentDb.OpenRecordset(tmpStr)

  11. #11
    B00592816 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Posts
    6
    I tried substituting what you had in post#7 and I am still being told that there is a syntax error (missing operator) in query expression.

  12. #12
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    Sorry, my mistake on this string
    tmpStr = tmpStr & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# & AND "

    Correction:

    tmpStr = "SELECT * FROM tblAppointments WHERE "
    tmpStr = tmpStr & "DateValue(ApptStart) <= #" & Format(vDate, "yyyy/m/d") & "# AND "
    tmpStr = tmpStr & "DateValue(ApptEnd) >= #" & Format(vDate, "yyyy/m/d") & "# AND "
    tmpStr = tmpStr & "ApptLocation = ""RSU1""" & " ORDER BY ApptStart"

    If you use MsgBox on the tmpStr, it should return
    SELECT * FROM tblAppointments WHERE DateValue(ApptStart) <= #2014/6/28# AND DateValue(ApptEnd) >= #2014/6/28# AND ApptLocation = "RSU1" ORDER BY ApptStart

    If the select looks right, then the code should work.

  13. #13
    B00592816 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Posts
    6
    That did the trick, thank you. I will mark the thread as solved.

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

Similar Threads

  1. Compile Error: Syntax Error in DoCmd.RunSQL Statement
    By Evilferret in forum Programming
    Replies: 1
    Last Post: 08-27-2012, 12:32 PM
  2. Replies: 2
    Last Post: 06-23-2012, 11:59 PM
  3. Replies: 7
    Last Post: 06-08-2012, 09:55 PM
  4. Replies: 7
    Last Post: 11-17-2011, 02:56 PM
  5. Replies: 6
    Last Post: 09-28-2011, 09:20 PM

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