Solved

SQL - saving strings with single quotes included

Posted on 1998-12-18
4
178 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
Comment Utility
Check out the GetChunk method
0
 
LVL 3

Accepted Solution

by:
jjmartin earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now