473,624 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select top "n" values from a query with a parameter

20 New Member
Hi

I wish to have a user enter a parameter into a form for the number of rows to use to do an average. This form is based upon a query.

SELECT TOP does not support parameters. Something like the top values box in the query page would be ideal.

Is there a way to select only the top "n" values from a query?

Thank You
Ivan
Sep 28 '06 #1
3 16398
PEB
1,418 Recognized Expert Top Contributor
Hi Ivan,

Not sure about:
"SELECT TOP does not support parameters."

Maybe the pb is elsewhere!

:)

Hi

I wish to have a user enter a parameter into a form for the number of rows to use to do an average. This form is based upon a query.

SELECT TOP does not support parameters. Something like the top values box in the query page would be ideal.

Is there a way to select only the top "n" values from a query?

Thank You
Ivan
Sep 30 '06 #2
DirkB
1 New Member
You could built a select statement, or copy it from SQL view. The Select Top N will not support an imbedded parameter but you can do an inputbox to get the N and concat (&) into the sql query text. Then do DoCmd.RunSQL txtSQL where txtSQL contains the action query. Only works for an action query, make-table, update, delete, etc. A partial example follows

Dim txtPrompt as String, txtSQL as String, tmpIB as String
txtPrompt = "How many rows?"
tmpIB = InputBox(txtPro mpt, "Top X Rows", "5")

' force to 5 if less than 1
If Val(tmpIB) < 1 Then tmpIB = "5"

txtSQL = "SELECT TOP " & tmpIB
txtSQL = txtSQL & " rest of sql string"

DoCmd.RunSQL txtSQL
Nov 27 '07 #3
Guy Moseley
5 New Member
Hi
Have a look at the following:

http://www.fontstuff.c om/access/acctut17.htm

Alternatively this is the short version
First create a query, Access doesn't let you create an "empty" query so you'll have to make something up (anything will do).
Now create a dialogue Form to collect the variable in this case the value of "n".
You can add to this form command button which will give you an event to run the VBA from.
Now use VBA to modify the query you saved earlier.


Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim strSQL, strVar As String
  3.  
  4. strVar = ‘Dialogue Form Control’
  5. strSQL = "SELECT TOP " & strVar & " rest of SQL "
  6.  
  7. CurrentDb.QueryDefs("QueryName").SQL = strSQL
  8.  
  9.  

And that should do you. If you want to you can now open another form / report based on this query.
Nov 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1807
by: erich93063 | last post by:
I have a stored procedure which is performing a search against a "task" table. I would like to pass in a variable called @strAssignedTo which will be a comma delimeted list of employee ID's ie: "5,6,10". So my SQL without this variable would be something like: WHERE intAssignedTo IN (5,6,10) but when I try to do: WHERE intAssignedTo IN (@strAssignedTo)
2
2713
by: Ellen Manning | last post by:
Using A2K. In my table I have the field "Grant" which can have a value or be null. I have a query that counts the number of records and has a Where clause on the Grant field. The query won't work when Grant is null. What's the proper syntax for getting it to work when Grant is null? Here's my Where criteria: !!
3
7544
by: MX1 | last post by:
I'm ready to pull the hair out of my head. I have a query with a couple of parameters that I want to get from combo boxes on a form. One parameter is a date with a dynamically calculated year and the other is criteria for a Yes or No field. I'll focus on the Yes/No field for simplicity. If I run the query with the criteria hard coded as either "YES" or "NO", it works. In the values of the combo box in my form, I have it set as...
3
1011
by: cassandra.flowers | last post by:
Hi, I was wondering if it is possible (Using access) to have a query parameter as a drop down box rather than a text box? e.g. typing as criteria for a query produces a box with a text box for you to enter the parameter. Can any body enlighten me on how to turn this text box into a drop down box? And whether it is possible?
10
2892
by: Lau Lei Cheong | last post by:
Hello, I really need to use volatile System.Int64 for a .NET v1.1 program in C#. But the compiler complains "a volatile field can not be of type long". How to work around it? Or is there any other way to get similar effect for Int64 type? Another question less urging question is, why long variables can't be used as volatile? I understand that in 32-bit arch. 64-bit operations are not atomic, but seems that there could be locks or so...
3
2272
by: eskelies | last post by:
Good day all...I have a combo box that selects numbers (ie. 1,2,3, etc.). What I am looking for is the combo box to have an "ALL" selection. Right now this is what I have for my SQL code: SELECT .Funds, .Fund FROM UNION Select "*", "(All)" From ; This is what my VB code is showing; however the error is in the: rs.FindFirst "=" & Str(Nz(Me!, 0) ___________________________________________________________ Private Sub Combo1_AfterUpdate()...
0
1498
by: HomerS007 | last post by:
Hi, I'm using asp.net 2.0 and sql server 2000 for my first ever project. On one of the page in the application, I want to limit what the user can see based on his/her login. It's a page that has sensitive personal info like social security number and I only want user to see their own information and no one else. Can anybody please tell me how to pass in the username as a query parameter? I used a DetailsView control to and configure...
1
7999
by: bswanstrom | last post by:
Hi there, trying to execute a db2 sql statement from a command line from UNIX. Here is the command I'm running db2 export to /work/ftp/lt/sku_insert.csv of del select digits'('UPC_NO')', UPC, ITEM_DESC_SHORT, VENDOR_NO, SHOES, VENDOR_PID, VENDOR_PID_DESC, WOMENS, DEPT_NO, SPACE,SPACE1, VENDOR_COLOR_DESC, SIZE_DESC_SHOES, SPACE2, CLASS_NO, SPACE3, SPACE4, NRF_COLOR_DESC, VENDOR_SIZE_DESC, SPACE5, SPACE6, SPACE7, SPACE8, SPACE9, SPACE10,...
11
12533
by: Alexander Vasilevsky | last post by:
Linq "into" and "let" equally??? http://www.alvas.net - Audio tools for C# and VB.Net developers
24
4273
sueb
by: sueb | last post by:
I want to create a query for my Surgery database that asks for a Start Date and an End Date (easy), but also asks for a Surgeon using a drop-down list. This list already exists (the main database already uses this list to accept surgeon's names in its records), but I don't know how to let the user choose from that list in the parameters to the query. Here's the query's SQL so far: SELECT DataTable., DataTable.Surgeon, DataTable.Operation...
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8330
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.