Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 113956 - Last Review: March 2, 2005 - Revision: 1.3
How To VB3 Using TOP N Query from Microsoft Access 2.0 in VB
This article was previously published under Q113956
One of the new types of queries added to Microsoft Access version 2.0 is
called a TOP N query. If you have the Microsoft Jet 2.0/Visual Basic 3.0
Compatibility Layer, Microsoft Access version 2.0, and Visual Basic version
3.0, you can use this type of query from Visual Basic version 3.0 programs.
This article gives a brief example that uses both the Microsoft Access TOP
N and TOP N PERCENT queries from Visual Basic.
For more information on these particular queries, please review the
Microsoft Access documentation.
Step-by-Step Example
- Start a new project in Visual Basic. Form1 is created by default.
- Add three Labels, three List Box controls, and three Command buttons
to Form1.
- Using the following table as a guide, set the properties of the
controls you added in step 2:
Control Name Property New Value
--------------------------------------------------------------------
Label1 Caption Selecting the first 10 titles from the
Titles table according to title field.
Label2 Caption Selecting the first 5 titles from the
Titles table according to date published.
Label3 Caption Selecting the first 7 years of percent
published from the Titles table according
to the year published field.
Command1 Caption First 10 Title names
Command2 Caption First 5 titles dates published
Command3 Caption First 7 years percent published
- Place the following code in the Command1 Click event procedure:
Sub Command1_Click ()
Dim ds As Dynaset
Dim db As database
Set db = OpenDatabase("BIBLIO.MDB")
' Enter the following two lines as one, single line:
Set ds =
db.CreateDynaset("select top 5 title from titles order by title")
Do Until ds.EOF
list1.AddItem "" & ds("title")
ds.MoveNext
Loop
ds.Close
db.Close
End Sub
- Place the following code in the Command2 Click event procedure:
Sub Command2_Click ()
Dim ds As Dynaset
Dim db As database
Set db = OpenDatabase("BIBLIO.MDB")
' Enter the following two lines as one, single line:
Set ds = db.CreateDynaset("select top 5 [year published],
title from titles order by [year published]")
Do Until ds.EOF
list2.AddItem "" & ds("title")
ds.MoveNext
Loop
ds.Close
db.Close
End Sub
- Place the following code in the Command3 Click event procedure:
Sub Command3_Click ()
Dim ds As Dynaset
Dim db As database
Set db = OpenDatabase("BIBLIO.MDB")
' Enter the following two lines as one, single line:
Set ds = db.CreateDynaset("select top 7 Percent [year published],
title from titles order by [year published]")
Do Until ds.EOF
list3.AddItem "" & ds("title")
ds.MoveNext
Loop
ds.Close
db.Close
End Sub
- From the Run menu, choose Start (ALT, R, S), or press the F5 key
to run the program. Click each of the buttons (Command1, Command2, and
Command3) in succession.
APPLIES TO
- Microsoft Visual Basic 3.0 Professional Edition
Retired KB Content DisclaimerThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate