Dim cn As New ADODB.Connection
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.Properties("Jet OLEDB:Database Password") = "quality"
cn.Open strCN
cn.Execute "insert into quality_issue(record_date,record_time,rec_id,shift,pkg,pkg_code,mach_id,device,lot_no,qty,defect_date,defect_time,badge,confirm,accident,process,category,reason,pm_issue)values('" & dtpDate.Value & "','" & dtpTime.Hour & ":" & dtpTime.Minute & ":" & dtpTime.Second & "','" & txtRec_id.Text & "','" & cboShift.List(cboShift.ListIndex) & "','" & cboPKG.List(cboPKG.ListIndex) & "','" & cboCode.List(cboCode.ListIndex) & "','" & txtMach_id.Text & "','" & txtDevice.Text & "','" & txtLot.Text & "','" & txtDef_Qty.Text & "','" & txtDef_Date.Text & "','" & txtDef_Time.Text & "','" & txtBadge.Text & "','" & txtConfirm.Text & "','" & RConfirm & "','" & cboProcess.List(cboProcess.ListIndex) & "','" & cboCategory.List(cboCategory.ListIndex) & "','" & CStr(txtDisc.Text) & "','" & cboPM.List(cboPM.ListIndex) & "')"
cn.Close
当运行到将textbox控件txtDisc的text字符串输入给access数据库reason字段时,如果字符串中包含单引号',就会报错:“Syntax error(missing operator) in query expression”,请问如何解决

解决方案 »

  1.   

    http://www.ddvip.net/web/VBScript/jq/41.htm
      

  2.   

    动态sql就会出现这个错误,如果接受的参数值不能有单引号,就使用replace(txtdisc.text,"'","")将单引号去掉,否则使用楼上的方法,将一个变为两个就不会出错了!
      

  3.   

    但是我需要经常在txtdisc中输入英文,难免要有',如果把'都去掉,那么英文重新调回来就没法看了,等待更好地解决办法
      

  4.   

    & "','" & REPLACE(txtDisc.Text,"'","''") & "','" & cboPM.List(cboPM.ListIndex) & "')"实意字符 "'" 在 SQL 命令中必须双写成"''"。