Parameter with apostrophe

jo15765

Well-known Member
Joined
Sep 23, 2011
Messages
591
I have a listbox in Excel that is passed as a parameter to a SQL Server stored procedure. The issue I am running into is that some of the values in Excel listbox have an apostrophe, which throws a SQL Server error of
'Incorrect Syntax near ' (the apostrophe)

How can I check the value being passed before it leaves Excel to determine if I should double single quote the value from the listbox?

VBA Question
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You could check using InStr to check for single quotes or you could just use Replace to double up any single quotes.
 
Upvote 0
You could check using InStr to check for single quotes or you could just use Replace to double up any single quotes.

Should I add a check if the string has a single apostrophe in it, or should I just run the replace function regardless?
Code:
Function ReplaceTest()
Dim teststring As String
teststring = "Michael's"
teststring = Replace(teststring, "'", "''")
Debug.Print teststring

teststring = "Michael"
teststring = Replace(teststring,"'","''")
Debug.Print teststring
End Function

The 1st example prints Michael''s and the second prints Michael so no error is thrown if I use the Replace function and the string has no apostrophe in it, just was not sure syntactically what is better. To code the if..else or to just have the replace and if it's needed it's done, if it is not, well it's in play as a catch all.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,794
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top