ID: Q168241
The information in this article applies to:
This article describes how to use the TOP predicate to return the top N number of records that meet the conditions of a query.
With the release of Microsoft Access version 2.0, the TOP predicate was introduced in Microsoft Access SQL syntax. Visual Basic version 3.0 itself will not accept the TOP predicate directly, but it is possible to use the TOP predicate with Visual Basic version 3.0 if you are using the Microsoft Jet 2.x engine that ships with Microsoft Access version 2.0.
The TOP predicate is now accepted syntax in Visual Basic version 4.0 and higher.
You 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:
   SELECT TOP 10 Titles.* FROM Titles;
   SELECT TOP 10 PERCENT Titles.* FROM Titles;
To 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:
   Dim mydb As Database, MyQuery As QueryDef
   Dim Sql$
   Sql$ = "SELECT TOP 10 Titles.* FROM Titles;"
   Set mydb = OpenDatabase("C:\VB\BIBLIO.MDB")
   Set MyQuery = mydb.CreateQueryDef("TopQuery", Sql$)
   "Invalid SQL Statement: expected 'DELETE', 'INSERT', 'PROCEDURE',
      'SELECT', or 'UPDATE'"
The following code works with Visual Basic version 3.0 and a Microsoft Access version 1.x database:
   Dim Sql$
   Dim MyDynaset as Dynaset
   Dim MySnapshot as Snapshot
   Sql$ = "SELECT TOP 10 Titles.* FROM Titles;"
   data1.recordSource = Sql$
   ' -or-
   MyDynaset = MyDb.CreateDynaset( Sql$ )
   ' -or-
   MySnapshot = MyDb.CreateSnapshot( Sql$ )
You can use the TOP predicate with Visual Basic version 3.0 and databases in Microsoft Access version 2.x format. This includes use within:
   Dim mydb As Database, MyQuery As QueryDef
   Dim Sql$
   Sql$ = "SELECT TOP 10 Titles.* FROM Titles;"
   Set mydb = OpenDatabase("C:\VB\BIBLIO.MDB")
   Set MyQuery = mydb.CreateQueryDef("TopQuery", Sql$)
Visual 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:
   ALL predicate
The 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+:
1. Start a new project in Visual Basic version 4.0+. Form1 is created by
   default.
   using the Tools menu in Visual Basic 4.0 or the Project menu in
   Visual Basic 5.0 and higher.
4. Add the following code to the click event for Command1:
      Private Sub Command1_Click()
         Dim MyDB As Database, MyQuery As QueryDef, MySet As Recordset
         Dim Sql$, QueryDefName$
         QueryDefName$ = "TopTitles"
         Sql$ = "SELECT TOP 10 Titles.* FROM Titles;"
         ' Open Database, and create querydef:
         Set MyDB = Workspaces(0).OpenDatabase("C:\VB4\BIBLIO.MDB")
         Set MyQuery = MyDB.CreateQueryDef(QueryDefName$, Sql$)
         ' Create Recordset from query and load list with results:
         Set MySet = MyDB.OpenRecordset(QueryDefName$)
         MySet.MoveFirst
         While MySet.EOF = False
            debug.print MySet(0)
            MySet.MoveNext
         Wend
         ' Clean up -- close objects and remove querydef:
         MyDB.QueryDefs.Delete QueryDefName$
         MyQuery.Close
         MySet.Close
         MyDB.Close
         Set MyQuery = Nothing
         Set MySet = Nothing
         Set MyDB = Nothing
      End Sub
5. Change the line defining SQL$ to be:
      Sql$ = "SELECT TOP 10 PERCENT Titles.* FROM Titles;"
Additional query words: kbVBp400 kbVBp500 kbVBp300 kbVPp kbWinOS98 kbVBp600 kbNoKeyWord
Platform          : WINDOWS
Issue type        : kbhowtoLast Reviewed: August 7, 1998