• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1307
  • Last Modified:

SQL Query with apostrophe

Hi,

I have an access database where I insert some records into my table. The records are file names from certain folders. The query works fine (please see the code below).

The only issue is when a file name has an apostrophe in it. For example a file called "Word Documents" would work fine, however a file called "Word Document's" would not work becuase of the apostrophe.

Any help would be great.

Thanks,
M

                strSQL = "INSERT INTO tblCompanyFiles ([Ticker], [Folder], [FileName], [DocType]) " & _
                            "VALUES ('" & Ticker & "','" & objFolder.Name & "','" & objFile.Name & "','" & DocType & "');"
                            
                DoCmd.RunSQL strSQL   

Open in new window

0
mcs26
Asked:
mcs26
2 Solutions
 
CluskittCommented:
To use concatenation, you'd have to use Replace(objFolder.Name,"'","''"). However, you should consider using parameters, as this leaves you open to SQL injection.
0
 
stalhwCommented:
just double the quotes...
for example
strSQL = "INSERT INTO tblCompanyFiles ([Ticker], [Folder], [FileName], [DocType]) " & _
                            "VALUES ('" & Ticker & "','" & replace(objFolder.Name,"'","''") & "','" & replace(objFile.Name,"'","''") & "','" & DocType & "');"
                            
                DoCmd.RunSQL strSQL   

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now