如何使用包含'的字段插入数据

时间:2013-10-11 21:22:34

标签: ms-access access-vba

我正在附加一个记录集的表数据,这个数据正在运行,直到我得到一个拥有'客户名称'的法语客户端。这是代码:

StrSQL3 = " INSERT INTO [ATB] ( SLSMAN, NOCARDEX, email, CLIENT, "
StrSQL3 = StrSQL3 & " NOTRANS, InvoiceDate, Current, [31-60], [61-90], [90+] ) "
StrSQL3 = StrSQL3 & " VALUES ( '" & myRecordSet.Fields(0).Value & "', "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(1).Value & "', "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(2).Value & "', "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(3).Value & "', "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(4).Value & "',  "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(5).Value & "',  "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(6).Value & "', "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(7).Value & "', "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(8).Value & "', "
StrSQL3 = StrSQL3 & " '" & myRecordSet.Fields(9).Value & "' ) "
    If OldEmail = email Then
    DoCmd.RunSQL StrSQL3 

一切都很好,直到我得到这个客户:位置D'Outils Nicolet。 由于'在客户端名称中,我得到错误3075 - 缺少运算符。

有没有人知道这方面的方法?

1 个答案:

答案 0 :(得分:3)

使用参数查询时,引号不是问题。

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strInsert As String

strInsert = "INSERT INTO [ATB] (" & vbCrLf & _
    "SLSMAN, NOCARDEX, email, CLIENT, " & _
    "NOTRANS, InvoiceDate, [Current], " & _
    "[31-60], [61-90], [90+])" & vbCrLf & _
    "VALUES (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9);"
Debug.Print strInsert

Set db = CurrentDb
Set qdf = db.CreateQueryDef("", strInsert)
qdf.Parameters("p0").value = MyRecordSet.Fields(0).value
qdf.Parameters("p1").value = MyRecordSet.Fields(1).value
' add the remaining parameter values
qdf.Execute
Set qdf = Nothing
Set db = Nothing