Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click
References on the
Tools menu in the Visual Basic Editor, and make sure that the
Microsoft DAO 3.6 Object Library check box is selected.
To use the
Seek method on a linked table from the sample database
Northwind.mdb, follow these steps:
- Create a new database and name it Db1.mdb.
- On the File menu, click Get External Data, and then click Link Tables.
- Select the Northwind.mdb file, and then click Link.
- In the Link Tables dialog box, select Orders, and then click OK.
- Create a module, and then type the following line in the Declarations section if the line is not already there:
Option Explicit
- Type or paste the following procedure:NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character.
Sub Seek_Attached_Table(Tablename, Indexname, SearchValue)
Dim db As Database
Dim t As TableDef
Dim rs As DAO.Recordset
Dim dbpath, SourceTable
On Error GoTo SA_Errors
Set db = DBEngine(0)(0)
dbpath = Mid(db(Tablename).connect, InStr(1, _
db(Tablename).connect, "=") + 1)
If dbpath = "" Then MsgBox "You've chosen a table already in the current database", 64, "": Exit Sub
SourceTable = db(Tablename).sourcetablename
Set db = DBEngine(0).OpenDatabase(dbpath)
Set rs = db.OpenRecordset(SourceTable, DB_OPEN_TABLE)
rs.Index = Indexname
rs.Seek "=", SearchValue
If Not rs.NoMatch Then
MsgBox "Found It!", 64
Else
MsgBox "Not Found", 64
End If
rs.Close
db.Close
Exit Sub
SA_Errors:
MsgBox Error, 16, CStr(Err)
Exit Sub
End Sub
- To run the Sub procedure, type the following line in the Immediate window, and then press ENTER:
Seek_Attached_Table "Orders","PrimaryKey",11000
Note that the message "Found It!" appears.