MaxRecords Property Example

This example uses the MaxRecords property to set a limit on how many records are returned by a query on an ODBC data source.

Sub MaxRecordsX()

   Dim dbsCurrent As Database
   Dim qdfPassThrough As QueryDef
   Dim qdfLocal As QueryDef
   Dim rstTemp As Recordset

   ' Open a database from which QueryDef objects can be
   ' created.
   Set dbsCurrent = OpenDatabase("DB1.mdb")

   ' Create a pass-through query to retrieve data from
   ' a Microsoft SQL Server database.
   Set qdfPassThrough = _
      dbsCurrent.CreateQueryDef("")

   ' Set the properties of the new query, limiting the 
   ' number of returnable records to 20.
   qdfPassThrough.Connect = _
      "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers"
   qdfPassThrough.SQL = "SELECT * FROM titles"
   qdfPassThrough.ReturnsRecords = True
   qdfPassThrough.MaxRecords = 20

   Set rstTemp = qdfPassThrough.OpenRecordset()

   ' Display results of query.
   Debug.Print "Query results:"
   With rstTemp
      Do While Not .EOF
         Debug.Print , .Fields(0), .Fields(1)
         .MoveNext
      Loop
      .Close
   End With

   dbsCurrent.Close

End Sub