S
Secret Squirrel
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.
to be able to enter a number and have it return the "top n" vendors.
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.
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)
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 = CurrentDbOn 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 -
John W. Vinson said: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.
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 said:On Sat, 27 Dec 2008 17:31:01 -0800, Secret Squirrel
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 -
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?
Makes sense. I understand what you're saying. But how do I pass the SQL
string to the recordsource of the report?
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:
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 -
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.