vba 为什么在此 MS Access 代码中出现运行时错误 91?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12205496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get Run-time Error 91 in this MS Access code?
提问by Jim P
So, I have a MS Access database application. In this application is a main form, which contains a number of subforms. One form in particular has a drop down box that I populate with dates from a database query. When one of these dates is selected, I run a subroutine that is supposed to update a recordset on the subform with history information. Below is some edited code (just removed the large number of fields from the queries)
所以,我有一个 MS Access 数据库应用程序。在这个应用程序中有一个主窗体,它包含许多子窗体。特别是一种形式有一个下拉框,我用数据库查询中的日期填充。When one of these dates is selected, I run a subroutine that is supposed to update a recordset on the subform with history information. 下面是一些编辑过的代码(刚刚从查询中删除了大量字段)
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
I get the error on the Forms!main!histories.Form.Recordset.AddNew
line.
我在线上收到错误Forms!main!histories.Form.Recordset.AddNew
。
I have tried the following versions of that line:
我已经尝试了该行的以下版本:
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
I am literally at my wit's end trying to figure out where the issue is. The subform has all the proper boxes to store the information. I have given them labels to match their database columns that will go into them. I've tried setting their control sources to the database column names and not setting them to anything. I've looked up a hundred different "solutions", none of which seem to either fit the problem or work.
我真的是在我的智慧尽头试图找出问题所在。子窗体具有所有适当的框来存储信息。我给了他们标签以匹配他们将进入他们的数据库列。我试过将他们的控制源设置为数据库列名,而不是将它们设置为任何内容。我查了一百个不同的“解决方案”,但似乎没有一个适合问题或工作。
I feel like I am overlooking something really easy.
我觉得我忽略了一些非常容易的事情。
回答by Fionnuala
I reckon you have problems with your names. Check all of them. Do not forget that a subform consists of two parts, the subform control and the form contained. These often have the same name, but not always. In the code you are using, you must have the name of the subform control, not the form contained. If entering data into the subform manually is not working properly, your controls are not bound.
我想你的名字有问题。检查所有这些。不要忘记子表单由两部分组成,子表单控件和包含的表单。它们通常具有相同的名称,但并非总是如此。在您使用的代码中,您必须具有子表单控件的名称,而不是包含的表单。如果手动将数据输入子表单无法正常工作,则您的控件未绑定。
This works for me on a sample table.
这在示例表上对我有用。
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
To run a query you could say:
要运行查询,您可以说:
sSQL="INSERT INTO NameOfTable (model, entered_date) " _
& "SELECT model, entered_date FROM history WHERE entered_date=#" _
& Me.pickdate.value & "#"
CurrentDB.execute, dbfailOnError
You can check the sql works in the query design window.
您可以在查询设计窗口中检查 sql 工作。