Visual Basic Concepts

Running a Basic Query

See Also

RDO

This event procedure returns a resultset based on an SQL statement. It performs a restricted query and passes the resultset to a control that inserts the resultant data into an MSHFlexGrid control. Note that building a resultset requires an open connection.

Private Sub RunButton_Click()
   Dim rs As rdoResultset
   Set rs = cn.OpenResultset("select * from titles where title _
    like '%h'")
   rdoGrid1.ShowData rs
   rs.Close
End Sub

ADO

Once the database connection is open, you can run a query against it. The following event procedure is very similar to the previous RDO code. In this case, however, you use the new ADO Open method that takes the SQL query and the ADO Connection object as arguments, rather than using the rdoConnection object's OpenResultset method. You can also opt to use the ADO Connection object’s Execute method, just as you could in RDO (as long as it didn’t return a rowset).

A primary difference of ADO2 compared with RDO2 is that ADO2 allows you to create a recordset and set its properties before you open the recordset.

Private Sub RunButton_Click()
   Dim rs As New ADODB.Recordset
   rs.Open "select * from titles where title like '%h'", cn
   ADOGrid1.ShowData rs
   rs.Close
End Sub

You can run this query and process its resultset asynchronously in ADO. That is, when you specify the adFetchAsynch option on rs.Open, ADO causes the cursor provider to automatically populate the resultset in the background.