2016-03-01 76 views
1

我正在嘗試在我的表格的列表框控件中搜索當前選定的項目。如何在DAO數據庫中正確使用搜索

在更新事件之後,我的列表框控件,我有這樣的代碼

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 

然後,我有一子在我模塊1與此代碼。我得到這個從示例代碼@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

回答

2

從鏈接頁面這一點...

定位記錄在索引表類型的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 
+0

我得到運行時錯誤3219當我使用'dbOpenTable'無效操作。我正在使用訪問2016年,以防萬一它是關聯訪問表 – Wayne

+0

。我的前端與我的數據分開。數據存儲在一個訪問文件中。前端使用鏈接表管理器訪問它,但都是訪問數據庫而不是SQL數據庫。如果我的設置是鏈接表,還有什麼可以用來搜索記錄?會發現第一次工作? – Wayne

+0

是的,'FindFirst'應該可以工作。它不需要「表格型記錄集」。 – HansUp

相關問題