The information in this article applies to:
SUMMARY
This article describes how to use the TOP predicate to return the top N
number of records that meet the conditions of a query. MORE INFORMATIONYou can use the TOP predicate to return the first N records that match the conditions of a query, where N is a number less than or equal to the total records returned by the query. For example, the following query returns the first 10 records in the Titles table:
You can use TOP with the PERCENT keyword to return the first N% records
matching the conditions of a query, where N% is between 0% and 100%. For
example, the following query returns the first 10% of all the records in
the Titles table:
The text below describes how to use the TOP predicate with the different
versions of Visual Basic and Microsoft Access database formats.
Using TOP Predicate in Visual Basic 3.0 with Microsoft Access 1.x DatabaseTo use the TOP predicate with Visual Basic version 3.0 in Titles, you will need Microsoft Access version 2.0 and the Microsoft Jet 2.0/Visual Basic 3.0 Compatibility Layer in addition to Visual Basic, even if the target database is in Microsoft Access version 1.x format.Visual Basic version 3.0 was released with the Microsoft Access version 1.x database, so TOP is not supported directly. This is demonstrated by trying to create a querydef using the TOP predicate as shown here:
The last line of this code sample will generate the following error
message:
This error message occurs because Visual Basic itself is trying to parse the SQL syntax given in the CreateQueryDef object in the context of the version of the database being used. This parsing or validation of the SQL Syntax, however, is not performed when setting a Data Control RecordSource property, a Dynaset object, or a Snapshot object for a 1.x database. The following code works with Visual Basic version 3.0 and a Microsoft Access version 1.x database:
Using TOP Predicate in Visual Basic 3.0 with Microsoft Access 2.X DatabaseYou can use the TOP predicate with Visual Basic version 3.0 and databases in Microsoft Access version 2.x format. This includes use within:
Using TOP Predicate in Visual Basic 4.0+ with Microsoft Access 2.x or 3.x
DatabaseVisual Basic version 4.0+ recognizes the TOP predicate as valid SQL Syntax. Although the TOP predicate itself is not listed as a index entry in the Visual Basic version 4.0+ Help menu, you can use the Find feature found under Windows 95, Windows 98, and Windows NT version 3.51 to create a search index and query on TOP, or search for this Help topic:
This Help topic discusses the proper syntax for the ALL, DISTINCT,
DISTINCTROW, and TOP predicates.
Step-by-Step ExampleThe following example shows how to use the TOP predicate with a Querydef using the new syntax for Data Microsoft Access Objects within Visual Basic version 4.0+:
Additional query words: kbVBp400 kbVBp500 kbVBp300 kbVPp kbWinOS98 kbVBp600 kbNoKeyWord
Keywords : |
Last Reviewed: September 7, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |