Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL - saving strings with single quotes included

Posted on 1998-12-18
4
Medium Priority
?
186 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 60 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

963 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