XTreme Logo

If you appreciate these tips, please consider a small donation:

Database Tip - by a guest contributor

Example of Opening and Closing Recordset, then Opening a Different Table with the DataEnvironment
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 steps and code below will open a Recordset against the Shippers table in the Northwind sample database using the DataEnvironment Connection and Command Objects. It will then close this Recordset, and open one against the Employees table in the same database.

1. Create a Standard EXE project in Visual Basic. Form1 is created by default.

2. Add a DataEnvironment to the project. Connection1 is created by default.

3. Set Connection1 to use the OLEDB Provider for ODBC Drivers to connect to the Nwind.mdb sample database.

4. Add a command to Connection1 based on the following query:
"SELECT * FROM Shippers"

5. Right-click and drag the command onto Form1 and choose "Data Grid."
(FYI: this is a short-cut, that you should not use on a regular basis; it can cause the bindings to get lost later.)

6. Add a CommandButton to Form1 from the toolbox.

7. Paste the following code in the General Declaration section of your code window.

Option Explicit

Private Sub Command1_Click()
    ' Close the recordset to the Shippers table.
With DataEnvironment1
' Set the Command Source to point to the new table.
        .rsCommand1.Source = "Select * from Employees"
' Open the new Command recordset.
End With
    ' Reset the binding for the DataGrid.
Set DataGrid1.DataSource = DataEnvironment1
' Display a recordcount to show new recordset.
    MsgBox DataEnvironment1.rsCommand1.RecordCount
End Sub

Private Sub Form_Load()
    Command1.Caption = "Close Shippers and Open Employees Table"
End Sub

[ Back To The Top ]

Contact: web@xtremecomp.com
All contents copyright XTreme Computing unless noted otherwise.