运行时错误3075无法弄明白

时间:2013-02-18 16:33:03

标签: ms-access access-vba

我正在尝试对按钮单击事件执行插入操作,并且我在查询表达式中不断收到缺少运算符的运行时错误,我的查询如下所示。有什么想法吗?

Private Sub CmdAdd_Click()
Dim strSql As String

strSql = "INSERT INTO Current_Costs(PO_Number,Lineitemid,Capital_detail,CapitalID,GL_Number,Cost_Type,Cost_Center,Cost_cat,Item_Cost,PO_Date)" & _
" VALUES (" & Me.txtPONum & "','" & _
    Me.cmbCapDetail & "','" & _
    Me.cmbCapDetail.Column(1) & "','" & _
    Me.txtCapID & "','" & _
    Me.txtGLNum & "','" & _
    Me.cmbCostType & "','" & _
    Me.txtCostCen & "','" & _
    Me.cmbCostCat & "','" & _
    Me.txtCost & "','" & _
    Me.TxtPODate & "')"

   DoCmd.RunSQL strSql

我有一个类似的查询,有相同的问题,我无法看到问题

 CurrentDb.Execute ("UPDATE Current_Costs " & _
    "SET PO_Number='" & Me.txtPONum & "'" & _
    ",Lineitemid='" & Me.cmbCapDetail & "'" & _
    ",Capital_detail='" & Me.cmbCapDetail.Column(1) & "'" & _
    ",CapitalID='" & Me.txtCapID & "'" & _
    ",GL_Number='" & Me.txtGLNum & "'" & _
    ",Cost_Type='" & Me.cmbCostType & "'" & _
    ",Cost_Center='" & Me.txtCostCen & "'" & _
    ",Cost_cat='" & Me.cmbCostCat & "'" & _
    ",Item_Cost='" & Me.txtCost & "'" & _
    ",PO_Date='" & Me.TxtPODate & "'" & _
    "WHERE LineItemPOID=" & Me.txtID.Tag)

编辑已解决

1 个答案:

答案 0 :(得分:1)

这个

" VALUES (" & Me.txtPONum & "','" & _ 

简短的引用,应该是

" VALUES ('" & Me.txtPONum & "','" & _

将sql编写成字符串,这样可以更容易地看到问题:

strSql = "UPDATE Current_Costs " & _
"SET PO_Number='" & txtPONum & "'" & _
",Lineitemid='" & cmbCapDetail & "'" & _
",Capital_detail='" & cmbCapDetail.Column(1) & "'" & _
",CapitalID='" & txtCapID & "'" & _
",GL_Number='" & txtGLNum & "'" & _
",Cost_Type='" & cmbCostType & "'" & _
",Cost_Center='" & txtCostCen & "'" & _
",Cost_cat='" & cmbCostCat & "'" & _
",Item_Cost='" & txtCost & "'" & _
",PO_Date='" & TxtPODate & "'" & _
" WHERE LineItemPOID=" & txtID


Dim db As database
Set db = CurrentDB
db.Execute strsql dbFailOnError

你在WHERE之前错过了一个空格,并且你有一个不匹配的括号。

考虑使用参数:End of statement error