【问题标题】:How to properly use Seek in DAO database如何在 DAO 数据库中正确使用 Seek
【发布时间】:2016-06-13 22:51:42
【问题描述】:

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

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

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 上有一个带有此代码的子。我从一个示例代码 @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 Operation is not supported for this type of object.

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

【问题讨论】:

    标签: ms-access vba dao


    【解决方案1】:

    在这点从链接页面...

    在索引表类型的 Recordset 对象中定位记录

    ... "table-type Recordset" 表示您必须使用dbOpenTable 而不是dbOpenDynasetOpenRecordset()

    这一点很关键。如果用dbOpenTable打不开表,就不能用Seek。而dbOpenTable 只能与当前数据库中包含的本机 Access 表一起使用。它不能与任何类型的链接表一起使用。

    所以如果dbOpenTabletbl_PROPOSAL兼容的话,这个改动会消除第一个错误...

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

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

    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
    

    【讨论】:

    • 我在使用 dbOpenTable 时收到运行时错误 3219 无效操作。我正在使用 access 2016 以防万一
    • 它是链接访问表。我的前端与我的数据是分开的。数据存储在一个访问文件中。前端使用链接表管理器访问它,但两者都是访问数据库而不是 SQL 数据库。如果我的设置是链接表,我还能用什么来搜索记录? findfirst 会起作用吗?
    • 是的,FindFirst 应该可以工作。它不需要“表型记录集”。
    • 是的,这当然很有用。如果不可能,那么这就是问题的答案,因为它说“如何正确使用......”如果即使链接表是访问表,搜索也无法在链接表中工作,那么你已经回答了这个问题。
    【解决方案2】:

    您不能在链接表上使用 Seek 方法,因为您不能将链接表作为表类型 Recordset 对象打开...

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

    所以而不是:

    Set theDB = CurrentDb()
    

    这样做:

    Set theDB = OpenDatabase("full path to backend database")
    Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenTable)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      相关资源
      最近更新 更多