Contents Index Executing statements with the Command object Working with Recordset object

ASA Programming Guide
  The OLE DB and ADO Programming Interfaces
    ADO programming with Adaptive Server Anywhere

Querying the database with the Recordset object


The ADO Recordset object represents the result set of a query. You can use it to view data from a database.

Sample code 

You can try this routine by placing a command button named cmdQuery on a form and pasting the routine into its Click event. Run the program and click the button to connect, display a message on the database server window, execute a query and display the first few rows in message boxes, and then disconnect.

Private Sub cmdQuery_Click()
' Declare variables
    Dim myConn As New ADODB.Connection
    Dim myCommand As New ADODB.Command
    Dim myRS As New ADODB.Recordset

    On Error GoTo ErrorHandler:

    ' Establish the connection
    myConn.Provider = "ASAProv"
    myConn.ConnectionString = _
      "Data Source=ASA 9.0 Sample"
    myConn.CursorLocation = adUseServer
    myConn.Mode = adModeReadWrite
    myConn.IsolationLevel = adXactCursorStability
    myConn.Open

    'Execute a query
    Set myRS = New Recordset
    myRS.CacheSize = 50
    myRS.Source = "Select * from customer"
    myRS.ActiveConnection = myConn
    myRS.CursorType = adOpenKeyset
    myRS.LockType = adLockOptimistic
    myRS.Open

    'Scroll through the first few results
    myRS.MoveFirst
    For i = 1 To 5
      MsgBox myRS.Fields("company_name"), vbInformation
      myRS.MoveNext
    Next

    myRS.Close
    myConn.Close
    Exit Sub
ErrorHandler:
    MsgBox Error(Err)
    Exit Sub
End Sub
Notes 

The Recordset object in this example holds the results from a query on the Customer table. The For loop scrolls through the first several rows and displays the company_name value for each row.

This is a simple example of using a cursor from ADO.

For more advanced examples of using a cursor from ADO, see Working with Recordset object.


Contents Index Executing statements with the Command object Working with Recordset object