2

I have written a simple code to loop through rows and insert values in column A (that is column containing full name) into an SQL Table. Something like this:

    For i = 1 to LastRow

    Command.CommandText = "INSERT INTO [TABLE] [Col1] VALUES ('" & Sheets("Sheet1").Cells(i, 1).Value & "')"

    Next i

Issue arises when we have names like [O'Connell], which obviously creates a Bobby Table issue.

Are there any clever workarounds to avoid this?

Thanks

N.B. The full name is inserted from another SQL Table. In other words, perhaps a little difficult to edit.

Oday Salim
  • 1,129
  • 3
  • 23
  • 46
  • Possible duplicate of [VBA, ADO.Connection and query parameters](https://stackoverflow.com/questions/10352211/vba-ado-connection-and-query-parameters) – Victor K Jun 10 '18 at 02:09

1 Answers1

1

You could use Replace to remove the problem character:

Command.CommandText = "INSERT INTO [TABLE] [Col1] VALUES ('" & _
    Replace(Sheets("Sheet1").Cells(i, 1).Value,"'","") & "')"
ashleedawg
  • 20,365
  • 9
  • 72
  • 105