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.

1

1 Answer 1

1

You could use Replace to remove the problem character:

Command.CommandText = "INSERT INTO [TABLE] [Col1] VALUES ('" & _ Replace(Sheets("Sheet1").Cells(i, 1).Value,"'","") & "')" 
Sign up to request clarification or add additional context in comments.

5 Comments

Brilliant... So in theory I can use this to santize all my strings from (') and (;)... Correct?
@OdaySalim in theory yes. And you can nest them replace(replace(replace(yourstring,"'",""),"*",""),".","") etc
How would you nest them?
if there are any " make sure you get those too replace Chr(34) with whatever
One more question dude, why are you sanitizing the "."? I do not see anything malicious with it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.