Jak znaleźć rekord za pomocą bazy danych ADO i dostawcy bazy danych OLE Jet
Uwaga
Nazwa usługi Office 365 ProPlus została zmieniona na Aplikacje usługi Microsoft 365 dla przedsiębiorstw. Aby uzyskać więcej informacji na temat tej zmiany, przeczytaj ten wpis w blogu.
Zaawansowane. Wymaga kodowania eksperta, współdziałania i umiejętności wielu użytkowników.
Ten artykuł dotyczy pliku bazy danych programu Microsoft Access (mdb) lub pliku bazy danych programu Microsoft Access (accdb).
Podsumowanie
W tym artykule pokazano, jak używać obiektów ActiveX Data Objects (ADO) i bazy danych OLE w celu znalezienia rekordów w bazie danych programu Microsoft Jet.
Więcej informacji
Poniżej znajdują się dwie przykładowe procedury. Pierwsza baza danych, CreateJetDB, tworzy nową bazę danych Microsoft Jet w katalogu głównym dysku C i wypełnia ją danymi. Drugi, CursorLocationTimed, przedstawia sposób używania metody Find z kursorem po stronie serwera i kursorem po stronie klienta.
Aby utworzyć te procedury, wykonaj następujące czynności:
Utwórz nową bazę danych programu Microsoft Access.
Utwórz nowy moduł.
W menu Narzędzia kliknij polecenie Odwołania i upewnij się, że są zaznaczone następujące odwołania:
Biblioteka obiektów Microsoft ActiveX Data Objects 2.1 lub nowsza wersja
Microsoft ADO Ext. 2.1 dla języka DDL i zabezpieczeń lub nowsza wersja
Wpisz lub wklej następujące procedury:
Sub CreateJetDB()
Dim cat As ADOX.Catalog
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim numrecords As Long
Dim i As Long
Set cat = New ADOX.Catalog
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Number of sample records to create
numrecords = 250000
On Error Resume Next
'Delete the sample database if it already exists.
'Change "findseek.mdb" to "findseek.accdb" for Access 2007.
'Change the Provider to "Microsoft.ACE.OLEDB.12.0" for
'Access 2007 ACCDB databases.
Kill "c:\findseek.mdb"
On Error GoTo 0
' Create a new Jet 4.0 database name findseek.mdb
'Change "findseek.mdb" to "findseek.accdb" for Access 2007.
'Change the Provider to "Microsoft.ACE.OLEDB.12.0" for
'Access 2007 ACCDB databases.
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\findseek.mdb"
' Set the provider, open the database,
'and create a new table called tblSequential.
'Change "findseek.mdb" to "findseek.accdb" for Access 2007.
'Change the Provider to "Microsoft.ACE.OLEDB.12.0" for
'Access 2007 ACCDB databases.
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.Open "Data Source=c:\findseek.mdb"
cn.Execute "CREATE TABLE tblSequential (col1 long, col2 text(75));"
'Open the new table.
rs.Open "tblSequential", cn, adOpenDynamic, _
adLockOptimistic, adCmdTableDirect
' Add sample records to the tblSequential table.
For i = 0 To numrecords
rs.AddNew
rs.Fields("col1").Value = i
rs.Fields("col2").Value = "value_" & i
rs.Update
Next i
rs.Close
'Create a multifield Index on col1 and col2.
cn.Execute "CREATE INDEX idxSeqInt on tblSequential (col1, col2);"
'Close the connection
cn.Close
End Sub
Sub CursorLocationTimed()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i, j As Long
Dim time As Variant
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo ErrHandler
'Change "findseek.mdb" to "findseek.accdb" for Access 2007.
'Change the Provider to "Microsoft.ACE.OLEDB.12.0" for
'Access 2007 ACCDB databases.
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=c:\findseek.mdb"
' Specify how ADO should open the recordset:
' adUseServer - use the native provider to perform cursor
' operations
' adUseClient - use the client cursor engine in ADO
' NOTE: adUseServer more closely resembles DAO
' Time opening a recordset and doing 1000 finds (Server cursor
' engine)
'
rs.CursorLocation = adUseServer
time = Timer
' Open the recordset and perform serveral Finds to locate records.
' Using the adCmdTableDirect opens a base table against Jet, which
' is generally the fastest, most functional way to access tables.
rs.Open "tblSequential", cn, adOpenDynamic, adLockOptimistic, _
adCmdTableDirect
For i = 0 To 1000
rs.Find "col1=" & i
Next i
Debug.Print "Sequential Find + Open (Server) = " & Timer - time
rs.Close
' Time opening a recordset and doing 1000 finds (Client cursor
' engine)
rs.CursorLocation = adUseClient
time = Timer
rs.Open "tblSequential", cn, adOpenDynamic, _
adLockOptimistic, adCmdTableDirect
For i = 0 To 1000
rs.Find "col1=" & i
Next i
Debug.Print "Sequential Find + Open (Client) = " & Timer - time
rs.Close
Exit Sub
ErrHandler:
For j = 0 To cn.Errors.Count - 1
Debug.Print "Conn Err Num : "; cn.Errors(j).Number
Debug.Print "Conn Err Desc: "; cn.Errors(j).Description
Next j
Resume Next
End Sub
Aby utworzyć przykładową bazę danych, wpisz następujący wiersz w oknie bezpośrednim i naciśnij klawisz ENTER:
CreateJetDB
Aby pokazać metodę Find, wpisz następujący wiersz w oknie bezpośrednim i naciśnij klawisz ENTER:
CursorLocationTimed
Dane wyjściowe powinny przypominać następujące:
Sequential Find + Open (Server) = 0,28125
Sequential Find + Open (Client) = 5,28125
UWAGA Liczby wynikowe mogą różnić się od liczby na komputerze.
Informacje
Aby dowiedzieć się więcej o obiektach ActiveX Data Objects, zobacz Obiekty Microsoft ActiveX Data Objects (ADO).