2013-07-26 57 views
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 
+0

被CodeStorage甚至定义? – basdwarf

+0

你能多给点信息吗?你用来添加数据的代码是什么? CodeStorage被定义为字符串吗? – Gutanoth

回答

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 + 克去那里。

在你报CodeStorage评论实际上是一个表。表格没有Execute方法。使用来自DAO.Database对象的Execute,例如CurrentDb

CurrentDb.Execute strInsert 
+0

我确实得到了同样的错误。 CodeStorage是容纳所有数据的表格。这三列是KW,Source,Code。 – user2119980

+0

我根据这些信息修改了答案。但是我很困惑,为什么你想用一个表CodeStorage来把'INSERT'变成另一个表'KWTable'。我怀疑这里有些东西我不明白。 – HansUp

+0

我改变了对CodeStorage和CurrentDB是解决这个问题,但现在我有错误:编译错误:参数不可选,现在私家SUV cmdAdd_Click()标题强调。 – user2119980