Link to home
Start Free TrialLog in
Avatar of adraughn
adraughnFlag for United States of America

asked on

Apostrophe included in SQL Insert string

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

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adraughn

ASKER

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?
chr(34) is double quote { " }

it will store the value as  string's ., ex. O'Brien    
>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.