我在组合字段的 onchange 事件中使用 vba 代码。代码如下
Dim db As Database
Dim rs As DAO.Recordset
Dim qrystr As String
Dim cond As String
Dim qrystr_CID As String
'cond = [Forms]![PharmDrug].[Commercial drugs subform2].Form.DrugCompanyName & vbNullString
cond = Me.DrugCompany & vbNullString
'MsgBox cond
Set db = CurrentDb
If cond = vbNullString Then
' do nothing
Else
qrystr = "SELECT DrugCompanyID FROM [Drug Company] WHERE Name ='" & cond & "';"
Set rs = db.openrecordset(qrystr)
qrystr_CID = rs!DrugCompanyID
Me.DrugCompanyID = qrystr_CID
rs.Close
Set rs = Nothing
End If
这工作正常,但它在查询表达式'Name = 'Dr.中给出错误3075语法错误(缺少运算符)。雷迪的实验室。';'
那是如果名称字段中的值包含撇号等特殊字符。我怎样才能摆脱这个错误?
请帮我解决这个问题。