Results 1 to 6 of 6
  1. #1
    deepin125 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    8

    Problem with SQL insert, getting a syntax error and not sure why

    I am doing a project for work, and I am attempting to do a manual insert into a query. I have been beating my head trying to get it to work and thought maybe someone might have some insight into the problem.

    I have a form that I am pulling all of the data from, I then put the information into variables, then I try to insert the variables into the query. I keep getting the blasted Syntax error on the Into Insert statement. Here is the code:

    Dim db As DAO.Database
    Dim sSQL As String
    Dim varIN As String, varTran As String, varTP As String, varEF As Date, varNP As Currency, varSo As String, varPr As String, varCS As String, varCom As String, varOf As String, varFCTC As String, varTC As String, varLP As Currency, varDK As Date, varPN As String
    varIN = Me.Insured_Name.value
    varTran = Me.Transaction.value
    varTP = Me.Type_of_Policy.value
    varEF = Me.Effective_Date.value
    varNP = Me.New_Premium.value
    varSo = Me.Combo162.value
    varPr = Me.Producer.value
    varCS = Me.Combo224.value
    varCom = Me.Memo.value
    varOf = Me.Office.value
    varFCTC = Me.Combo242.value
    varTC = Me.Combo395.value
    varLP = Me.Text261.value
    varDK = Me.Text410.value
    varPN = Me.Text425.value



    Set db = CurrentDb
    sSQL = "INSERT INTO Transactions Extended ([Insured Name], [Transaction],[Type of Policy], [Effective Date], [New Premium], [Source], [Producer], [CSR], [Comments], [Office], [From Company / To Company], [To Company], [Lost Premium], [Date Keyed], [Policy Number]) Values ("" & varIN & "","" & varTran & "", "" & varTP & "",#" & varEF & "#,"" & varNP & "","" & varSo & "","" & varPr & "","" & varCS & "","" & varCom & "","" & varOf & "","" & varFCTC & "","" & varTC & "","" & varLP & "",#" & varDK & "#,"" & varPN & "")"
    db.Execute sSQL, dbFailOnError

    If anyone sees anything glaring that I am missing let me know!

    Thanks

    ~Joel

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    It is difficult to check as displayed above. Before the db.Execute statement put in Debug.Print sSql. It will print the actual SQL statement in the immediate window along with all the values. Copy and paste this into an empty query design and you can then easily troubleshoot it.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Here's a visual of what aytee is talking about:

    http://www.baldyweb.com/ImmediateWindow.htm

    I suspect the problem is with your quotes. The "inner" quotes probably need to be singles, or they need to be tripled up. There's a tutorial and sample db here that might help:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also, the variables declared as currency (a number type) don't need delimiters.

  5. #5
    deepin125 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    8
    Ok fantastic. I followed your instructions, fixed it so it now looks like the following:

    code:
    sSQL = "INSERT INTO Transactions Extended ([Insured Name], [Transaction],[Type of Policy], [Effective Date], [New Premium], [Source], [Producer], [CSR], [Comments], [Office], [From Company / To Company], [To Company], [Lost Premium], [Date Keyed], [Policy Number]) Values ('" & varIN & "','" & varTran & "','" & varTP & "',#" & varEF & "#," & varNP & ",'" & varSo & "','" & varPr & "','" & varCS & "','" & varCom & "','" & varOf & "','" & varFCTC & "','" & varTC & "'," & varLP & ",#" & varDK & "#,'" & varPN & "')"

    Debug window:
    INSERT INTO Transactions Extended ([Insured Name], [Transaction],[Type of Policy], [Effective Date], [New Premium], [Source], [Producer], [CSR], [Comments], [Office], [From Company / To Company], [To Company], [Lost Premium], [Date Keyed], [Policy Number]) Values ('Joel','New Business','Accident',#3/5/2012#,1,'Account Rounded','Mizener, CISR, Kimberly K','Mizener, CISR, Kimberly K','test','New Hartford','A Central Insurance Co.','A Central Insurance Co.',0,#3/29/2012#,'a')

    Problem is it is giving the same syntax error. It appears that the information is being read correctly now, but for some reason something else in my syntax is a causing an error. Single quotations are enough correct? Do I need to triple up?

    Thanks for your speedy help.

  6. #6
    deepin125 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    8
    OK I found my error on my own. I had to put brackets around my query [Transactions Extended] in order to get rid of the syntax error.

    Thanks for all your help!

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

Similar Threads

  1. INSERT INTO Syntax Error
    By eww in forum Programming
    Replies: 1
    Last Post: 04-06-2011, 10:28 AM
  2. Insert INTO syntax help
    By jpkeller55 in forum Access
    Replies: 3
    Last Post: 02-27-2011, 03:38 PM
  3. Combine insert into syntax
    By Cojack in forum Queries
    Replies: 1
    Last Post: 09-22-2010, 07:48 AM
  4. SQL INSERT INTO Date Syntax Error
    By tuna in forum Programming
    Replies: 5
    Last Post: 08-10-2010, 06:17 AM
  5. Syntax Error In Insert Into Statement
    By frankvfox in forum Queries
    Replies: 1
    Last Post: 09-09-2008, 12:35 PM

Tags for this Thread

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