Solved

Apostrophe included in SQL Insert string

Posted on 2007-12-03
5
2,770 Views
Last Modified: 2012-06-27
I'm sure someone has the perfect answer for this. what is the trick to inserting text into a table via sql/vba that includes apostrophe's? (Or other characters that will break it for that matter?)

For instance, I am using this code:
For j = 0 To 1

            

        'Save Row and Column Descriptions

        

        If j = 0 Then c = "Row" Else c = "Col"

        

        strSQL = "INSERT INTO tbHRI_Descrip (HRI_ID,Descrip_ID,DescripType,Label," & _

            "Description)" & _

            "VALUES ('" & Me.txtHRI_ID.Value & "','" & i & "','" & c & "','" & _

            Me.Controls("txt" & c & "Label" & i).Value & "','" & _

            Me.Controls("txt" & c & "Descrip" & i).Value & "')"

        

        DoCmd.RunSQL strSQL

              

        Next

Open in new window

0
Comment
Question by:adraughn
  • 2
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
you have to duplicate the quote. assuming that only Lable is subject to contain a quote:



For j = 0 To 1
            
        'Save Row and Column Descriptions
        
        If j = 0 Then c = "Row" Else c = "Col"
        
        strSQL = "INSERT INTO tbHRI_Descrip (HRI_ID,Descrip_ID,DescripType,Label," & _
            "Description)" & _
            "VALUES ('" & Me.txtHRI_ID.Value & "','" & i & "','" & c & "','" & _
            Me.Controls("txt" & c & "Label" & i).Value & "','" & _
            replace(Me.Controls("txt" & c & "Descrip" & i).Value , "'", "''") & "')"
        
        DoCmd.RunSQL strSQL
              
        Next

Open in new window

0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
Comment Utility
wrap it with chr(34)

 " & chr(34) & variablestring & chr(34) & "
0
 
LVL 13

Author Comment

by:adraughn
Comment Utility
so in angel's solution, the table will show an apostrophe as a double apostrophe? So then when I populate the form with the recordset, i would need to replace the double apostrophe with an apostrophe? Would this also replace quotes? (")

in cap's solution, is chr(34) an apostrophe? So would it store the string in the table as 'string's' instead of string's?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
chr(34) is double quote { " }

it will store the value as  string's ., ex. O'Brien    
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>so in angel's solution, the table will show an apostrophe as a double apostrophe?
no. duplicating the single quote into 2 single quotes is ONLY for the insert to work.
after the insert, there will indeed only be 1 single quote in the table.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 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

10 Experts available now in Live!

Get 1:1 Help Now