Returning Recordcount with ADO
Tested with VB6 SP1
This VB Tip is provided
courtesy of our friend Aslan. When
you're done here, be sure to check out her VBDelight web site for more delightful
info!
The following ADO code should successfully
return a RecordCount for the Shippers table in the Northwind sample database that comes
with Visual Basic, before and after updating the database with a new value.
Test the following code in New StdEXE project.
1. Add a Reference to the Microsoft ActiveX Data Objects 2.0 Library.
2. Add two Label controls to the Form.
3. Paste the following code in the General Declarations section of your code window.
Option Explicit
Private cn As New
ADODB.Connection
Private cmd As New
ADODB.Command
Private rs As New
ADODB.Recordset
Private Sub Form_Load()
' This is the
key step, you must use a Client Side Cursor.
cn.CursorLocation = adUseClient
' You will
have to change the path to point at the NorthWind database.
cn.Open "Provider=MSDASQL;" & _
"Driver={Microsoft
Access Driver (*.mdb)};" & _
"DBQ=C:\Program
Files\Microsoft Visual Studio\VB98\NWIND.MDB;"
cmd.CommandType = adCmdText
cmd.CommandText = "Select * from Shippers"
Set cmd.ActiveConnection = cn
' Open the
Recordset.
rs.Open cmd, , adOpenKeyset, adLockOptimistic
' Display the
record count before adding a new record.
Label1.Caption = "Records in recordset = " &
rs.RecordCount
' Add a new
record.
rs.AddNew
rs.Fields("CompanyName") = "Federal Express"
rs.Update
' Display the
record count after adding a new record.
Label2.Caption = "Records in recordset = " &
rs.RecordCount
End Sub
|