Results 1 to 14 of 14
  1. #1
    accessTenderfoot is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    7

    Ms access 2010 insert into syntax error

    I'm practically starting to use MS Access and simply copied a code from the internet. I think I've checked it thoroughly but still getting syntax error message when the command is ran. Below is the code I have.

    CurrentDb.Execute "INSERT INTO tblAql(area, woNo, pn, qtyRec, qtyAcc, qtyRej, source, optr, insp, dmrNo, aqlPct, def1, def2, def3, machine, comment, dateTime, noSmpls, dispo) " & _
    "VALUES(" & Me.cboArea & ",'" & Me.txtWO & "','" & Me.txtPN & "','" & Me.intRec & "','" & Me.intAcc & "','" & _
    Me.intRej & "','" & Me.cboSource & "','" & Me.txtOptr & "','" & Me.txtInsp & "','" & Me.txtDMR & "','" & Me.cboAqlPct & "','" & _
    Me.cboDef1 & "','" & Me.cboDef2 & "','" & Me.cboDef3 & "','" & Me.txtMachine & "','" & Me.txtComment & "','" & nowDate & "','" & Me.cboDispo & "')"




    Can anyone help me out?

    Thanks.


    accessTenderfoot

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The number of fields and values don't match.

    Date fields require # delimiter instead of apostrophe. Number fields do not require delimiter.
    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
    accessTenderfoot is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    7
    Thanks for the quick response Moderator. You are right, I was missing another field. As for the date field (nowDate), do I still have to put # though it's a variable? Below is the complete code for the command buttons.

    Option Compare Database
    Private Sub cmdAdd_Click()
    nowDate = Now()
    CurrentDb.Execute "INSERT INTO tblAql(area, woNo, pn, qtyRec, qtyAcc, qtyRej, source, optr, insp, dmrNo, aqlPct, def1, def2, def3, machine, comment, dateTime, noSmpls, dispo) " & _
    "VALUES(" & Me.cboArea & ",'" & Me.txtWO & "','" & Me.txtPN & "','" & Me.intRec & "','" & Me.intAcc & "','" & _
    Me.intRej & "','" & Me.cboSource & "','" & Me.txtOptr & "','" & Me.txtInsp & "','" & Me.txtDMR & "','" & Me.cboAqlPct & "','" & _
    Me.cboDef1 & "','" & Me.cboDef2 & "','" & Me.cboDef3 & "','" & Me.txtMachine & "','" & Me.txtComment & "','" & nowDate & "','" & Me.intNoSamples & "','" & Me.cboDispo & "')"


    'cmdClear_Click
    frmAQLsub.Form.Requery
    End Sub
    Private Sub cmdClose_Click()
    DoCmd.Close
    End Sub

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Try with the # but definitely remove the apostrophe.

    Don't really need that variable, just use Now() in the concatenation.

    So why do you need to use INSERT to create record? Why not a bound form?
    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.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  6. #6
    accessTenderfoot is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    7
    I hate to admit it but I'm confused with this single apostrophe and double apostrophes; not really sure which is being enclosed by which apostrophe. Would you mind marking up this code to where the # goes? Thanks.

    CurrentDb.Execute "INSERT INTO tblAql(area, woNo, pn, qtyRec, qtyAcc, qtyRej, source, optr, insp, dmrNo, aqlPct, def1, def2, def3, machine, comment, dateTime, noSmpls, dispo) " & _
    "VALUES(" & Me.cboArea & ",'" & Me.txtWO & "','" & Me.txtPN & "','" & Me.intRec & "','" & Me.intAcc & "','" & _
    Me.intRej & "','" & Me.cboSource & "','" & Me.txtOptr & "','" & Me.txtInsp & "','" & Me.txtDMR & "','" & Me.cboAqlPct & "','" & _
    Me.cboDef1 & "','" & Me.cboDef2 & "','" & Me.cboDef3 & "','" & Me.txtMachine & "','" & Me.txtComment & ",#" & Now() & "#," & Me.intNoSamples & "','" & Me.cboDispo & "')"

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Missing the second apostrophe for Me.txtComment.

    "','" & Me.txtComment & "',#"

    Want to answer my questions from earlier post?
    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.

  8. #8
    accessTenderfoot is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    7
    Sorry, didn't see the questions.

    I'm trying to make a database wherein several people (at different computer stations) would be using so I'm trying to make a simple custom-made form where users simply enter the data and click on the ADD command button.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    But if form is bound to table and controls bound to fields, data entered into controls would pass directly to table and no code would be needed. This is the appeal of Access - it does the heavy lifting for you.
    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.

  10. #10
    accessTenderfoot is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    7
    Still getting syntax error message...

    CurrentDb.Execute "INSERT INTO tblAql(area, woNo, pn, qtyRec, qtyAcc, qtyRej, source, optr, insp, dmrNo, aqlPct, def1, def2, def3, machine, comment, dateTime, noSmpls, dispo) " & _
    "VALUES(" & Me.cboArea & ",'" & Me.txtWO & "','" & Me.txtPN & "','" & Me.intRec & "','" & Me.intAcc & "','" & _
    Me.intRej & "','" & Me.cboSource & "','" & Me.txtOptr & "','" & Me.txtInsp & "','" & Me.txtDMR & "','" & Me.cboAqlPct & "','" & _
    Me.cboDef1 & "','" & Me.cboDef2 & "','" & Me.cboDef3 & "','" & Me.txtMachine & "','" & Me.txtComment & "',#" & Now() & "#," & Me.intNoSamples & "','" & Me.cboDispo & "')"

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What error message?

    If intRec, intAcc, intRej pass numbers to number type fields - as stated, number fields don't use delimiters.

    Did you read my previous post?

    I still don't understand why you are using this approach.
    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.

  12. #12
    accessTenderfoot is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    7
    Ok, I think I'll try it. Thanks again for all the help.

  13. #13
    accessTenderfoot is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    7
    Thanks Orange for the references.

  14. #14
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Whatever the reason for doing what you're doing, I don't agree with this: #" & Now() & "#.
    In my not always enlightened opinion, Now() and Date() are functions that return dates, so date delimiters are not only not required, they can cause the error. If you create a query and put < Date() or <Now() in the design grid and look at the sql, Access does not put delimiters around the functions and the query will run (assuming all else is OK).

    One thing you can do is, with the form open as normal, put a debug.print line right after the last line that builds your sql string, copy it from the immediate window and dump it into the sql view of a new query. Change all ocurrences of Me to an explicit form reference (Forms!yourFormName), and set the query view to datasheet view. If it balks, it might highlight the offending piece of the sql.

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

Similar Threads

  1. Replies: 14
    Last Post: 07-07-2015, 07:39 PM
  2. Replies: 3
    Last Post: 01-29-2015, 07:48 PM
  3. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  4. Syntax Error: Insert Into statement in VBA
    By Kimbertha in forum Programming
    Replies: 1
    Last Post: 07-24-2012, 05:02 PM
  5. INSERT INTO Syntax Error
    By eww in forum Programming
    Replies: 1
    Last Post: 04-06-2011, 10:28 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