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

Home Posts Topics Members FAQ

Max Number of Records Returned by a Query Based on a Form-Entered Value

7 New Member
Help Again!! I’m using Access 2003 and I have a form that allows users to enter criteria for selecting tables and queries as well as the query parameters that will be used to generate a report. The report includes some minor but essential statistical information in its summary. One of the parameters I need to allow the user to provide is the maximum number of records to include in the report’s analysis (e.g., should the report include 30, 45, 52, etc. of the most recent records?). The Top Value property in the query would be perfect if I could get it to read a variable name instead of a fixed integer value. I’ve tried the MaxRecord property as well as assigning row numbers in the query and neither approach has worked (my sort field may have duplicates, my unique identifier doesn’t provide a record count in the right order). I know there should be a simple solution to this but I can’t find it. Is there a way to limit the number of records returned by a query based on a value entered into a form? Thanks!
Oct 20 '10 #1
1 3269
ADezii
8,834 Recognized Expert Expert
  1. Create a Query that includes ALL Records from your Data Source(s).
  2. Modify the SQL Property of the Query, reading the TOP Number of Values from a Text Box, in this case txtTOP.
  3. Control which Records appear by the ORDER BY Clause.
  4. Open the Query, remembering that Ties will also be included in the Result Set if relevant.
  5. In the following Demo, based on the [Order Details] Table of the Northwind Sample Database, I will display the TOP N Records, when N is a Value in the Text Box txtTOP on the Current Form.
  6. The Records will reflect the TOP N Records based on [UnitPrice] DESC as indicated in the ORDER BY Clause in Line #10.
    Expand|Select|Wrap|Line Numbers
    1. Dim qdf As QueryDef
    2. Dim strSQL As String
    3.  
    4. 'Make sure there is a Value in the Text Box txtTOP, it is Numeric, and it is < 1000
    5. If IsNull(Me![txtTOP]) Or Not IsNumeric(Me![txtTOP]) Or Me![txtTOP] >= 1000 Then Exit Sub
    6.  
    7. 'Customize the SQL for your Query reading the TOP Values from txtTOP
    8. strSQL = "SELECT TOP " & Me![txtTOP] & " [Order Details].OrderID, [Order Details].ProductID, " & _
    9.          "[Order Details].UnitPrice, [Order Details].Quantity, [Order Details].Discount " & _
    10.          "FROM [Order Details] ORDER BY [UnitPrice] DESC;"
    11.  
    12. Set qdf = CurrentDb.QueryDefs("qryOrderDetails")
    13.  
    14. qdf.SQL = strSQL
    15.  
    16. DoCmd.OpenQuery "qryOrderDetails", acViewNormal, acReadOnly
    17. DoCmd.Maximize
Oct 20 '10 #2

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

Similar topics

1
1785
by: mo | last post by:
I'm using the query desinger in ASP.NET , however the number of records in the resultset are not displaying, so I cut and paste it into Query analyzer which is silly. How do I set this in the...
3
14113
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
1
2102
by: Tom Keane | last post by:
I pretty much want to know how to get the number of records returned from a query, so I can divide that number into another number and place it as another field in the query result. Is this...
13
6552
by: MLH | last post by:
Suppose I have this simple SQL string... SELECT tblDrivers.DriverID, tblDrivers.DName FROM tblDrivers WHERE (((tblDrivers.DName) Like "N*")) ORDER BY tblDrivers.DriverID; And suppose that...
4
2676
by: MLH | last post by:
I have a combo box control on a form and a text box beside it in which I would like to display the number of listings in the combo-box. The combo box lists more or fewer rows depending on the...
12
2342
by: Tom | last post by:
Hello, I have a database of employee data in access and I am trying to create a form with combo boxes for criteria (ex. gender, office, position, etc.) that let the user select criteria from...
7
2249
by: J-P-W | last post by:
Hi, in the past I've spent ages writing VB routines to get around this problem, is there a simple way? I have three tables for a membership system: tblMembership - MembershipNumber; Names etc...
3
2075
by: questionit | last post by:
HI Is there a way to count number of Records on a continous form. Or Is there a way of finding number of records returned when we set recordsource of a form like this: Me.Form.RecordSource ...
5
10249
by: Ferasse | last post by:
Hi, I'm an occasional Ms-Access developer, so there is still a lot of stuff that I don't get... Right now, I'm working on a database that stores contractual information. One of the form that...
1
1185
by: samtymom | last post by:
I have a database that is used for Salvage Vehicles. (At present I am using 2002, but will be moving to 2007 shortly.) Presently there is a Switchboard that opens the Form View of the Salvage...
0
7259
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,...
0
7380
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,...
1
7098
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...
1
5085
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...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
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...

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.