Re: TOP in query user selects amount to return



Indeed. In addition, if someone is using MS SQL Server 2005 instead of Jet
4.0, you can use a parameter but take note that ( ) are mandatory in that
case:


SELECT TOP (@N) ...



There is no equivalent 'immediate' solution with Jet.




"John Spencer" <spencer@xxxxxxxxx> wrote in message
news:%234NhZDH0IHA.5520@xxxxxxxxxxxxxxxxxxxxxxx
Select Top N records where N is variable ( a parameter)

One method that can be used is to use a ranking query to assign a number
to the records and then use the ranking to return N records.

'Probably not updatable
SELECT MyTable.*
FROM MyTable INNER JOIN
(
SELECT A.MyField, Count(B.MyField) as TheCount
FROM MyTable as A LEFT JOIN MyTable As B
ON A.MyField < B.MyField
GROUP BY A.MyField
) as Ranking
ON MyTable.MyField = Ranking.MyField
WHERE Ranking.TheCount < [Top How Many Number]

'Probably updatable
SELECT MyTable.*
FROM MyTable
WHERE MyField in
(SELECT A.MyField
FROM MyTable as A LEFT JOIN MyTable As B
ON A.MyField < B.MyField
GROUP BY A.MyField
HAVING Count(B.MyField) < [Top How Many Number])

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County

Kevin wrote:
Thanks Dennis, I tried that also but, like you, just get errors!

"Dennis" wrote:

I don't think so. I tried putting this into the SQL view of a query but
it errors out
SELECT TOP [Enter How Many] etc. etc.

"Kevin" wrote:

Thanks Dennis,

I am sure this will work, and can use it if I need to but, do you know
if there is a way to do this from within the query rather than through
a form?

"Dennis" wrote:

Forget my last post, I have just realised you want the user to supply
how many records to return. You could try building the SQL string and
incorporating a textbox from a form as the amount to return
e.g.
strSQL = "SELECT TOP " & Forms![FormName]![TextBoxName] & " etc etc

"Kevin" wrote:

Using Top, is there a way to ask the user how many records to return?
if not is there another simple way of acheiving this?


.



Relevant Pages

  • Re: Calculate days between 2 dates ignoring weekends?
    ... I sometimes compare Jet to SQL Server as an aside ... JamieC on Jet: "If this were Jet... ... 'classes' using the MOD operator" [Note SQL Server has no MOD ...
    (microsoft.public.access.formscoding)
  • Re: Creating a new Access database file (.mdb) in .NET/C#
    ... You definitely need to download my Ebook. ... It can be passed around as a file like JET and backed up in a similar fashion. ... Consider that this database is also fully encryptable--a feature not in JET or in SQL Server until 2008. ... Per the MS product pages, I'm eligible for the Upgrade package, and ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Official Status of SQLServer 2005 ADP
    ... As far back as Access 97, Jet has made every attempt ... to "pass through" every Access query against a linked ODBC datasource. ... ends - DB2, Oracle, and SQL Server among others. ... > solution might be to use ADP. ...
    (microsoft.public.access.adp.sqlserver)
  • Re: Calculate days between 2 dates ignoring weekends?
    ... I sometimes compare Jet to SQL Server as an aside ... JamieC on Jet: "If this were Jet... ... 'classes' using the MOD operator" [Note SQL Server has no MOD ...
    (microsoft.public.access.formscoding)
  • Re: Why not Access...?
    ... The first point is, yes, Jet, not Access, is a file based, Pseudo-RDBMS. ... Jet database will corrupt and become ... MS Jet/Access, MSDE, and SQL Server 2005 Express Edition. ... > I am developing a desktop application using VB as front-end for some clients> where the a large number of regular entries are done but there is a rare need> to delete old customers. ...
    (microsoft.public.sqlserver.server)