Replacing apostrophe in a recordset for SQL insert

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Himmel
    New Member
    • Apr 2007
    • 21

    Replacing apostrophe in a recordset for SQL insert

    Hello!

    I am attempting to take data from one table and insert it into another. I am encountering problems where data in a recordset may contain an apostrophe, which completely kills my SQL Insert.

    Code:
    DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES('" & rs![oldtext] & "')"
    Problem is, some of these records may also be Null, so that kills a Replace() call.

    I've found a really sloppy way of getting around this, but it's horrendous!

    Code:
    oldvar = ""
    if(Len(rs![oldtext]) > 0 then
    oldvar = rs![oldtext]
    oldvar = Replace(oldvar, "'", "''")
    end if
    
    DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES('" & oldvar & "')"
    Please help. Been trying to figure this out for over an hour, and each time I think I find a solution, some funky record comes up and knocks me down!

    Thanks!
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Try this ...

    Code:
    DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & "" & rs![oldtext] & "" & ")"

    Comment

    • Himmel
      New Member
      • Apr 2007
      • 21

      #3
      That returns an error on the first value it tries to write: "Syntax error (missing operator) in query expression."

      In Debug, I can hover over the expression it is saying is in error, and it (along with all other expressions) contain the data that is expected.

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Try this ...

        Code:
        Dim strSQL as String
        
           strSQL = "INSERT INTO NEWTABLE (Record1) VALUES(" & "" & rs![oldtext] & "" & ")"
        
           DoCmd.RunSQL strSQL

        Comment

        • pks00
          Recognized Expert Contributor
          • Oct 2006
          • 280

          #5
          try this


          DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & chr$(34) & NZ(rs![oldtext],"") & chr$(34) & ")"

          nulls, hmm yes. If u ensure that the field allows nulls and is not mandatory then it should work

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Originally posted by pks00
            try this


            DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & chr$(34) & NZ(rs![oldtext],"") & chr$(34) & ")"

            nulls, hmm yes. If u ensure that the field allows nulls and is not mandatory then it should work
            Nice one, should have thought of that. ;)

            Comment

            • pks00
              Recognized Expert Contributor
              • Oct 2006
              • 280

              #7
              Originally posted by mmccarthy
              Nice one, should have thought of that. ;)
              Its cos your overworked as a Administrator and ensuring orphaned questions get cleared :)

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Originally posted by pks00
                Its cos your overworked as a Administrator and ensuring orphaned questions get cleared :)
                LOL, thanks for that.

                Comment

                • pks00
                  Recognized Expert Contributor
                  • Oct 2006
                  • 280

                  #9
                  No probs :)

                  Comment

                  • Himmel
                    New Member
                    • Apr 2007
                    • 21

                    #10
                    Thanks guys. :) This is starting to shape up nicely.

                    I hate to throw a wrench into the works, but now I'm getting an operand error when the SQL encounters a record that contains a quotation mark. And again, the field may be Null.

                    Any suggestions on what to do next?

                    Comment

                    • pks00
                      Recognized Expert Contributor
                      • Oct 2006
                      • 280

                      #11
                      What code are you using

                      Comment

                      • Himmel
                        New Member
                        • Apr 2007
                        • 21

                        #12
                        Originally posted by pks00
                        What code are you using
                        I took a page out of your book:

                        Code:
                        DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & chr$(34) & NZ(rs![oldtext],"") & chr$(34) & ")"
                        This handles fields that have an apostrophe. But if any have a quotation, it breaks.

                        Comment

                        • goforth4
                          New Member
                          • Mar 2007
                          • 2

                          #13
                          I can advise you where to look.

                          Not 100% sure how..

                          But I'd search on how SQL insert statements handle apostrophes in inserting data.

                          Then I'd use the technique to 'replace' apostrophes with whatever the syntax SQL is appropriate for the database/SQL.

                          e.g.
                          Code:
                          tempStr = "String with apost'rophe"
                          newStr = replace(tempStr,"'","''") ''DOUBLE APOSTROPHE
                          Might work.. Dunno.

                          Comment

                          • BigJ
                            New Member
                            • Feb 2013
                            • 1

                            #14
                            Replace the apostrophe in values that are encased in single quotes (e.g. the O'Brien in 'O'Brien') as follows: O' & "'" & 'Brien

                            Use the following code snippet:
                            Code:
                            Replace(rs![field1], " ' ", " ' " & " & " & """" & " ' " & """" & " & ' ") & "'" )
                            NOTE I have added a space between the single and double quotes to make it easier to tell them apart. In the actual code you use, you should not have these spaces.

                            For example

                            Instead of
                            Code:
                            Docmd.RunSQL ("INSERT INTO Tablename (DestinationField) SELECT '" & rs![field1] & "'")
                            use
                            Code:
                            Docmd.RunSQL ("INSERT INTO Tablename (DestinationField) SELECT '" & Replace(rs![field1], "'", "'" & " & " & """" & "'" & """" & " & '") & "'" )
                            This worked for me and allowed me to use VBA to insert values containing apostrophes (single quote marks) into a table using SQL insert statements
                            Last edited by NeoPa; Feb 7 '13, 12:20 AM. Reason: Added mandatory [CODE] tags.

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32668

                              #15
                              This is really a lot more straightforward than people give it credit for. Doubling up quotes of either form can be used to represent the character that is used as the string delimiter. See SQL Injection Attack for some very simple code, and also why it can be very important to get right, of course.

                              Comment

                              Working...