How can I create a query / report that would allow me to display n number of records – where it would ask for the number I want to see? Sure hope someone can help. This is the first time I have tried a forum. This looks very good and informative to me. Thanks in advance.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Display x number of records
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Display x number of records
- This topic has 35 replies, 6 voices, and was last updated 22 years, 7 months ago.
Viewing 0 reply threadsAuthorReplies-
WSMarkLiquorman
AskWoody Lounger -
WSrobinr
AskWoody Lounger -
WSpatt
AskWoody Lounger -
WSrobinr
AskWoody LoungerSeptember 20, 2002 at 9:49 pm #618708I didn’t create a new query – I have a query set up with 3 tables. I have an ID field for names and I want to be able to select x number of names at random. Hope I am explaining this ok.. So I typed into the SQL screen in the query – where it starts
SELECT TOP [HOW MANY?], ATA.Surname, ATA.Title, ATA.Prename, ATA.EmailAddress, ATA.HomeEmailAddress, ATA_Committee.Lcomm, Committee.TabComm, ATA.ID AS [Count]
FROM (ATA INNER JOIN ATA_Committee ON ATA.ID = ATA_Committee.ID) INNER JOIN Committee ON ATA_Committee.Lcomm = Committee.Code;Then I tried typing in the same thing in the criteria row.
Thanks -
WSpatt
AskWoody Lounger -
WSrobinr
AskWoody Lounger -
WSpatt
AskWoody Lounger -
WSrobinr
AskWoody Lounger -
WSpatt
AskWoody LoungerSeptember 20, 2002 at 11:27 pm #618726Start by:
1. Creating a query (named qry TopN) using what you already have and put the Top number as anything you like because we are going to be creating this query eveytime anyway.
2. Create the report based upon this query.
3.. Create the form with the unbound text box (called textTopN) and a button that prints the report (make it a preview while you are testing so you don’t waste too much paper).
In the OnClick event of the button we need to put the following code just prior to the DoCmd.OpenReport command.
Dim db as database, qdf as QueryDef, strSQL as string
Set db = currentDB
strSQL = “SELECT TOP ” & Me!textTopN & “field1, etc FROM tablename ”
strSQL = strSQL & ” WHERE ………”
db.QueryDefs.Delete “qry TopN”
Set qdf = db.createquerydef(“qry TopN”, strSQL)DoCmd.OpenReport …….
Set db = nothing
Set qdf = nothingHope this helps, if you have any problems just holler.
Pat -
WSrobinr
AskWoody LoungerSeptember 21, 2002 at 12:27 am #618746Hi Again,
This is what I typed in at the On Click code. It won’t run at all.Private Sub Command2_Click()
On Error GoTo Err_Command2_ClickDim db As database, qdf As QueryDef, strSQL As String
Set db = CurrentDb
strSQL = “SELECT TOP ” & Me!textTopN & “ATA.Surname, ATA.Title, ATA.Prename, ATA.EmailAddress, ATA.HomeEmailAddress, ATA_Committee.Lcomm, Committee.TabComm,ATA.EmailAddress, ATA.HomeEmailAddress FROM ATA”
strSQL = strSQL & “Where…..”
db.QueryDefs.Delete “qry Query2”
Set qdf = db.createquerydef(“Query2”, strSQL)Dim stDocName As String
stDocName = “Query2”
DoCmd.OpenReport stDocName, acPreviewSet db = Nothing
Set qdf = NothingExit_Command2_Click:
Exit SubErr_Command2_Click:
MsgBox Err.Description
Resume Exit_Command2_ClickEnd Sub
-
WSjohnhutchison
AskWoody LoungerSeptember 21, 2002 at 12:53 am #618753You need to be clear about the name for your query. You use different names on different lines.
db.QueryDefs.Delete “qry Query2”
Set qdf = db.createquerydef(“Query2”, strSQL)
Either call it “qyr Query2” or “Query2”
Before the line db.QueryDefs.Delete “qry Query2”, put in a new line
msgbox(strSQL) so that you can look at the sql string you have built.
This line strSQL = “SELECT TOP ” & Me!textTopN & “ATA.Surname, etc” needs a space before ATA.surname
strSQL = “SELECT TOP ” & Me!textTopN & ” ATA.Surname, etc -
WSrobinr
AskWoody LoungerSeptember 21, 2002 at 1:04 am #618757 -
WSpatt
AskWoody Lounger -
WSrobinr
AskWoody Lounger -
WSpatt
AskWoody Lounger -
WSrobinr
AskWoody LoungerSeptember 21, 2002 at 3:17 am #618786I live in just outside Edmonton, Alberta, Canada (St. Albert). What is the weather like there. Here it is cold (9 Celcius) and very windy today. Isn’t it funny – here we are communicating from other sides of the world. You are very clever with Access. You must have lots of training (background) with the program. Thanks again for all your help. I was so excited when it worked.
Robin -
WSpatt
AskWoody LoungerSeptember 21, 2002 at 3:24 am #618787Hi Robin,
It’s always good to know where people come from and hence what time it is where they are.
It’s about 18 Celsius currently, I’m about to go take my dogs training.
It’s funny I have been programming for about 38 years and I still get a kick when something I have written works, it’s a great feeling.I have been into Access for about 5 years now, but there are a lot of people who know a lot more than I do.
Pat -
WSrobinr
AskWoody Lounger -
WSpatt
AskWoody Lounger -
WScharlotte
AskWoody LoungerSeptember 21, 2002 at 2:01 am #618773The code Pat posted is DAO code (see, Pat, you have used DAO
), which means you need to have a DAO 3.6 reference set in order for it to work. Access 2000 and 2002 default to an ADO reference, not DAO. If you have both an ActiveX Data Objects (ADO) reference and a Microsoft DAO reference set, you’ll need to change the code like this:
Dim db As DAO.database, qdf As DAO.QueryDef, strSQL As String
-
WSpatt
AskWoody LoungerSeptember 21, 2002 at 2:06 am #618775Thanks Charlotte, I have just been into Tool/References and I realised that I needed to check the Microsoft DAO 3.5 Object Library or Microsoft DAO 3.6 Object Library as you have pointed out. I was just about to tell Robin to tick the 3.5 as I have done in Access 97 but I was not sure.
Do you have to change the code to differentiate between ADO and DAO?
Pat -
WScharlotte
AskWoody LoungerSeptember 21, 2002 at 1:59 pm #618818For Access 2000 or 2002, the DAO reference is 3.6.
ADO and DAO are different object models and you can’t mix them in a single routine, although you can use them in the same application. Unfortunately, both models contain objects with the same name, like Recordset, Property, Field, Parameter, etc. So if you don’t declare those object variables specifically as ADODB.Recordset or DAO.Property, you’ll get an error because the methods and properties of those objects are not the same in DAO and ADO. The order of the references in the list (ADO above DAO or vice versa) affects which model Access assumes when it comes across and ambiguous declaration like “Dim rst As Recordset”. If ADO is listed above DAO in references, Access will assume that means an ADODB.Recordset and will choke on methods that don’t belong in the ADO recordset object. You’ll also run into problems with “Dim db As Database” because the ADO model doesn’t have a database object. It also doesn’t have QueryDef or TableDef objects so Access will object to those if you don’t specific the object model in the declaration.
The safest way to handle it is always to declare your object variables by specifying the object model: Dim rst As DAO.Recordset or Dim rst As ADODB.Recordset, etc. ADO is an entirely different object model which is not specifically Access-aware the way DAO is. Microsoft has a site for ADO at http://microsoft.com/data/ado/%5B/url%5D and you might want to read the MSKB article Q225048 “INFO: Issues Migrating from DAO/Jet to ADO/Jet”.
-
WSrobinr
AskWoody LoungerSeptember 21, 2002 at 4:16 pm #618835Thanks Charlotte. I had heard reference to ADO and DAO but didn’t know what they meant. I also went into help and read up on References. I went into Tools, References and there is a whole list of them – some checked and most not. I did add a couple that sounded like I might need them. There is sooooo much to learn. Thanks Again. Robin
-
WScharlotte
AskWoody LoungerSeptember 21, 2002 at 7:58 pm #618850Be careful of setting references just because they look likely. I would advise you to not set references unless you know you need them. There are some you can’t avoid because Access won’t let you remove them, but the rest are only needed when your code actually uses calls into that library.
-
WSpatt
AskWoody Lounger -
WScharlotte
AskWoody Lounger -
WScarbonnb
AskWoody Lounger -
WScharlotte
AskWoody Lounger -
WSpatt
AskWoody LoungerSeptember 21, 2002 at 1:33 am #618770John is correct with the points he has made.
Before you go any further, get your object names right.
Use ‘qry Query2’ as your query, use ‘prt Query2’ as your report.Also take out the line ‘Where…’, I put that there to show you where the WHERE clause goes as well as showing how to add it to the string strSQL.
The code that you have there will work in Access97 or Access2000, I just don’t know about Access 2002, but I think you need to use DAO commands, maybe Charlotte or someone will chime in here and tell you more about this. I haven’t used DAO (or ADO for that matter) yet either.
Pat
-
WScharlotte
AskWoody Lounger -
WSpatt
AskWoody Lounger -
WSrobinr
AskWoody Lounger -
WSpatt
AskWoody Lounger
-
-
-
-
WSMarkLiquorman
AskWoody Lounger -
WSpatt
AskWoody Lounger
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
23 minutes ago -
Apple releases 18.5
by
Susan Bradley
42 minutes ago -
Fedora Linux 40 will go end of life for updates and support on 2025-05-13.
by
Alex5723
1 hour, 49 minutes ago -
How a new type of AI is helping police skirt facial recognition bans
by
Alex5723
2 hours, 27 minutes ago -
Windows 7 ISO /Windows 10 ISO
by
ECWS
9 hours, 23 minutes ago -
No HP software folders
by
fpefpe
10 hours, 8 minutes ago -
Which antivirus apps and VPNs are the most secure in 2025?
by
B. Livingston
2 hours, 21 minutes ago -
Stay connected anywhere
by
Peter Deegan
15 hours, 31 minutes ago -
Copilot, under the table
by
Will Fastie
6 hours, 44 minutes ago -
The Windows experience
by
Will Fastie
21 hours, 45 minutes ago -
A tale of two operating systems
by
Susan Bradley
12 hours, 41 minutes ago -
Microsoft : Resolving Blue Screen errors in Windows
by
Alex5723
1 day, 3 hours ago -
Where’s the cache today?
by
Up2you2
1 day, 18 hours ago -
Ascension says recent data breach affects over 430,000 patients
by
Nibbled To Death By Ducks
1 day, 11 hours ago -
Nintendo Switch 2 has a remote killing switch
by
Alex5723
11 hours, 31 minutes ago -
Blocking Search (on task bar) from going to web
by
HenryW
1 day, 19 hours ago -
Windows 10: Microsoft 365 Apps will be supported up to Oct. 10 2028
by
Alex5723
2 days, 11 hours ago -
Add or Remove “Ask Copilot” Context Menu in Windows 11 and 10
by
Alex5723
2 days, 11 hours ago -
regarding april update and may update
by
heybengbeng
2 days, 13 hours ago -
MS Passkey
by
pmruzicka
1 day, 15 hours ago -
Can’t make Opera my default browser
by
bmeacham
2 days, 20 hours ago -
*Some settings are managed by your organization
by
rlowe44
2 days, 7 hours ago -
Formatting of “Forward”ed e-mails
by
Scott Mills
2 days, 19 hours ago -
SmartSwitch PC Updates will only be supported through the MS Store Going Forward
by
PL1
3 days, 15 hours ago -
CISA warns of hackers targeting critical oil infrastructure
by
Nibbled To Death By Ducks
4 days ago -
AI slop
by
Susan Bradley
1 day, 18 hours ago -
Chrome : Using AI with Enhanced Protection mode
by
Alex5723
4 days, 1 hour ago -
Two blank icons
by
CR2
1 day, 9 hours ago -
Documents, Pictures, Desktop on OneDrive in Windows 11
by
ThePhoenix
1 day, 11 hours ago -
End of 10
by
Alex5723
4 days, 13 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.