Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Top Query

 
   Database Forums (Home) -> MS Access RSS
Next:  Query question - limit amounts included in sum by..  
Author Message
Secret Squirrel

External


Since: Sep 26, 2005
Posts: 106



(Msg. 1) Posted: Fri Dec 26, 2008 5:52 pm
Post subject: Top Query
Archived from groups: microsoft>public>access (more info?)

Is there a way to set a parameter for the "Top n" in a query? I want my users
to be able to enter a number and have it return the "top n" vendors.

 >> Stay informed about: Top Query 
Back to top
Login to vote
aaron.kempf

External


Since: Apr 11, 2008
Posts: 135



(Msg. 2) Posted: Fri Dec 26, 2008 6:15 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Only with SQL Server can you have TOP N as a variable; and this only
came with SQL 2005






On Dec 26, 5:52 pm, Secret Squirrel
wrote:
> Is there a way to set a parameter for the "Top n" in a query? I want my users
> to be able to enter a number and have it return the "top n" vendors.

 >> Stay informed about: Top Query 
Back to top
Login to vote
John W. Vinson1

External


Since: Jan 30, 2004
Posts: 507



(Msg. 3) Posted: Fri Dec 26, 2008 7:51 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 26 Dec 2008 17:52:02 -0800, Secret Squirrel
wrote:

>Is there a way to set a parameter for the "Top n" in a query? I want my users
>to be able to enter a number and have it return the "top n" vendors.

Aaron's correct in this case - you can't parameterize the TOP value. The only
way to do so is to construct the SQL string in code.
--

John W. Vinson [MVP]
 >> Stay informed about: Top Query 
Back to top
Login to vote
Coderz

External


Since: Dec 27, 2008
Posts: 1



(Msg. 4) Posted: Sat Dec 27, 2008 1:25 am
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can however create a temporary table and call a TOP N query using code.
ex:

sSql = "SELECT TOP " & intTop & " id, title FROM table1;"

to make a make query try using this code:

SELECT TOP 5 Table1.id, Table1.title INTO table1temp
FROM Table1;

Hope this help

http://www.sourcecodester.com - Download free source code


"Secret Squirrel" wrote in
message
> Is there a way to set a parameter for the "Top n" in a query? I want my
> users
> to be able to enter a number and have it return the "top n" vendors.
>
 >> Stay informed about: Top Query 
Back to top
Login to vote
Secret Squirrel

External


Since: Sep 26, 2005
Posts: 106



(Msg. 5) Posted: Sat Dec 27, 2008 5:31 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I was able to figure it out thanks to your post. I created a SQL string and
then using that string created a query that I then used for my report. I
based the "Top N" on a textbox on my form so the users can enter a number
then fire the SQL string to create the query. Within my code I also delete
the query it creates so that the value that was last used doesn't remain.
Here's a sample of the code I'm using:

Private Function TopParameter()

Dim dbf As Database, qdf As QueryDef, strSQL As String, strSQL1 As String
Set dbs = CurrentDb

On Error Resume Next
DoCmd.DeleteObject acQuery, "ListTop"

strSQL = "SELECT TOP " & [Forms]![frmSelectVendors]![txtTopCount] & "
tblVendors.VendorID, tblVendors.VendorName,
qryPerformanceDetailExtCost.ExtCost"

Set dbf = DBEngine.Workspaces(0).Databases(0)
Set qdf = dbf.CreateQueryDef("ListTop", strSQL)


End Function

"John W. Vinson" wrote:

> On Fri, 26 Dec 2008 17:52:02 -0800, Secret Squirrel
> wrote:
>
> >Is there a way to set a parameter for the "Top n" in a query? I want my users
> >to be able to enter a number and have it return the "top n" vendors.
>
> Aaron's correct in this case - you can't parameterize the TOP value. The only
> way to do so is to construct the SQL string in code.
> --
>
> John W. Vinson [MVP]
>
 >> Stay informed about: Top Query 
Back to top
Login to vote
John W. Vinson1

External


Since: Jan 30, 2004
Posts: 507



(Msg. 6) Posted: Sat Dec 27, 2008 6:48 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sat, 27 Dec 2008 17:31:01 -0800, Secret Squirrel
wrote:

