Author: Calvin Smith http://www.CalvinSmithSoftware.com/AllADO.htm
The following code will allow a developer to programmatically seek the value of a column via ADO and the related Primary Key value. |
Function SeekRecordUsingADO() As Boolean On Error GoTo ErrorHandling_Err ' -------------------------------------------------------------------- ' Purpose: Example of how to seek a value using ADO and a Primary Key ' -------------------------------------------------------------------- Dim conn As ADODB.Connection Dim rstMyRecordSet As ADODB.Recordset Set conn = New ADODB.Connection Set rstMyRecordSet = New ADODB.Recordset 'Set the connection properties and open the connection. With conn .Provider = "Microsoft.Jet.OLEDB.4.0" .ConnectionString = "C:\Calvin\TestAnything.mdb" .Open End With With rstMyRecordSet 'Select the index used in the recordset. .Index = "PrimaryKey" 'Set the location of the cursor service. .CursorLocation = adUseServer 'Open the recordset. .Open "tblYourTableName", conn, adOpenKeyset, _ adLockOptimistic, adCmdTableDirect 'Find the record where my primary key column = 5. .Seek 5, adSeekFirstEQ 'If a match is found, print the value of your column. If Not rstMyRecordSet.EOF Then Debug.Print rstMyRecordSet.Fields("YourColumnName").Value End If End With SeekRecordUsingADO = (Err = 0) ErrorHandling_Err: If Err Then 'Trap your error(s) here, if any! End If End Function