Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top n records

Status
Not open for further replies.

Ulsterman2

Programmer
May 20, 2005
11
GB
I have the following query which selects the top 25 records based on the
"Random" field.

SELECT TOP 25 [qry Invoices].ID, [qry Invoices].Period, [qry
Invoices].Gross, [qry Invoices].Random
FROM [qry Invoices]
ORDER BY [qry Invoices].Random;

The problem is that I do not always want the top 25. Ideally I would like
the query to ask me how many records I want to show. Is this possible?
 
yes....you would have to create a dynamic SQL statement that is built when you press a button on the form where you enter the number of records you want to return.

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
Thanks. Next question...

What would the dynamic SQL be?
 
something like this in the OnClick event of a button:

Code:
Dim strSQL As String
Dim db As Database
Dim rs As Recordset

Set db = CurrentDb
strSQL = "SELECT TOP " & Me.txtBoxName & " FROM TableName;"
              
Set rs = db.OpenRecordset(strSQL)

the syntax may be off some, I don't do Access programming.

HTH

leslie

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top