Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL - saving strings with single quotes included

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

688 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