访问代码错误

时间:2013-07-26 13:01:20

标签: ms-access

我有一个表单设置,我可以在访问中向表中添加数据。我有数据输入的空格,然后有一个“添加”按钮来添加数据。但每当我运行它时,我都会收到错误:运行时错误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

1 个答案:

答案 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