Results 1 to 12 of 12
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095

    Syntax error claimed by SQL?

    Can anyone see the syntax error claimed by SQL? All fields are text except "FamilySalutation" which is integer. Both "FamilyPhone" and "FamilyE-Mail" are in fact zero-length strings.

    SQL execution error: 3134 Syntax error in INSERT INTO statement.


    SQL STMT: INSERT INTO Families (FamilyName,FamilyAddress,FamilyCityState,FamilyZi p,FamilySalutation,FamilyPhone,[FamilyE-Mail]) Values("Ackerman, Ron","645 J Street","Gering, NE","69341-3377",1,"","")

    Thanks,
    Bill

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Where are you building this sql? If SQL STMT is a constructed field in a query, cannot run an INSERT action as a subquery.

    If you want to run this from VBA, the entire SQL construct needs to be within quote marks because it is a string value. Then all the string values need to be within apostrophe delimiters.

    Is table set to allow empty strings - I don't allow empty strings in any fields. Use Null instead.

    CurrentDb.Execute "INSERT INTO Families (FamilyName,FamilyAddress,FamilyCityState,FamilyZi p,FamilySalutation,FamilyPhone,[FamilyE-Mail]) Values('Ackerman, Ron','645 J Street','Gering, NE','69341-3377',1,Null,Null)"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In my testing, SQL is complaining about the two empty strings.
    If you change
    <snip>,"Gering, NE","69341-3377",1,"","") <snip>
    to
    <snip>,"Gering, NE","69341-3377",1," "," ") <snip> (1 space between the quotes)
    I do not get the error. But that leave a single space in the fields. Not ideal.....

    Then I changed the SQL string to
    Code:
    INSERT INTO Families (FamilyName,FamilyAddress,FamilyCityState,FamilyZip,FamilySalutation,FamilyPhone,[FamilyE-Mail]) 
    Values("Ackerman, Ron","645 J Street","Gering, NE","69341-3377",1,Null,Null)
    The query executed without error.

    So, it seems the problem is the "" in the query...

  4. #4
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095
    The statement as posted in this thread was copied from the log file created during the running of the app. As such, it shows in the log without the quotes. They are however, included when the CurrentDb.Execute statement is run. The SQL string is constructed in code within a general module. What puzzles me the most is that the code ran fine until I changed "FamilySalutation" from text to integer. Previously, I just let SQL deal with the conversion during the table insertion.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    So what is SQL STMT:? Just a label in the forum post, nothing actually in your code?

    Why did you change the data to integer? If the field is a text type, still need the value within delimiters
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095
    Yes, I thought perhaps using a single blank character whenever an zero-length string occurred, but that would cause some un-wanted side effects in the app. Also, I can't set string variables to NULL, so that's out as well, though perhaps I could use a temporary variable DIM'd Variant and set them to NULL when the source is found to be zero-length.

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095
    SQL STMT: is a label in the log file from which I copied the offending statment. I changed the type from text to integer because the table design is Number for that field.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I had the same problem in an update query in code.
    I ended up modifying the code to build the update SQL on-the-fly.
    If the length any field's value was zero, I omitted the field and the value from the SQL update string.

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095
    That's a great idea. My code builds the code statement one field at a time, so I'll simply omit the field if it's a zero-length string. I'll post back after I fix that.
    Thanks,
    Bill

  10. #10
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095
    Here's an example of what my error handler writes to the apps log file:

    SQL execution error: 3134 Syntax error in INSERT INTO statement.
    SQL STMT: INSERT INTO Families([FamilyName], [FamilyAddress], [FamilyCityState], [FamilyZip], [FamilySalutation])
    Values("Ackerman, Ron","645 J Street","Gering, NE","69341-3377",1)

    The statement is wrapped with quotes even though they're omitted when the error handle writes to the log. And yes, those are labels prefixed.

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095
    By-the-way, an item of interest here is that even though SQL is claiming a syntax error, all the insertions were done anyway, EXCEPT where one of the statements included a non-zero-length phone number.

    Here's the code that builds the statement:

    Code:
    strInsert = "INSERT INTO Families([FamilyName], [FamilyAddress], [FamilyCityState], [FamilyZip], [FamilySalutation]"
    strValues = " Values(""" & strFamilyName & """,""" & FT(1) & """,""" & strFamilyCtySt & ""","
    strValues = strValues & """" & FT(4) & """," & intSalutation
    
    If Len(FT(5)) > 0 Then
        strInsert = strInsert & ",[HomePhone]"
        strValues = strValues & ",""" & FT(5) & """"
    End If
    
    If Len(FT(7)) > 0 Then
        strInsert = strInsert & ",[e-MailAddr]"
        strValues = strValues & ",""" & FT(7) & """"
    End If
    
    strInsert = strInsert & ")"
    strValues = strValues & ")"
    
    strSQL = strInsert & " " & strValues
    
    
    CurrentDb.Execute strSQL

  12. #12
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,095
    My deepest apologies to those whose time was spent on this thread. The problem turned out to be related to the error handling. There are two Subs within the same general module that Insert rows into tables, the one you've seen posted repeatedly is that of the "Families". The other is the "Registry" table. (This is ALL new code being added to the app.) What was missing was the error handler in the second Sub where rows are INSERTed into the "Registry". Using a variable (strSQL) global to the module but NOT shared by the two Subs, any SQL errors in the second Sub were being serviced by the error handler in the "Families" Sub where the content of variable strSQL was being posted in the apps log. Meaning anytime there was an error with insertions into the "Registry" were being reported as though the "Families" INSERT was the offending statement. The obvious resolution was the addition of a proper error handler in the 2nd sub, after which it was easy to determine what was wrong with the INSERT statement relating to the "Registry" table.

    Again, my apologies for wasting your time.

    Bill

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

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  2. Compile Error: Syntax Error in DoCmd.RunSQL Statement
    By Evilferret in forum Programming
    Replies: 1
    Last Post: 08-27-2012, 12:32 PM
  3. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  4. Replies: 5
    Last Post: 03-27-2012, 01:40 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