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

x
?
Solved

Apostrophe included in SQL Insert string

Posted on 2007-12-03
5
Medium Priority
?
2,788 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
[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
  • 2
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20398625
chr(34) is double quote { " }

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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