对象变量错误 [英] Object variable error
问题描述
我有一个窗体(RegistrationFsub)的基础上表登记。它的目的是为了创造一个人与去年(),他们都参加了小组。之间有一个一对多的关系
I've got a subform (RegistrationFsub) based on table Registration. It's purpose is to create a one-to-many relationship between a person and the year(s) they have enrolled in the group.
当一个妈妈招收在新的一年里,我有一个命令按钮时的应该的一个新行添加到表登记与MomID(从父窗体:MomsFsub)和起始日期目前报名一年(YearStart,从表年)。由于目前报名期间重新采用最新的日期psented $ P $,我想使用MAX()命令从YearT检索。我尝试以下code:
When a mom enrolls in a new year, I have a command button that is supposed to add a new line to the table Registration with MomID (from the parent form: MomsFsub) and the start date of the current enrollment year (YearStart, from table Year). Since the current enrollment period is represented by the latest date, I want to use the Max() command to retrieve it from YearT. I tried the following code:
Dim db As DAO.Database
Dim sqlSTR As String
Dim IDvar As String
'new code added since question posted
Set db = CurrentDb
Call MsgBox (Max(YearT!YearStart), vbOKonly)
'MsgBox checks value returned for Max(YearStart)
'end new code
IDvar = CStr(MomID)
sqlSTR = "INSERT INTO Registration(YearStart, MomID) SELECT Max(YearStart), "_
& IDvar & " AS expr1 FROM YearT;"
'new: debug statement
Debug.Print sqlSTR
db.Execute sqlSTR
和我得到了一个对象变量或带块变量未设置错误。我究竟做错了什么?
And I got an "Object variable or With block variable not set" error. What am I doing wrong?
编辑:设置分贝Currentdb修复对象变量错误,但现在返回一个参数太少的错误。原始表名年已经更改为YearT,因为我只引用它在code这一位呢。
Setting the db to Currentdb fixes the Object variable error, but now returns a "Too few parameters" error. The original table name "Year" has been changed to "YearT," since I only reference it in this one bit of code anyway.
更新
现在,我已经固定RegistrationFsub,似乎按钮的也的目前在窗体上的其他控件显示插入的数据。因此,如果2012年的条目有 RID = 1
和领袖= TRUE
,上述code创建一个条目注册
的的也的有 RID = 1
和领袖= TRUE
。我怎样才能保持与其他领域的空白?
Now that I've fixed RegistrationFsub, it seems that the button also inserts data currently displayed in other controls on the form. So if the 2012 entry has RID = 1
and Leader = True
, the above code creates an entry in Registration
that also has RID = 1
and Leader = True
. How can I keep those other fields blank?
推荐答案
如果您要添加的基础上妈妈的当前新进入一个新的记录,你需要采取当前时间:NOW(),并解析一年下来吧..
If you are adding a new record based on the Mom's current new entry, you need to take the current time: Now() and parse the year off of it..
Year(Now())
寻找最大(YearStart)可寻找发生在5年前的记录。
Looking for Max(YearStart) could be looking for a record that happened 5 years ago..
sqlSTR = "INSERT INTO Registration(YearStart, MomID) SELECT Max(YearStart), "_
& IDvar & " AS expr1 FROM Year;"
我认为你需要更新code到两种不同的操作:
I think you need to update the code to two different operations:
sqlSTR = "INSERT INTO Registration(Year(Now()), MomID)"
运行code .. 然后做一个..
run your code.. Then do a..
sqlSTR= "SELECT Year(Now()), " & IDvar & " AS expr1 FROM [Year];"
这篇关于对象变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!