Solved

SQL - saving strings with single quotes included

Posted on 1998-12-18
4
181 Views
Last Modified: 2013-12-25
in my vb app, I'm using sql statements to save data to an access db table.  One field in particular is a memo field which is used for comments.  When I'm constructing my sql statement I get errors because of the way single quotes are used.  If single quotes are used in the memo field (e.g, this is credit's responsibility), when it is used in the sql statement (e,g, strSQL="Update tblInfo SET memofield = '" & txtmemo.text & "';"), an error is received because of the single quote in the text being saved.  How do I overcome this problem, without preventing the user from entering single quotes in the memo field.
0
Comment
Question by:gbr
4 Comments
 
LVL 3

Expert Comment

by:jjbyers
ID: 1498390
Check out the GetChunk method
0
 
LVL 3

Accepted Solution

by:
jjmartin earned 20 total points
ID: 1498391
Here is a subroutine you can use to setup the string for saving to your database.  Simply send the string to the routine, and it will give back a string ready to be saved.

Public Function Apostrophe(sFieldString As String) As String

              If InStr(sFieldString, "'") Then
                     Dim iLen As Integer
                     Dim ii As Integer
                     Dim apostr As Integer
                     iLen = Len(sFieldString)
                     ii = 1

                            Do While ii <= iLen
                                    If Mid(sFieldString, ii, 1) = "'" Then
                                    apostr = ii
                                   sFieldString = Left(sFieldString, apostr) & "'" & _
                                   Right(sFieldString, iLen - apostr)
                                    iLen = Len(sFieldString)
                                    ii = ii + 1
                                    End If
                                    ii = ii + 1
                            Loop

              End If

       strClean = sFieldString
        'strClean is a public string used to save the newly formatted string
End Function

0
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1498392
One way to update a field is to use a recordset rather than executing an update.

Example:

Dim WS AS Workspace
Dim DB AS Database
Dim Rst AS Recordset

Set WS = DBEngine.CreateWorkspace("","Admin","")
Set DB = WS.opendatabase("C:\MyDB.Mdb")
Set Rst = DB.OpenRecordset("SELECT tblRecord.* FROM tblRecord WHERE tblRecord.fldID = '" & thePrimaryKey & "';")

If Not (Rst.Bof And Rst.Eof) Then
  Rst.MoveFirst
  Rst.Edit
  Rst!memofield = txtmemo.text
  Rst.Update
End IF


This will place any valid string into your memo field.


I hope this helps you out!
0
 

Expert Comment

by:Donavon
ID: 1498393
Another way to do what was suggested by jjmartin is use the replace function.

replace("credit's","'","[single quote]",1,-1)

your statement example would look like this:

strSQL="Update tblInfo SET memofield = '" & replace(txtmemo.text,"'","[single quote]",1,-1) & "'"

This will replace all your single quotes with the "Code" for a single quote (Or whatever you want).   Of course, any time you want to display the information again, you will want to reverse the process.  Easily done, but what if someone makes a connection to your table without using your front-end?  That is why I would recommend using a Recordset as suggested by VBWhiz.  The Recordset method will not "Corrupt" your data and will work Whatever is contained in the string.

PS: Don't know when the replace function was first implemented?  Your version of VB may not have it.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question