Access/VBA Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sc5502
    New Member
    • Jun 2014
    • 102

    Access/VBA Question

    Front End: Access 2010; Back End SQL Server 2008

    The following produces this error messages and I do not know why: Runtime error '3134' Syntax error in INSERT INTO statement.

    Code:
    zSQL2 = _
        "INSERT INTO [dbo_Notes] " & _
            "(partshortages_ID, date, [by], [note])" & _
            " VALUES(" & _
            "  " & gblDBResult2 & _
            ", '" & RDateAdded & "'" & _
            ", '" & Rbuyer & "'" & _
            ", '" & Me.Notes.Value & "')"
     Debug.Print zSQL2
    DoCmd.SetWarnings False
    DoCmd.RunSQL zSQL2
    The database definitions are:
    partshortages_I D int
    Date varchar(12)
    By varchar(50)
    note text

    Thank you.
    Last edited by Rabbit; Jun 25 '14, 05:09 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3662

    #2
    When you Debug.Print, what is the result that you get? That will help us greatly!

    BTW, please use Code tags when you post Code!

    Comment

    • sc5502
      New Member
      • Jun 2014
      • 102

      #3
      This what I get with a debug.print:

      INSERT INTO [dbo_Notes] (partshortages_ ID, date, [by], [note]) VALUES( 16, '6/25/2014', 'SClark', 'sdbsdfgsdfgsdf gsdfgfdsg')

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3662

        #4
        sc5502,

        I am assuming [dboNotes] is the table? If so, this should not be in brackets.

        Your fields: (partshortages_ ID, date, [by], [note]) also should not be in parentheses.

        Your date "may" require "#" before and after it.

        So, this might help:

        Code:
        zSQL2 = _
        "INSERT INTO dbo_Notes " & _
        "partshortages_ID, date, [by], [note]" & _
        " VALUES (" & _
        " " & gblDBResult2 & _
        ", '" & RDateAdded & "'" & _
        ", '" & Rbuyer & "'" & _
        ", '" & Me.Notes & "')"
        Debug.Print zSQL2
        DoCmd.SetWarnings False
        DoCmd.RunSQL zSQL2
        I've tried it without the "#", but if this still causes an error, try this for line 6:

        Code:
        ", #" & RDateAdded & "#" & _
        Hope this hepps!

        Comment

        • sc5502
          New Member
          • Jun 2014
          • 102

          #5
          I've tried your the suggestions and still get the error.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Actually, the parentheses around the field list is required.

            After you print out the SQL, put it in a query and see if it will run.

            Comment

            • sc5502
              New Member
              • Jun 2014
              • 102

              #7
              Here what I get


              INSERT INTO dbo_Notes (partshortages_ ID, date, by , note ) VALUES( 17, '6/25/2014', 'SClark', 'fhdfghdfghdfgh dfghdfghdfghdfg h')


              Msg 156, Level 15, State 1, Line 2
              Incorrect syntax near the keyword 'by'.

              Comment

              • twinnyfo
                Recognized Expert Moderator Specialist
                • Nov 2011
                • 3662

                #8
                @Rabbit,

                Yes, thank you--my mistake!

                @sc5502,

                "By" is a reserved word. Try putting it back into brackets.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  date is also probably a key word, put that in brackets as well.

                  Comment

                  • twinnyfo
                    Recognized Expert Moderator Specialist
                    • Nov 2011
                    • 3662

                    #10
                    I guess this is a good demonstration showing the importance of choosing good field names and avoiding reserved/key words!

                    I admit, I learned that the hard way.... :-/

                    Also, reviewing some of my sparse code that uses the INSERT INTO, you will have to enclose your date with "#":

                    Code:
                    ", #" & RDateAdded & "#" & _

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32654

                      #11
                      As your [Date] field is designed as varchar(12) you actually won't want the # chars. Your SQL quotes there are already correct. Otherwise, the advice given so far, where not already corrected, should get you to where you want to be.

                      Good luck :-)

                      Comment

                      Working...