>I was able to figure it out thanks to your post. I created a SQL string and
>then using that string created a query that I then used for my report. I
>based the "Top N" on a textbox on my form so the users can enter a number
>then fire the SQL string to create the query. Within my code I also delete
>the query it creates so that the value that was last used doesn't remain.
>Here's a sample of the code I'm using:
>
>Private Function TopParameter()
>
>Dim dbf As Database, qdf As QueryDef, strSQL As String, strSQL1 As String
>Set dbs = CurrentDb
>
>On Error Resume Next
>DoCmd.DeleteObject acQuery, "ListTop"
>
>strSQL = "SELECT TOP " & [Forms]![frmSelectVendors]![txtTopCount] & "
>tblVendors.VendorID, tblVendors.VendorName,
>qryPerformanceDetailExtCost.ExtCost"
>
>Set dbf = DBEngine.Workspaces(0).Databases(0)
>Set qdf = dbf.CreateQueryDef("ListTop", strSQL)

That'll work. You don't actually need to create a Query object though - you
can set the Report's Recordsource property to the SQL string in the report's
Open event if you wish.
--

John W. Vinson [MVP]
 >> Stay informed about: Top Query 
Back to top
Login to vote
aaron.kempf

External


Since: Apr 11, 2008
Posts: 135



(Msg. 7) Posted: Mon Dec 29, 2008 12:14 am
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

or of course-- you could move to SQL Server that natively supports a
parameter like

create procedure spGetTopNRecords
(
@N Integer
)
As
Select top (@N) *
From MyTable


so again-- you can either decide to use the built in functionality--
or you can subject your users to sub-optimal execution plans.

-Aaron






On Dec 27, 5:48 pm, John W. Vinson
wrote:
> On Sat, 27 Dec 2008 17:31:01 -0800, Secret Squirrel
>
>
>
>
>
> wrote:
> >I was able to figure it out thanks to your post. I created a SQL string and
> >then using that string created a query that I then used for my report. I
> >based the "Top N" on a textbox on my form so the users can enter a number
> >then fire the SQL string to create the query. Within my code I also delete
> >the query it creates so that the value that was last used doesn't remain..
> >Here's a sample of the code I'm using:
>
> >Private Function TopParameter()
>
> >Dim dbf As Database, qdf As QueryDef, strSQL As String, strSQL1 As String
> >Set dbs = CurrentDb
>
> >On Error Resume Next
> >DoCmd.DeleteObject acQuery, "ListTop"
>
> >strSQL = "SELECT TOP " & [Forms]![frmSelectVendors]![txtTopCount] & "
> >tblVendors.VendorID, tblVendors.VendorName,
> >qryPerformanceDetailExtCost.ExtCost"
>
> >Set dbf = DBEngine.Workspaces(0).Databases(0)
> >Set qdf = dbf.CreateQueryDef("ListTop", strSQL)
>
> That'll work. You don't actually need to create a Query object though - you
> can set the Report's Recordsource property to the SQL string in the report's
> Open event if you wish.
> --
>
>              John W. Vinson [MVP]- Hide quoted text -
>
> - Show quoted text -
 >> Stay informed about: Top Query 
Back to top
Login to vote
Secret Squirrel

External


Since: Sep 26, 2005
Posts: 106



(Msg. 8) Posted: Mon Dec 29, 2008 5:46 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I assume I would need to leave my form open to pass the SQL string to the
reports recordsource when I open the report since the public function I
created is on the form. Correct? How would I call that string from the open
event this way?

"John W. Vinson" wrote:

> On Sat, 27 Dec 2008 17:31:01 -0800, Secret Squirrel
> wrote:
>
> >I was able to figure it out thanks to your post. I created a SQL string and
> >then using that string created a query that I then used for my report. I
> >based the "Top N" on a textbox on my form so the users can enter a number
> >then fire the SQL string to create the query. Within my code I also delete
> >the query it creates so that the value that was last used doesn't remain.
> >Here's a sample of the code I'm using:
> >
> >Private Function TopParameter()
> >
> >Dim dbf As Database, qdf As QueryDef, strSQL As String, strSQL1 As String
> >Set dbs = CurrentDb
> >
> >On Error Resume Next
> >DoCmd.DeleteObject acQuery, "ListTop"
> >
> >strSQL = "SELECT TOP " & [Forms]![frmSelectVendors]![txtTopCount] & "
> >tblVendors.VendorID, tblVendors.VendorName,
> >qryPerformanceDetailExtCost.ExtCost"
> >
> >Set dbf = DBEngine.Workspaces(0).Databases(0)
> >Set qdf = dbf.CreateQueryDef("ListTop", strSQL)
>
> That'll work. You don't actually need to create a Query object though - you
> can set the Report's Recordsource property to the SQL string in the report's
> Open event if you wish.
> --
>
> John W. Vinson [MVP]
>
 >> Stay informed about: Top Query 
