Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Reginald is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    10

    Syntax error only shows when loading report

    Hello,
    I have a form with a list area and a button to load a report.


    Both of these use the same query and the result shows up in the list area fine, but when I click the button to load the report, it throws an error (3075) saying I have an error in the syntax.
    The query is:
    Code:
    "SELECT ID,description,location FROM assets WHERE  assets.sublocation like '*" & sublocation & "*'" & " And assets.[Date Removed] Is Null"
    Can anyone help?
    -Reginald

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    description is a reserved word - as a minimum, put it in square brackets but ideally change to something more meaningful (like assetDescription) - and get rid of spaces in your field names whilst you are at it -potentially another reason for unexplained errors

    if this does not solve the problem, I can't see anything wrong with your sql except you can simplify

    ....& sublocation & "*'" & " And assets....

    to

    ....& sublocation & "*' And assets....

    So the only other thing is your data - do you have sublocations with single quotes in them? e.g. "St. Jame's Place"

    if so use the replace function to replace single quotes with two single quotes

    ....& replace(sublocation,"'","''") & "*' And assets....

  3. #3
    Reginald is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    10
    Thanks for your quick response,
    Why does the query work for populating the listbox?
    I have no single quotes in any of my sublocations or in any other data as well.
    I tried to run with the square brackets around description but no joy.
    Also ran the query
    Code:
    "SELECT ID,[description],location FROM assets WHERE  assets.sublocation like '*" & sublocation & "*'"
    to remove Date Removed and still the same error shows.

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I've provided my best guess on the information provided so what exactly does your syntax error say?

    and have you used debug.print to generate the actual sql string and then copy pasted to the query sql window to see what errors it generates

  5. #5
    Reginald is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    10
    Click image for larger version. 

Name:	accesserror.jpg 
Views:	18 
Size:	36.3 KB 
ID:	24145
    This is a screenshot of the error

  6. #6
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    can you view it in the query grid - any clues there?

    try building a new query in the query grid and see how the sql compares

    otherwise, just leaves the report - what exactly are you doing there? perhaps sublocation does not exist in the report or has not been assigned a value

  7. #7
    Reginald is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    10
    I'm afraid I don't know what a query grid is.
    Also sublocation in query has the value
    ADO Kitchen
    Also I can't understand why the query works fine for populating the listbox?

  8. #8
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Take out the asset. in your SQL statement, you're only referencing one table so it's unnecessary.

    see if this works
    select id, description, location from assets where sublocation like '*ado kitch*' and [date removed] is null

    it did for me, if it does then start substituting in your variables until it breaks and tell us where it bombs out.

  9. #9
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    query grid - the form that opens when you open a new or existing query

    if it works fine for populating the listbox, then as previous suggested this implies an issue with your report

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    ohhhhh.... snap.... I misread... again... if you're opening a report using a field from a form as a criteria your syntax would be


    "SELECT ID,description,location FROM assets WHERE assets.sublocation like '*" & forms!FORMNAME!sublocation & "*'" & " And assets.[Date Removed] Is Null"

  11. #11
    Reginald is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    10
    Quote Originally Posted by rpeare View Post
    ohhhhh.... snap.... I misread... again... if you're opening a report using a field from a form as a criteria your syntax would be


    "SELECT ID,description,location FROM assets WHERE assets.sublocation like '*" & forms!FORMNAME!sublocation & "*'" & " And assets.[Date Removed] Is Null"
    Thanks but it's not necessary to use forms!Formname since sublocation is defined as follows:
    Dim sublocation As String
    Code:
        If IsNull(cmbxSubLocation) = False Then        bSubLocation = True
            'sublocation = Me.cmbxSubLocation.Column(1)
             sublocation = Replace(Me.cmbxSubLocation.Column(1), "'", "''")
        Else
            bSubLocation = False
            
        End If
    And it still works to populate the listbox.
    I'm thinking it has nothing to do with the query...
    Reginald

  12. #12
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    How about uploading a sample database, I'm pretty sure it's the way you're referencing the sublocation but I'm not making myself clear.

  13. #13
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    When and where do you assign your query to the reports datasource? Perhaps its easier to use the DoCmd.OpenReport method in combination with the WhereCondition parameter.

  14. #14
    Reginald is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    10

    Fixed

    Well,
    I got it working.
    I had to remove the WHERE word in the condition so that it looks like this:
    Code:
    "sublocation like '*" & sublocation & "*'" & " And [Date Removed] Is Null"
    instead of this:
    Code:
    "SELECT ID,[description],location FROM assets WHERE  sublocation like '*" & sublocation & "*'" & " And [Date Removed] Is Null"
    So I guess that takes care of it then.
    Thanks everybody for your time and help.

  15. #15
    Reginald is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    10
    Thanks hapm,
    Your post set me on the right track.
    Do you know how to mark post as
    Solved
    ?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. #Name? error when loading a subform
    By stardust1979 in forum Forms
    Replies: 2
    Last Post: 12-21-2014, 08:59 PM
  2. loading an ActiveX control error
    By AeroProDrive in forum Access
    Replies: 3
    Last Post: 10-01-2014, 12:52 PM
  3. Syntax error in SQL query for report
    By maxmaggot in forum Reports
    Replies: 3
    Last Post: 09-29-2013, 02:27 PM
  4. Syntax Error For Main Form Subform Report
    By burrina in forum Reports
    Replies: 1
    Last Post: 12-27-2012, 03:03 AM
  5. Error message 'Error in Loading DLL'
    By rahayes in forum Programming
    Replies: 2
    Last Post: 09-22-2012, 05:17 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