Use Backquotes Instead of Apostrophes

Use Backquotes Instead of Apostrophes

Often when using Transact-SQL, I want to capture comments from a user in a text box and send them tothe database. However, if the user types an apostrophe in the text box, a run-time error is generated whenthe update is processed, because SQL Server thinks the apostrophe is being used to mark the end of astring.To get around this problem, intercept the user’s keystrokes in the KeyPress event and exchange theapostrophe with an “upside-down” quote mark (ASCII(145)) like this:

 Private Sub Text1_Keypress_        (KeyAscii as Integer)        If KeyAscii = 39 Then                KeyAscii = 145        End IfEnd Sub

Alternatively, you might decide to substitute all occurrences of single quotes into backquotes immediatelybefore sending them to SQL Server.


Share the Post: