如何在DAO数据库中正确使用Seek
How to properly use Seek in DAO database
我正在尝试在 table 上的列表框控件中搜索当前选定的项目。
在更新事件后的列表框控件中,我有这段代码
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 此类对象不支持操作。
当我点击调试时,它突出显示 .Seek "=", intSeek
在这一点上从链接页面...
Locates the record in an indexed table-type Recordset object
... "table-type Recordset" 意味着您必须使用 dbOpenTable
而不是 dbOpenDynaset
和 OpenRecordset()
这一点很关键。用dbOpenTable
打不开table,用Seek
打不开。并且 dbOpenTable
只能与当前数据库中包含的本机 Access table 一起使用。它不能与任何类型的链接一起使用 table.
因此,如果 dbOpenTable
与 tbl_PROPOSAL
兼容,此更改将消除第一个错误 ...
'Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenDynaset)
Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenTable)
如果确实有效,下一个错误将是#3019,"Operation invalid without a current index." 发生这种情况是因为您必须在调用 Seek
之前设置控制索引。 ..
With rstTemp
' Store current record location.
theBookmark = .Bookmark
' Set the index.
.Index = "PrimaryKey" '<- use your index name here
.Seek "=", intSeek
如果您需要列出 table 索引的名称,您可以检查它的 TableDef.Indexes
集合。这是一个即时 window 示例,在我的数据库中有一个 table ...
set db = CurrentDb
for each idx in db.TableDefs("tblFoo").Indexes : ? idx.name : next
id
PrimaryKey
您不能在链接的 table 上使用 Seek 方法,因为您无法将链接的 table 作为 table 打开-类型 Recordset 对象...
不过,如果使用OpenDatabase方法打开后台数据库,则可以使用Seek方法。
所以代替:
Set theDB = CurrentDb()
这样做:
Set theDB = OpenDatabase("full path to backend database")
Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenTable)
我正在尝试在 table 上的列表框控件中搜索当前选定的项目。
在更新事件后的列表框控件中,我有这段代码
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 此类对象不支持操作。
当我点击调试时,它突出显示 .Seek "=", intSeek
在这一点上从链接页面...
Locates the record in an indexed table-type Recordset object
... "table-type Recordset" 意味着您必须使用 dbOpenTable
而不是 dbOpenDynaset
和 OpenRecordset()
这一点很关键。用dbOpenTable
打不开table,用Seek
打不开。并且 dbOpenTable
只能与当前数据库中包含的本机 Access table 一起使用。它不能与任何类型的链接一起使用 table.
因此,如果 dbOpenTable
与 tbl_PROPOSAL
兼容,此更改将消除第一个错误 ...
'Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenDynaset)
Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenTable)
如果确实有效,下一个错误将是#3019,"Operation invalid without a current index." 发生这种情况是因为您必须在调用 Seek
之前设置控制索引。 ..
With rstTemp
' Store current record location.
theBookmark = .Bookmark
' Set the index.
.Index = "PrimaryKey" '<- use your index name here
.Seek "=", intSeek
如果您需要列出 table 索引的名称,您可以检查它的 TableDef.Indexes
集合。这是一个即时 window 示例,在我的数据库中有一个 table ...
set db = CurrentDb
for each idx in db.TableDefs("tblFoo").Indexes : ? idx.name : next
id
PrimaryKey
您不能在链接的 table 上使用 Seek 方法,因为您无法将链接的 table 作为 table 打开-类型 Recordset 对象...
不过,如果使用OpenDatabase方法打开后台数据库,则可以使用Seek方法。
所以代替:
Set theDB = CurrentDb()
这样做:
Set theDB = OpenDatabase("full path to backend database")
Set theProposalsTable = theDB.OpenRecordset("tbl_PROPOSAL", dbOpenTable)