top 30 records programaticaly

cpampas

Registered User.
Local time
Today, 12:27
Joined
Jul 23, 2012
Messages
221
Hi,
I am not sure If I am looking at this the right way. I want to get the highest number of the field "closecot" of qryCotacoes, considering only the n last entered records, in this case n would be a parameter.

I understand that in a select query I would set field parameters with :

Code:
n=30

Set MyQryDef = db.QueryDefs("qryCotacoes")                                             
MyQryDef.Parameters("Parameter1").Value = n

but how can I set programaticaly the top value parameter , wich is this case is 30 to a variable ?

Code:
SELECT TOP 30 *
FROM tblCotacoes
ORDER BY tblCotacoes.cotacaoid DESC;

I would appreciate your help
 
Hi. You would modify the SQL property. For example:
Code:
n=30
strSQL = "SELECT TOP " & n & " FieldList FROM TableName"
MyQryDef.SQL= strSQL
Hope that helps...
 
Hi,
That realy helped, I am closer now. I do get the last n records(30) with the following :

Code:
strSQL = "SELECT TOP " & n & " closecot FROM tblCotacoes"
MyQryDef.SQL = strSQL

but of those records I would like to get the highest one, so I did this instead :

Code:
strSQL = "SELECT TOP " & n & " Max(closecot) As H FROM tblCotacoes"
MyQryDef.SQL = strSQL

hoping that I would get the highest of the 30 records, but I get the highest of all records in that table.
Any thoughts ?
 
Hi,
That realy helped, I am closer now. I do get the last n records(30) with the following :

Code:
strSQL = "SELECT TOP " & n & " closecot FROM tblCotacoes"
MyQryDef.SQL = strSQL

but of those records I would like to get the highest one, so I did this instead :

Code:
strSQL = "SELECT TOP " & n & " Max(closecot) As H FROM tblCotacoes"
MyQryDef.SQL = strSQL

hoping that I would get the highest of the 30 records, but I get the highest of all records in that table.
Any thoughts ?
Hi. Do a search on "Top N Per Group" subquery.
 
try

strSQL = "SELECT TOP " & n & " closecot FROM tblCotacoes ORDER BY closecot Desc"

note if two records have the same closecot value then you will get 31 records back because the records returned are based on the top values, not a recordcount. Rationale is quite simple, if you ask for the top 1 and there are 3 records with that value, which should it choose since the records are otherwise random after the initial sort? The usual way round it is to order by a secondary field, typically a primary key or timestamp
 
Remember that using top and specifying a sort order go hand in hand.

It never makes sense to use TOP without specifying a SORT, unless you want to get back something random.

Edit: As CJ has mentioned +1
 
Many thanks, it´s working just fine

Code:
Set MyQryDef = db.CreateQueryDef("")

strSQL = "SELECT TOP " & n & " closecot FROM tblCotacoes ORDER BY closecot Desc"
MyQryDef.SQL = strSQL

Set rs = MyQryDef.OpenRecordset(dbOpenDynaset)
rs.MoveFirst: HighNumber = rs!closecot
rs.MoveLast: LowNumber= rs!closecot
 
Glad to hear things are working. A mention of one possible approach to saving 'template' queries, with placeholders, placeholders to be Replace()'ed at runtime:
 

Users who are viewing this thread

Back
Top Bottom