简体   繁体   English

尝试使用记录集书签更新记录ID时出现“编译错误:限定符必须是集合”错误

[英]“Compile error: qualifier must be a collection” error when trying to update record ID with recordset Bookmark

How would I address the "Compile error: qualifier must be a collection" on the indicated line in the code below? 如何在下面的代码中指定的行上解决“编译错误:限定符必须是集合”?

I basically have two tables: items ( ID , title , amount ) and relationships ( parentId , clientId ). 我基本上有两个表: itemsIDtitleamount )和relationshipsparentIdclientId )。 I'm basically just trying here to create a new record in items , and then creating a new relationships table record as well. 我基本上只是在这里尝试在items创建一个新记录,然后再创建一个新的relationships表记录。

I'm not sure how to allocate the the new items record (just created) to the new relationships record's childId field. 我不确定如何将新items记录(刚刚创建)分配给新relationships记录的childId字段。

Private Sub Command18_Click()
    Debug.Print ("*** Starting ***")

    ' New Record
    Dim rsItems As Recordset
    Set rsItems = CurrentDb.OpenRecordset("items")
    rsItems.AddNew
    rsItems![title] = "title"
    rsItems![amount] = 123
    rsItems.Update

    ' Get new ID
    rsItems.Bookmark = rsItems.LastModified
    newId = rsItems.Bookmark
    Debug.Print ("New ITEM record with ID " & newId)

    ' Relationships
    Dim rsRelationship As Recordset
    Set rsRelationship = CurrentDb.OpenRecordset("relationships")
    rsRelationship.AddNew
    'Debug.Print ("Relationships Field Types: " & TypeName(gcItemParentId) & ", " & TypeName(rsItems.LastModified!ID))
    rsRelationship![parentId] = gcItemParentId  'taken from text box on main form that holds current parent ID
    rsRelationship![clientId] = rsItems.LastModified!["ID"] ' *** ERROR HERE *** 
    rsRelationship.Update

    ' Get new ID
    rsRelationship.Bookmark = rsRelationship.LastModified
    newId = rsItems.Bookmark
    Debug.Print ("New RELATIONSHIP record with ID " & newId)

    ' Refresh Form
    Me.Refresh

    ' Cleanup
    rsItems.Close
    rsRelationship.Close
    Set rsItems = Nothing
    Set rsRelationship = Nothing

End Sub

You're getting this error because you're trying to use LastModified and Bookmark to represent IDs in records rather than just record positions in recordsets. 之所以出现此错误,是因为您试图使用LastModifiedBookmark代表记录中的ID,而不仅仅是记录集中的记录位置。 They can't do that. 他们不能那样做。

What you need instead of newId = rsItems.Bookmark , therefore, is newId = rsItems!ID . 因此,您需要的是newId = rsItems!ID而不是newId = rsItems.Bookmark Once you have the primary key for the record you just added, you'll use it with rsRelationship!clientId = newId . 获得刚刚添加的记录的主键后,将其与rsRelationship!clientId = newId (You should probably also add a primary key field named RelationshipID [although the name is not really important] to the Relationships table. In Access, all tables that have enforceable relationships [in the Access sense] must have primary keys, and generally just about all tables should have them whether they technically need them or not. Mostly because all tables that can theoretically have enforceable relationships should; Access is far better at maintaining data integrity and making easy-to-use links than hand-written code usually is.) (您可能还应该将一个名为RelationshipID的主键字段[尽管名称不是很重要]添加到Relationships表中。在Access中,所有具有可强制执行的关系(在Access方面)的表都必须具有主键,并且通常只有所有表都应该具有它们,无论它们在技术上是否需要它们。主要是因为所有可以在理论上具有可强制执行的关系的表都应该这样;与通常的手写代码相比,访问在维护数据完整性和建立易于使用的链接方面要好得多。

The code as a whole, then, would look something like this, debugging statements, error handling, and redundant comments removed: 然后,整个代码看起来像这样,调试语句,错误处理和多余的注释被删除了:

Private Sub Command18_Click()
    Dim rsItems As Recordset
    Set rsItems = CurrentDb.OpenRecordset("items")
    rsItems.AddNew
    rsItems!title = "title"
    rsItems!amount = 123
    rsItems.Update
    ' Go back to the record we just saved
    rsItems.Bookmark = rsItems.LastModified
    newId = rsItems!ID

    Dim rsRelationship As Recordset
    Set rsRelationship = CurrentDb.OpenRecordset("relationships")
    rsRelationship.AddNew
    rsRelationship!parentId = gcItemParentId
    rsRelationship!clientId = newId
    rsRelationship.Update

    Me.Refresh

    rsItems.Close
    rsRelationship.Close    
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM