0

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

1 回答 1

2

Execute是一种方法。不要试图将其设置为等于某事。丢弃=标志。

CodeStorage.Execute "INSERT INTO KWTable(KW, Source, Code) " & _
    " VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
    Me.combo_source & "')"

假设CodeStorage是一个有效的 DAO.Database,这可能会起作用。如果您遇到另一个错误,请告诉我们如果您将 SQL 保存到查询设计器中的新查询,CodeStorage以及您的语句是否有效。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
于 2013-07-26T13:14:12.873 回答