为什么我在此MS Access代码中获得运行时错误91?

时间:2012-08-30 21:04:52

标签: vba ms-access

所以,我有一个MS Access数据库应用程序。在此应用程序中是一个主窗体,其中包含许多子窗体。一个表单特别有一个下拉框,我用数据库查询填充日期。当选择其中一个日期时,我运行一个子程序,该子程序应该使用历史信息更新子表单上的记录集。下面是一些经过编辑的代码(刚从查询中删除了大量字段)

Private Sub pickdate_AfterUpdate()
'''''''''''''''''''''''''''''''''''''''''
'   Add review history by selected date
'''''''''''''''''''''''''''''''''''''''''
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT model, entered_date FROM history WHERE entered_date=#" & Me.pickdate.value & "# ORDER BY model DESC", dbOpenDynaset, dbSeeChanges)

If rs.BOF = False Then rs.MoveFirst
While rs.EOF = False

    Forms!main!histories.Form.Recordset.AddNew
    Forms!main!histories.Form.Recordset![model] = rs![model]
    Forms!main!histories.Form.Recordset![entered_date] = rs![entered_date]
    Forms!main!histories.Form.Recordset.Update

    rs.MoveNext
Wend
End Sub

我在Forms!main!histories.Form.Recordset.AddNew行上收到了错误。

我尝试过该行的以下版本:

Forms!main!histories.Form.Recordset.AddNew
main!histories.Form.Recordset.AddNew
histories.Form.Recordset.AddNew
Me.Form.Recordset.AddNew
Me.Recordset.AddNew
Me.AddNew
Me.main!histories.Form.Recordset.AddNew
Me!histories.Form.Recordset.Addnew
Me!main!histories.Form.Recordset.AddNew

我确实在我的智慧结束时试图找出问题所在。 子表单具有存储信息的所有适当的框。我给了他们标签以匹配他们将进入他们的数据库列。我已经尝试将其控件源设置为数据库列名,而不是将它们设置为任何内容。我查阅了一百种不同的“解决方案”,其中任何一种似乎都不适合问题或工作。

我觉得我很容易忽视一些事情。

1 个答案:

答案 0 :(得分:2)

我估计你的名字有问题。检查所有这些。不要忘记子表单由两部分组成,子表单控件和包含的表单。这些通常具有相同的名称,但并非总是如此。在您使用的代码中,您必须具有子窗体控件的名称,而不是包含的窗体。如果手动将数据输入到子窗体中无法正常工作,则不会绑定控件。

这适用于我的样本表。

Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT atext from table1 WHERE akey=21")

If rs.BOF = False Then rs.MoveFirst
While Not rs.EOF '= False
    Me.Table1_subform1.Form.Recordset.AddNew
    Me.Table1_subform1.Form.Recordset!AText = rs!AText
    Me.Table1_subform1.Form.Recordset.Update

    rs.MoveNext
Wend

要运行查询,您可以说:

sSQL="INSERT INTO NameOfTable (model, entered_date) " _
& "SELECT model, entered_date FROM history WHERE entered_date=#" _
& Me.pickdate.value & "#"

CurrentDB.execute, dbfailOnError

您可以在查询设计窗口中检查sql是否正常工作。