A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

TOP in query user selects amount to return



 
 
Thread Tools Display Modes
  #1  
Old June 17th, 2008, 09:02 AM posted to microsoft.public.access.queries
Kevin
external usenet poster
 
Posts: 9
Default TOP in query user selects amount to return

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?
  #2  
Old June 17th, 2008, 10:19 AM posted to microsoft.public.access.queries
Dennis
external usenet poster
 
Posts: 1,222
Default TOP in query user selects amount to return

Just put the number you want after Top
SELECT TOP 10 .....

"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?

  #3  
Old June 17th, 2008, 10:23 AM posted to microsoft.public.access.queries
Dennis
external usenet poster
 
Posts: 1,222
Default TOP in query user selects amount to return

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?

  #4  
Old June 17th, 2008, 10:51 AM posted to microsoft.public.access.queries
Kevin
external usenet poster
 
Posts: 9
Default TOP in query user selects amount to return

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?

  #5  
Old June 17th, 2008, 11:06 AM posted to microsoft.public.access.queries
Dennis
external usenet poster
 
Posts: 1,222
Default TOP in query user selects amount to return

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?

  #6  
Old June 17th, 2008, 11:14 AM posted to microsoft.public.access.queries
Kevin
external usenet poster
 
Posts: 9
Default TOP in query user selects amount to return

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?

  #7  
Old June 17th, 2008, 12:54 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default TOP in query user selects amount to return

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?

  #8  
Old June 17th, 2008, 02:50 PM posted to microsoft.public.access.queries
Michel Walsh
external usenet poster
 
Posts: 2,404
Default 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" wrote in message
...
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?



  #9  
Old June 17th, 2008, 04:42 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default TOP in query user selects amount to return

Whoops, I just noted that I forgot to credit the original author of that
solution. My apologies to Michel Walsh from whom I copied this solution.

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

Michel Walsh wrote:
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" wrote in message
...
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?



  #10  
Old June 17th, 2008, 07:45 PM posted to microsoft.public.access.queries
Michel Walsh
external usenet poster
 
Posts: 2,404
Default TOP in query user selects amount to return

Just as Ken Getz once said: "If I have thought about it, all by
myself, it is quite possible that someone ELSE have thought about it too,
all by himself..."

It is not as much as the idea than the way you expressed it that
'matters', in the end. And definitively, *you* typed your post! :-)


Vanderghast, Access MVP


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 05:03 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.