0

I'm trying to do an insert on a button click event and I keep getting a runtime error of missing operator in query expression my query looks like the following. Any ideas?

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

i have a similar query that has the same issue and i cant see the problem

 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)

edit solved

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
user1902540
  • 111
  • 2
  • 12
  • 1
    This `" VALUES (" & Me.txtPONum & "','" & _` is short one quote, it should be `" VALUES ('" & Me.txtPONum & "','" & _`. But I do not think that will give the error number you quote. – Fionnuala Feb 18 '13 at 16:37
  • Please consider using prepared statements instead. Here is an example: http://stackoverflow.com/questions/6572448/msaccess-prepared-statements – mechanical_meat Feb 18 '13 at 16:49

1 Answers1

1

This

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

Is short one quote, it should be

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

Write your sql in a string, it makes it easier to see the problems:

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

You were missing a space before WHERE and you had an unmatched parenthesis.

Consider using parameters: End of statement error

Community
  • 1
  • 1
Fionnuala
  • 90,370
  • 7
  • 114
  • 152