我有一个表单设置,我可以在访问中向表中添加数据。我有数据输入的空格,然后有一个“添加”按钮来添加数据。但每当我运行它时,我都会收到错误:运行时错误424对象需要。
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. For insert
'2. For Update
If Me.txt_code.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute = "INSERT INTO KWTable(KW, Source, Code) " & _
" VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
Me.combo_source & "')"
Else
'otherwise (Tag of txtID store the id of student to be modified)
CurrentDb.Execute "UPDATE KWTable " & _
" SET KW=" & Me.text_key & _
", Code='" & Me.txt_code & "'" & _
", Source='" & Me.combo_source & "'" & _
" WHERE KW=" & Me.text_key
End If
'clear form
cmdClear_Click
'refresh data in list on form
TableSub.Form.Requery
End Sub
答案 0 :(得分:2)
Execute
是一种方法。不要试图将它设置为等于某物。放弃=
标志。
CodeStorage.Execute "INSERT INTO KWTable(KW, Source, Code) " & _
" VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
Me.combo_source & "')"
假设CodeStorage
是有效的DAO.Database,那可能会有效。如果您收到其他错误,请告诉我们CodeStorage
以及如果将SQL保存到查询设计器中的新查询,INSERT
语句是否有效。
Dim strInsert As String
strInsert = "INSERT INTO KWTable(KW, Source, Code) " & _
" VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
Me.combo_source & "')"
Debug.Print strInsert
CodeStorage.Execute strInsert
然后,您可以在立即窗口中检查已完成的INSERT
语句。您可以使用 Ctrl + g 去那里。
在您评论的评论CodeStorage
实际上是一张桌子。表没有Execute
方法。使用Execute
对象中的DAO.Database
,例如CurrentDb
。
CurrentDb.Execute strInsert