代码之家  ›  专栏  ›  技术社区  ›  Wayne

如何在DAO数据库中正确使用Seek

  •  3
  • Wayne  · 技术社区  · 9 年前

    我正试图在我的表的列表框控件中搜索当前选定的项目。

    在更新事件后的列表框控件中,我有以下代码

    Private Sub lst_MainList_AfterUpdate()
        Dim theDB As DAO.Database
        Dim theProposalsTable As DAO.Recordset
    
        Set theDB = CurrentDb
        Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenDynaset)
    
        theSeeker theProposalsTable, Me.lst_PPpg_MainList.Value    
    End Sub
    

    然后我在我的Module1上有一个sub,上面有这个代码。我从一个示例代码中得到了这个@ https://msdn.microsoft.com/en-us/library/office/ff836416.aspx

    Sub theSeeker(ByRef rstTemp As Recordset, intSeek As Integer)
    
       Dim theBookmark As Variant
       Dim theMessage As String
    
       With rstTemp
          ' Store current record location.
          theBookmark = .Bookmark
          .Seek "=", intSeek
    
          ' If Seek method fails, notify user and return to the
          ' last current record.
          If .NoMatch Then
             theMessage = "Not found! Returning to current record." & vbCr & vbCr & "NoMatch = " & .NoMatch
             MsgBox theMessage
             .Bookmark = theBookmark
          End If
       End With
    End Sub
    

    我得到运行时错误3251此类型的对象不支持操作。

    当我点击Debug时,它会突出显示 .Seek "=", intSeek

    3 回复  |  直到 9 年前
        1
  •  3
  •   HansUp    9 年前

    在这一点上,从链接页面。。。

    在索引表类型Recordset对象中查找记录

    ... “表类型记录集” 意味着您必须使用 dbOpenTable 而不是 dbOpenDynaset 具有 OpenRecordset()

    这一点至关重要。如果你不能用 数据库打开表 ,您不能使用 Seek 数据库打开表 只能与当前数据库中包含的本机Access表一起使用。它不能与任何类型的链接表一起使用。

    所以如果 数据库打开表 与兼容 tbl_PROPOSAL ,此更改将消除第一个错误。。。

    'Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenDynaset)
    Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenTable)
    

    如果这确实有效,下一个错误将是#3019, “没有当前索引的操作无效。” 这是因为在调用之前必须设置控制索引 寻找 ...

    With rstTemp
      ' Store current record location.
      theBookmark = .Bookmark
      ' Set the index. 
      .Index = "PrimaryKey" '<- use your index name here
      .Seek "=", intSeek
    

    如果需要列出表索引的名称,可以检查 TableDef.Indexes 收集这是一个即时窗口示例,在我的数据库中有一个表。。。

    set db = CurrentDb
    for each idx in db.TableDefs("tblFoo").Indexes : ? idx.name : next
    id
    PrimaryKey
    
        2
  •  1
  •   David E Meek    4 年前

    您不能使用 寻找 方法,因为无法将链接表作为表类型的记录集对象打开。。。

    但是,如果使用OpenDatabase方法打开后端数据库,则可以使用Seek方法。

    因此,代替:

    Set theDB = CurrentDb()
    

    请执行以下操作:

    Set theDB = OpenDatabase("full path to backend database")
    Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenTable)
    
        3
  •  0
  •   HackSlash    2 年前

    请允许我将这两个旧答案结合起来。是的,打开链接表时出错 dbOpenTable 因为这只在您正在处理的DB对象的本地表上受支持。正如David所指出的,您可以将链接的后端作为DB对象打开并使用它。

    我在后端使用了一个名为“设置”的可靠表。如果您没有一个可靠的表,您可以使用它来拉后端,您可以将表名作为参数传入。

    一旦有了句柄,我就存储后端对象,这样我们可以在整个代码中快速调用它,而无需重新创建对象。

    Private thisBEDB As Database
    
    '@Description("This allows us to call directly against linked tables.")
    Public Function thisBackend() As Database
        ' For MS-ACCESS table
        If (thisBEDB Is Nothing) Then
            With DBEngine
                Set thisBEDB = .OpenDatabase(Mid(CurrentDB.TableDefs("Settings").Connect, 11), False, False, "")
            End With
        End If
        
        Set thisBackend = thisBEDB
    End Function
    

    现在我们可以使用后端句柄使示例中的代码按预期工作。

    Private Sub lst_MainList_AfterUpdate()
        Dim theDB As DAO.Database
        Dim theProposalsTable As DAO.Recordset
    
        Set theDB = CurrentDb
        Set theProposalsTable = thisBackend.OpenRecordset("tbl_PROPOSAL", dbOpenTable)
    
        theSeeker theProposalsTable, Me.lst_PPpg_MainList.Value    
    End Sub
    
    Sub theSeeker(ByRef rstTemp As Recordset, intSeek As Integer)
    
        Dim theBookmark As Variant
        Dim theMessage As String
    
        With rstTemp
            .Index = "PrimaryKey" 
            ' Store current record location.
            theBookmark = .Bookmark
            .Seek "=", intSeek
    
            ' If Seek method fails, notify user and return to the
            ' last current record.
          If .NoMatch Then
             theMessage = "Not found! Returning to current record." & vbCr & vbCr & "NoMatch = " & .NoMatch
             MsgBox theMessage
             .Bookmark = theBookmark
          End If
       End With
    End Sub