devxlogo

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist