Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - Programming » SQL Anywhere OLE DB and ADO APIs » ADO programming with SQL Anywhere

Executing statements with the Command object Next Page

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 in the Server Messages 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 = "SAOLEDB"
    myConn.ConnectionString = _
      "Data Source=SQL Anywhere 10 Demo"
    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 Customers"
    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("CompanyName"), 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 Customers table. The For loop scrolls through the first several rows and displays the CompanyName 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 the Recordset object.