Solved

SQL - saving strings with single quotes included

Posted on 1998-12-18
4
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

740 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