如何在DAO数据库中正确使用Seek [英] How to properly use Seek in DAO database

查看:201
本文介绍了如何在DAO数据库中正确使用Seek的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表格的列表框控件中搜索当前选定的项目.

I'm trying to search for currently selected item in my listbox control on my table.

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

In my listbox control after update event, I have this code

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

Then I have a sub on my Module1 with this code. I got this from an example code @ 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操作.

I am getting Runtime Error 3251 Operation is not supported for this type of object.

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

When I hit Debug, it highlights .Seek "=", intSeek

推荐答案

这时从链接页面开始...

In this point from the linked page ...

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

Locates the record in an indexed table-type Recordset object

... 表类型记录集" 表示您必须在OpenRecordset()

... "table-type Recordset" means you must use dbOpenTable instead of dbOpenDynaset with OpenRecordset()

这一点很关键.如果无法使用dbOpenTable打开表,则不能使用Seek.并且dbOpenTable只能与当前数据库中包含的本机Access表一起使用.不能与任何类型的链接表一起使用.

That point is critical. If you can't open the table with dbOpenTable, you can't use Seek. And dbOpenTable can only be used with native Access tables contained in the current database. It can not be used with any kind of linked table.

因此,如果dbOpenTabletbl_PROPOSAL兼容,此更改将消除第一个错误...

So if dbOpenTable is compatible with tbl_PROPOSAL, this change will eliminate the first error ...

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

如果这行得通,则下一个错误将是#3019,没有当前索引的操作无效." 之所以发生,是因为您必须在调用Seek之前设置控制索引. /p>

If that does work, the next error will be #3019, "Operation invalid without a current index." That happens because you must set the controlling index before calling Seek ...

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

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

If you need to list the names of your table's indexes, you can examine its TableDef.Indexes collection. Here is an Immediate window example with a table in my database ...

set db = CurrentDb
for each idx in db.TableDefs("tblFoo").Indexes : ? idx.name : next
id
PrimaryKey

这篇关于如何在DAO数据库中正确使用Seek的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