Solved

Apostrophe included in SQL Insert string

Posted on 2007-12-03
5
2,772 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
ID: 20398475
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
ID: 20398485
wrap it with chr(34)

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

Author Comment

by:adraughn
ID: 20398562
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
ID: 20398625
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]
ID: 20398710
>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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

861 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

30 Experts available now in Live!

Get 1:1 Help Now