MaxRecords Property

Applies To

QueryDef object.

Description

Sets or returns the maximum number of records to return from a query.

Settings And Return Values

The setting or return value is a Long that represents the number of records to be returned. The default value is 0, indicating no limit on the number of records returned.

Remarks

Once the number of rows specified by MaxRecords is returned to your application in a Recordset, the query processor will stop returning additional records even if more records would qualify for inclusion in the Recordset. This property is useful in situations where limited client resources prohibit management of large numbers of records.

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