Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Example 1:
Dim con as ADODB.Connection
Set con = New.ADODB.Connection
'set the connection string property
con.ConnectionString = "Provider=MSDASQL.1; " _
& "Data Source=BiblioDSN"
'open the Connection object with the connection string set above
con.Open
OR
'set the connection string property with the open method
con.Open "Provider=MSDASQL.1; Data Source=BiblioDSN"
Example 2:
Dim con as ADODB.Connection
Dim rst as ADODB.Recordset
Set con = New ADODB.Connection
Set rst = New ADODB.Recordset
ado_connection_object.doc
Page 1 of 8
ado_connection_object.doc
Page 2 of 8
ado_connection_object.doc
Page 3 of 8
ADO Recordsets
Creating a recordset
In addition to creating a recordset with the Execute method, as
described above, recordsets have methods and properties that can be
used.
Dim rs AS ADODB.Recordset
Set rs = New ADODB.Recordset
Set the ActiveConnection property to an ADO Connection object:
rs.ActiveConnection = cn
OR
rs.ActiveConnection = "DSN=BIBLIO"
If you use the string value instead of a connection object, the
recordset will open its own connection to the database.
ado_connection_object.doc
Page 4 of 8
CursorType Property
adOpenForwardOnly
adOpenKeySet
adOpenDynamic
adOpenStatic
LockType Property
adLockReadOnly
adLockPessimistic
adLockOptimistic
adLockBatchOptimistic
ado_connection_object.doc
Page 5 of 8
Deleting Records
rs.Open "SELECT Author, Books FROM Authors WHERE Books = 10"
Do While Not rs.EOF
rs.Delete
rs.MoveNext
Loop
Delete all records where Book = 10.
ado_connection_object.doc
Page 6 of 8
Recordset Filters
The filter property of the Recordset object allows you to filter a recordset
even after it has been created.
Using a search criteria string.
rs.Open "Authors", "DSN=BiblioDSN", adOpenKeyset
rs.Filter = "[LastName]='Twain'"
rs.Filter = "[Books]=12"
rs.Close
Recordset Sorting
Records can be sorted on an opened recordset.
A list of comma-delimited fields set which fields to sort.
DESC or ASC can be added after a space to any field in the list.
The sort property only works on client-side cursors.
rs.CursorLocation = adUseClient
rs.Open "Authors", "DSN=BiblioDSN"
rs.Sort = "[Books] DESC, Author"
rs.Close
Find Records
rs.Open "Authors", "DSN=BiblioDSN"
rs.Find "Authors='Dickens'"
Record Navigation
MoveNext and MovePrevious will set EOF and BOF to true. The record
pointer is not at a valid record.
rs.Open "Authors", "DSN=BiblioDSN"
rs.MoveFirst
rs.MoveNext
rs.MovePrevious
rs.MoveLast
ado_connection_object.doc
Page 7 of 8
Command Object
In MS Access, create a generic query with SQL:
PARAMETERS Letters Text;
SELECT *
FROM Authors
WHERE Author Like Letter+'*';
Running this query in Access will prompt for the Letter parameter.
To run this query from code, use a command object.
Dim cmd As ADODB.Command
Dim parmTemp As ADODB.Parameter
Set cmd.ActiveConnection = cn
cmd.CommandText = "LetterLookup"
cmd.CommandType = adCmdStoredProc
Set parmTemp = cmd.CreateParameter("Letter", adChar, adPaaramInput, 1)
cmd.Parameters.Append parmTemp
cmd("Letter") = "Q"
Set rs = cmd.Execute
CommandTypes
adCmdText
adCmdStoredProc
adCmdTable
adCmdUnknown
ado_connection_object.doc
Page 8 of 8