Limiting the Number of Records Returned

When working with an ODBC data source, you can limit the number of records returned to a QueryDef object by setting the MaxRecords property. For example, the following code fragment creates a QueryDef object against the Microsoft SQL Server Pubs sample database and sets the MaxRecords property to limit the maximum number of records returned to 20. In this example, strDbPath is the path to the Microsoft Jet database:

Dim dbs As Database, qdf As QueryDef
Dim rst As Recordset

Set dbs = OpenDatabase(strDbPath)
' Create a QueryDef object.
Set qdf = dbs.CreateQueryDef("")

' Set the properties of the new query to retrieve data from
' the Microsoft SQL Server Pubs database, limiting the number
' of returnable records to 20.
With qdf
	.Connect = "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Pubs"
	.SQL = "SELECT * FROM titles"
	.ReturnsRecords = True
	.MaxRecords = 20
End With

' Open a recordset against this query to work
' with the records returned.
Set rst = qdf.OpenRecordset()

See Also For more information about limiting returned data, see Chapter 4, “Queries.”