Results 1 to 5 of 5
  1. #1
    bopsgtir is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    62

    Help with SQL syntax error

    Hi all im trying to insert a record with the below code in vba but i cant seem to get the code right?? any suggestions


    sSQL = "INSERT INTO TblFuelCard (DateOrdered,OrderReason) values (Date(),"Card Ordered For New Starter " & [FirstName] & " " & [Surname]"

  2. #2
    Toyman is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    233
    Quote Originally Posted by bopsgtir View Post
    Hi all im trying to insert a record with the below code in vba but i cant seem to get the code right?? any suggestions


    sSQL = "INSERT INTO TblFuelCard (DateOrdered,OrderReason) values (Date(),"Card Ordered For New Starter " & [FirstName] & " " & [Surname]"
    Dim sName as String

    sName = Me.FirstName & " " & Me.Surname

    sSql = "INSERT INTO tbl_test ( DateOrdered, OrderReason)
    SELECT Date() AS sDate, 'Card Ordered For New Starter '" & sName & " AS stext;"

  3. #3
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I don't know if it was just a typo when you copied the query to your post but you are missing the closing parenthesis. Additionally, you have an extra double quote. Also what are [FirstName] and [Surname]? I assume they are form controls on the form from which you are executing this code, if so you should reference them with me. me.[FirstName]. Also, Date() is technically a variable value and since it is a date, it should be set off with # signs

    sSQL = "INSERT INTO TblFuelCard (DateOrdered,OrderReason) values (#" & Date() & "#,Card Ordered For New Starter " & me.[FirstName] & " " & me.[Surname] & ")"

    I think you also have to set off the two controls with single quotes. I would be tempted to construct the string and store it in a variable and then reference the variable in sSQL

    dim myphrase as string

    myphrase= "Card Ordered For New Starter " & me.[FirstName] & " " & me.[Surname]

    Then the sSQL would be as follows

    sSQL = "INSERT INTO TblFuelCard (DateOrdered,OrderReason) values (#" & Date() & "#,'" & myphrase & "')"

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,604
    Even literal strings need apostrophe delimiter in SQL. Since you are building a string for the second field, one set of apostrophes will work:

    sSQL = "INSERT INTO TblFuelCard (DateOrdered, OrderReason) VALUES (#" & Date() & "#, 'Card Ordered For New Starter " & Me.[FirstName] & " " & Me.[Surname] & "')"
    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
    bopsgtir is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    62
    Thank you all, The Final code i used was

    sSQL = "INSERT INTO TblFuelCard (DateOrdered, OrderReason) VALUES (#" & Date() & "#, 'Card Ordered For New Starter " & Me.[FirstName] & " " & Me.[Surname] & "')"

    supplied by June7 many thanks, this is a real learning curve.

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

Similar Threads

  1. Compile Error Syntax Error
    By Mohamed in forum Access
    Replies: 3
    Last Post: 10-06-2011, 10:12 AM
  2. Syntax Error
    By abriscoe in forum Access
    Replies: 5
    Last Post: 07-27-2011, 01:17 PM
  3. SQL Syntax Error
    By NoiCe in forum Queries
    Replies: 5
    Last Post: 04-01-2011, 11:43 AM
  4. Need help with Syntax Error
    By jgelpi16 in forum Programming
    Replies: 2
    Last Post: 02-04-2011, 08:34 AM
  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