Back to top
Login to vote
aaron.kempf

External


Since: Apr 11, 2008
Posts: 135



(Msg. 9) Posted: Mon Dec 29, 2008 9:21 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

create procedure spGetTopNRecords
(
@N Integer
)
As
Select top (@N) *
From MyTable





On Dec 29, 5:46 pm, Secret Squirrel
wrote:
> I assume I would need to leave my form open to pass the SQL string to the
> reports recordsource when I open the report since the public function I
> created is on the form. Correct? How would I call that string from the open
> event this way?
>
>
>
> "John W. Vinson" wrote:
> > On Sat, 27 Dec 2008 17:31:01 -0800, Secret Squirrel
> > wrote:
>
> > >I was able to figure it out thanks to your post. I created a SQL string and
> > >then using that string created a query that I then used for my report. I
> > >based the "Top N" on a textbox on my form so the users can enter a number
> > >then fire the SQL string to create the query. Within my code I also delete
> > >the query it creates so that the value that was last used doesn't remain.
> > >Here's a sample of the code I'm using:
>
> > >Private Function TopParameter()
>
> > >Dim dbf As Database, qdf As QueryDef, strSQL As String, strSQL1 As String
> > >Set dbs = CurrentDb
>
> > >On Error Resume Next
> > >DoCmd.DeleteObject acQuery, "ListTop"
>
> > >strSQL = "SELECT TOP " & [Forms]![frmSelectVendors]![txtTopCount] & "
> > >tblVendors.VendorID, tblVendors.VendorName,
> > >qryPerformanceDetailExtCost.ExtCost"
>
> > >Set dbf = DBEngine.Workspaces(0).Databases(0)
> > >Set qdf = dbf.CreateQueryDef("ListTop", strSQL)
>
> > That'll work. You don't actually need to create a Query object though - you
> > can set the Report's Recordsource property to the SQL string in the report's
> > Open event if you wish.
> > --
>
> >              John W. Vinson [MVP]- Hide quoted text -
>
> - Show quoted text -
 >> Stay informed about: Top Query 
Back to top
Login to vote
John W. Vinson1

External


Since: Jan 30, 2004
Posts: 507



(Msg. 10) Posted: Tue Dec 30, 2008 11:44 am
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 29 Dec 2008 17:46:01 -0800, Secret Squirrel
wrote:

>I assume I would need to leave my form open to pass the SQL string to the
>reports recordsource when I open the report since the public function I
>created is on the form. Correct? How would I call that string from the open
>event this way?

You can actually open the criteria form in the Report's Open event. Open it in
Dialog mode so that code execution stops; the user can fill in the criteria.
Then put a command button on the form to set the form's Visible property to No
- this resumes the code and the report executes.

Then close the (hidden) form in the Report's Close event.
--

John W. Vinson [MVP]
 >> Stay informed about: Top Query 
Back to top
Login to vote
Secret Squirrel

External


Since: Sep 26, 2005
Posts: 106



(Msg. 11) Posted: Tue Dec 30, 2008 1:11 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Makes sense. I understand what you're saying. But how do I pass the SQL
string to the recordsource of the report?

"John W. Vinson" wrote:

> On Mon, 29 Dec 2008 17:46:01 -0800, Secret Squirrel
> wrote:
>
> >I assume I would need to leave my form open to pass the SQL string to the
> >reports recordsource when I open the report since the public function I
> >created is on the form. Correct? How would I call that string from the open
> >event this way?
>
> You can actually open the criteria form in the Report's Open event. Open it in
> Dialog mode so that code execution stops; the user can fill in the criteria.
> Then put a command button on the form to set the form's Visible property to No
> - this resumes the code and the report executes.
>
> Then close the (hidden) form in the Report's Close event.
> --
>
> John W. Vinson [MVP]
>
 >> Stay informed about: Top Query 
Back to top
Login to vote
John W. Vinson1

External


Since: Jan 30, 2004
Posts: 507



(Msg. 12) Posted: Tue Dec 30, 2008 5:35 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Tue, 30 Dec 2008 13:11:45 -0800, Secret Squirrel
wrote:

>Makes sense. I understand what you're saying. But how do I pass the SQL
>string to the recordsource of the report?
>
>"John W. Vinson" wrote:
>
>> On Mon, 29 Dec 2008 17:46:01 -0800, Secret Squirrel
>> wrote:
>>
>> >I assume I would need to leave my form open to pass the SQL string to the
>> >reports recordsource when I open the report since the public function I
>> >created is on the form. Correct? How would I call that string from the open
>> >event this way?
>>
>> You can actually open the criteria form in the Report's Open event. Open it in
>> Dialog mode so that code execution stops; the user can fill in the criteria.
>> Then put a command button on the form to set the form's Visible property to No
>> - this resumes the code and the report executes.
>>
>> Then close the (hidden) form in the Report's Close event.
>> --
>>
>> John W. Vinson [MVP]
>>

There's no need to do so. The report's Recordsource is just a query
referencing the criteria form with parameters such as

=[Forms]![frmCrit]![controlname]

When frmCrit is opened, the user can enter criteria into controls on the form;
clicking the button will resume the opening of the report, and the query will
get its parameters from the form.
--

John W. Vinson [MVP]
 >> Stay informed about: Top Query 
Back to top
Login to vote
aaron.kempf

External


Since: Apr 11, 2008
Posts: 135



(Msg. 13) Posted: Tue Dec 30, 2008 8:07 pm
Post subject: Re: Top Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

John;

Do you know how to read?
This guy is using Sql-Stringing to build a query.

How is he supposed to do that with a forms parameter?

Only with SQL Server can you write expressions in fields-- like in
Reporting Services.
With Access, you need to write mountains of VBA that cripple most
databases.

It's time for a 21st century toolset-- SQL Server is here; and it is
the worlds most popular database.

Just because you're too stupid to learn the 'worlds most popular
database' that doesn't mean that you can purposefully confusing
people.
ULS and Replication are not available in Access 2007 (format).

Screw you and your baby-sized databases.






On Dec 30, 4:35 pm, John W. Vinson
wrote:
> On Tue, 30 Dec 2008 13:11:45 -0800, Secret Squirrel
>
>
>
>
>
> wrote:
> >Makes sense. I understand what you're saying. But how do I pass the SQL
> >string to the recordsource of the report?
>
> >"John W. Vinson" wrote:
>
> >> On Mon, 29 Dec 2008 17:46:01 -0800, Secret Squirrel
> >> wrote:
>
> >> >I assume I would need to leave my form open to pass the SQL string to the
> >> >reports recordsource when I open the report since the public function I
> >> >created is on the form. Correct? How would I call that string from the open
> >> >event this way?
>
> >> You can actually open the criteria form in the Report's Open event. Open it in
> >> Dialog mode so that code execution stops; the user can fill in the criteria.
> >> Then put a command button on the form to set the form's Visible property to No
> >> - this resumes the code and the report executes.
>
> >> Then close the (hidden) form in the Report's Close event.
> >> --
>
> >>              John W. Vinson [MVP]
>
> There's no need to do so. The report's Recordsource is just a query
> referencing the criteria form with parameters such as
>
> =[Forms]![frmCrit]![controlname]
>
> When frmCrit is opened, the user can enter criteria into controls on the form;
> clicking the button will resume the opening of the report, and the query will
> get its parameters from the form.
> --
>
>              John W. Vinson [MVP]- Hide quoted text -
>
> - Show quoted text -
 >> Stay informed about: Top Query 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Help with SQL query - Hello I need some help with a SQL query (ASP page accessing Access database) I have a table called 'BookedRooms' which contains the following fields :- Room (text) DateBooked (date) Lesson (numeric from 1 to 9) Staff (text) I want to create a..

Query - I have the following fields in my database: SKU Number, Date, Shift/Team, Finished Units, Defects I need to create a query that sums the # of finished units and defects by SKU Number, Date, and Shift/Team. The problem I have run into is that the ..

query - Hi i have a table containing the following fields man_date type test wheremeas measured value 1/1/08 12:00:00 M1 Hardness Top 2.4 1/1/08 12:00:00 ...

Query - On a query, I used the following - One Call: IIf([SumOfCount]=1,1,0) which places a 1 in the One Call column if SumOfCount equals 1 How would I write add a 1 in the Multiple Calls column if the SumOfCount calls are 2,3,4 or 5

making a query - ok this is my problem - i work at a doctors office and we have a database of about 1000 patients in the practice. we have fields called "cardiac diagnosis 1" , "cardiac diagnosis 2", and "cardiac diagnosis 3". we want to...
   Database Forums (Home) -> MS Access All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]