This may be just me, but I won’t open any attached files unless I know exactly who is sending it and why; not even from my father! I’d advise you to copy the relevant portion of your code into your message.
![]() |
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 |
-
Handling Report Recordsets with a For loop (2000)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Handling Report Recordsets with a For loop (2000)
- This topic has 39 replies, 4 voices, and was last updated 23 years, 8 months ago.
AuthorTopicWSMarkLiquorman
AskWoody LoungerAugust 6, 2001 at 7:58 pm #358798Viewing 1 reply threadAuthorReplies-
WSgrovelli
AskWoody LoungerAugust 7, 2001 at 6:16 am #536133When I try and open the Durate Report(put 2.4 in the text25 text box on the Mask1 form before opening it) of the attached file I get the following:
Run-time error ‘2147217904 (80040e10)’: No value given for one or more required parameters.
I don’t see which parameter(s) is missing, do you?Private Sub Report_Activate()
Dim a
Set cnn = CurrentProject.Connection
For a = 0 To Forms!Mask1!Text25 * 1000 Step Forms!Mask1!Passo
rst.Open “SELECT MEDIEGIO.Mese, MEDIEGIO.Giorno, Avg(MEDIEGIO.Cassiglio) AS selectedfield, Nz(selectedfield)/24 AS AvgOfPowerday” _
& ” FROM MEDIEGIO INNER JOIN tblMonthOrder ON MEDIEGIO.Mese = tblMonthOrder.Month” _
& ” WHERE MEDIEGIO.Anno Between 1997 And 2000 and AvgOfPowerday >=” & a _
& ” GROUP BY MEDIEGIO.Mese, MEDIEGIO.Giorno, tblMonthOrder.OrderOfMonth” _
& ” ORDER BY tblMonthOrder.OrderOfMonth;”, cnn, adOpenKeyset, adLockOptimistic
durate = rst.RecordCount
Reports!durate!Passo = a
Medie = (a * durate) / 365
rst.Close
Next
End Sub -
WScharlotte
AskWoody LoungerAugust 6, 2001 at 8:06 pm #536266Giorgio,
You sometimes get that message for other reasons. Try putting a breakpoint in your code, or hit the Debug button if it’s offered, or do a Ctrl+Break before clicking OK on the message box and try to troubleshoot what’s actually happening. Part of learning to program is learning to figure out what you did wrong. We’re glad to give you our assistance, but you also need to start learning how to work through some of these problems yourself .
I agree with Mark in that you need to post the code that is throwing the error instead of expecting anyone to figure it out by trying to make your database work.
-
WSgrovelli
AskWoody LoungerAugust 7, 2001 at 6:57 am #536349 -
WScharlotte
AskWoody LoungerAugust 7, 2001 at 2:06 pm #536382At the risk of repeating myself, don’t try and do it all in one step! And for goodness sake, set Option Explicit at the top of the module and declare all your variables. That will save you all the problems you’ve had with misspelling names. And declare any module level variables with an m at the front of the name to make it obvious they don’t need to be declared in the current routine.
As far as the rest goes, you’re having problems because you aren’t clearly thinking through what you want to do. You don’t open a recordset over and over in a loop, you open the recordset once and then loop through the records. What exactly are you trying to do here?
What does “durate = rst.RecordCount” mean? You hae a control or field on the report named duarter–is that what you’re referring to? Or are you referring to a variable with the same name? RecordCount in ADO recordsets normally returns a -1 or 0, meaning there are records or there aren’t. It doesn’t work quite like RecordCount in DAO.
By coding it like the following, you can put a break in at the rst.Open line and print the SQL string to the immediate window. Then copy that SQL and paste it into the SQL window of a new query. That will allow you to see exactly what might be wrong with the query.
Private Sub Report_Activate() Dim a As Long Dim cnn As ADODB.Connection Dim lngSteps as Long Dim strSQL As String Set cnn = CurrentProject.Connection lngSteps = Forms!Mask1!Text25 * 1000 Step Forms!Mask1!Passo strSQL = "SELECT MEDIEGIO.Mese, MEDIEGIO.Giorno, Avg(MEDIEGIO.Cassiglio) " _ & "AS selectedfield, Nz(selectedfield)/24 " _ & " AS AvgOfPowerday " _ & "FROM MEDIEGIO INNER JOIN tblMonthOrder ON " _ & "MEDIEGIO.Mese = tblMonthOrder.Month" _ & " WHERE MEDIEGIO.Anno Between 1997 And 2000 and AvgOfPowerday >=" & a _ & " GROUP BY MEDIEGIO.Mese, MEDIEGIO.Giorno, tblMonthOrder.OrderOfMonth" _ & " ORDER BY tblMonthOrder.OrderOfMonthSELECT MEDIEGIO.Mese, " _ & "MEDIEGIO.Giorno, Avg(MEDIEGIO.Cassiglio) AS " _ & "selectedfield, Nz(selectedfield)/24 AS AvgOfPowerday" _ & " FROM MEDIEGIO INNER JOIN tblMonthOrder ON MEDIEGIO.Mese = " _ & "tblMonthOrder.Month" _ & " WHERE MEDIEGIO.Anno Between 1997 And 2000 and AvgOfPowerday >=" & a _ & " GROUP BY MEDIEGIO.Mese, MEDIEGIO.Giorno, " _ & "tblMonthOrder.OrderOfMonth" _ & " ORDER BY tblMonthOrder.OrderOfMonth" rst.Open strSQL, cnn, adOpenKeyset, adLockOptimistic rst.MoveLast durate = rst.AbsolutePostion rst.MoveFirst For a = 0 To lngSteps Reports!durate!Passo = a Medie = (a * durate) / 365 rst.MoveNext Next rst.Close End Sub
-
WSgrovelli
AskWoody LoungerAugust 8, 2001 at 11:30 am #536516
What does “durate = rst.RecordCount” mean? You have a control or field on the report named durate–is that what you’re referring to?
Yes, it’s a text box in the Durate Report.
As far as the rest goes, you’re having problems because you aren’t clearly thinking through what you want to do. You don’t open a recordset over and over in a loop, you open the recordset once and then loop through the records. What exactly are you trying to do here?
I’m trying to obtain the report depicted in the attached text file.
set Option Explicit at the top of the module
If you look at the attachment of the first post, I did set Option Explicit in the Declaration section at the top of the Report_Durate module.
RecordCount in ADO recordsets normally returns a -1 or 0, meaning there are records or there aren’t. It doesn’t work quite like RecordCount in DAO.
Actually the RecordCount Property in ADO does indicate the current number of records in a Recordset object. I know because I’ve used it before, see this.
(How I suffer when I have to correct you.)
I’ve created a query(Query1, see attached mdb) using the same Select statement contained in the rst.Open command of the Sub Report_Activate and another query(Query2) which is the same as Query1 except I took out AvgOfPowerday from the Where clause.
How come Query2 works while Query1 doesn’t?
Ciao -
WSrory
AskWoody LoungerAugust 8, 2001 at 12:39 pm #536537Hi,
I think your problem is that the select statement is effectively opening a recordset which calculates the averages and then trying to restrict that recordset based on one of the values it has calculated. This is really 2 queries and you can’t combine them into one, as I think has already been mentioned. What you could do is open the recordset and then apply a filter to it – e.g.:
rst.Filter = “[selectedfield] >= ” & a *24
but this still doesn’t get around the fact that you are repeatedly opening the recordset rather than looping through the values in it, so your report will only actually show the values for the last pass. If you can explain what exactly you’re trying to achieve, perhaps we can help. (My impression though is that your current method won’t do what you’re after.)
Hope that helps. -
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 6:42 am #536707Now I’m sure to wade the waters.
Following your advice I’ve moved the Where clause of Sub Report_Activate into
the Filter method for the recorset(see attached file) but this time the code stops at the rst.Filter line with the message: Run-time error ‘3001’: the application is using arguments that are of the wrong type, are out of acceptable range, or are in conflict with one another.
You came in just at the right time(how could it be any different?) because something very similar happened in the and you came through with flying colors.
What’s wrong with
rst.Filter = “MEDIEGIO.Anno Between 1997 And 2000 and AvgOfPowerday >=” & a
?
Ciao -
WSrory
AskWoody Lounger -
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 8:41 am #536722 -
WSrory
AskWoody LoungerAugust 9, 2001 at 12:06 pm #536735Hi,
I imagine that’s because there are no records in the recordset at some point, due to the filter, so you can’t move to the last one! You can either add an If Not rst.EOF then rst.MoveLast or simply skip the movelast altogether – because you’re using a keyset cursor, Recordcount should return the actual number of records in the recordset correctly. In theory anyway!
Hope that helps. -
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 12:53 pm #536745O.k., now that I’ve commented out the rst.MoveLast line, the For loop loops nicely but your prediction(“but this still doesn’t get around the fact that you are repeatedly opening the recordset rather than looping through the values in it, so your report will only actually show the values for the last pass.”) has come true(never doubted it, I knew it would come, at least now the rest of the work is done
).
I think to enable the report to show all the 50 lines(see text file attached to this), one should conjugate a “Do until rst.EOF” with the existing For loop but I don’t know how to do that. -
WSgrovelli
AskWoody Lounger -
WScharlotte
AskWoody LoungerAugust 8, 2001 at 2:02 pm #536558[indent]
I’m trying to obtain the report depicted in the attached text file.
[/indent]In programming, a picture may *not* be worth a thousand words. You need to verbalize what all that business with the loop and opening and closing the recordset is supposed to accomplish within the report.[indent]
If you look at the attachment of the first post, I did set Option Explicit in the Declaration section at the top of the Report_Durate module.
[/indent]In that case, where did you declare the variables that weren’t declared in the routine? It’s customary to give module level variable names a one letter prefix to denote where they were declared. If it was declared at a module level, a variable might be called mVariable. If it was declared public in a standard module, it might be called gVariable. Only local variables (those declared in a Sub or Function) don’t have a special character.[indent]
Actually the RecordCount Property in ADO does indicate the current number of records
[/indent]Not without a movelast and movefirst, which you had left out. Otherwise all you get is a zero or a minus one indicating whether or not there are records.
The second query works because you took AvgOfPowerday out of the Where clause. I believe Rory explained that in his reply.
-
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 7:53 am #536717You’re always a step ahead because you can put things in perspective so me knowing in advance that you’re eventually bound to rest your case makes me suffer when I’m about to correct something you said.
You need to verbalize what all that business with the loop and opening and closing the recordset is supposed to accomplish within the report.
All right, here we go:
Starting with the qryProdGiox of the attached file, I’m trying to obtain the report depicted in the attached text file and here are the house-keeping details:
I begin by setting to 0 the start value of the variable “a as Long” declared in Sub Report_Activate and ask myself, “How many times the value in the qryProdGiox.AvgOfPowerday field is equal to or greater than a”? And that’s the value that goes into the Duration column you see in the text file. The Step column in the text file contains the value assumed by “a” during each cycle and the Average column is equal to (Duration*a)/365.
Afterwards I increase the value of “a” by the amount contained in the Passo text box of the Mask1 form and repeat the cycle.
All of this I tried to accomplish in Sub Report_Activate.
Ciao -
WScharlotte
AskWoody LoungerAugust 9, 2001 at 1:24 pm #536756[indent]
makes me suffer when I’m about to correct something you said.
[/indent]If there was any correction in there, I failed to notice it. But feel free to suffer if it makes you happy.
My question should have included a “why are you doing this” as well. I can see *what* you’re doing, but the reason for it escapes me. Is this a one-time calculation to populate an unbound control in your report, or are you attempting to populate a field for each record?
-
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 1:47 pm #536759
quote]makes me suffer when I’m about to correct something you said.If there was any correction in there, I failed to notice it.
Ouch, I was still referring to this
Is this a one-time calculation to populate an unbound control in your report, or are you attempting to populate a field for each record?
The former.
I’m trying to populate the three unbound controls you see in the report according to the goals set in my explanation, each column(Duration, Step, Average) mentioned in said explanation corresponds to an unbound text box of the report.
Ciao -
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 1:21 pm #536755
Not without a movelast and movefirst, which you had left out. Otherwise all you get is a zero or a minus one indicating whether or not there are records.
Hi Charlotte, did you now that if you use a keyset cursor, you can skip using the MoveLast method for recorsets?(Rorytaught me that)
If I give a default value to the text25 text box in the Mask1 form using the properties sheet instead of typing it in, then the default value is not accepted. Even if, after opening the Mask1 form, I tab out of text25, it still behaves as if I hadn’t left text25. Is it because of this?
Ciao
-
-
-
-
WSMarkLiquorman
AskWoody LoungerAugust 7, 2001 at 11:33 am #536364Are you sure both of those forms are opened? Also, I would use “Dim a As Long”. Using just “Dim a” declares it has a variant. Access can process Longs much faster than Variants. Oh, and that part of the WHERE clause that is Avgof Powerday=a, shouldn’t that be part of a Having statement. Not sure here, as I always have trouble with WHERE vs HAVING.
-
WSgrovelli
AskWoody Lounger -
WScharlotte
AskWoody Lounger
-
-
-
WSMarkLiquorman
AskWoody Lounger -
WSgrovelli
AskWoody Lounger -
WSMarkLiquorman
AskWoody Lounger -
WSgrovelli
AskWoody Lounger -
WSMarkLiquorman
AskWoody LoungerAugust 8, 2001 at 3:05 pm #536575Okay, let’s look at your code again. Your problem stems from this part of the code:
Avg(MEDIEGIO.Cassiglio) AS selectedfield,
Nz(selectedfield)/24 AS AvgOfPowerdayYou can’t use “selectedfield” or “avgOfPowerday” anywhere else in the select statement. The AS clause for a field merely define how this field is named in the resultant recordset, so you can use “rs!selectedfield”, etc. Therefore, you must always use the expression that created it. So the 2nd line above should read:
NZ(Avg(mdiegio.cassiglio))/24 as AvgOfPowerDay
Similarly, when you use AvgOfPowerDay in your WHERE statement, you must use the expression above instead. Also, this should appear in the Having statement, not the Where statement.
Try use the QBE grid to create this query. Then look at the SQL code it creates.
-
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 6:07 am #536705I’ve done what you said, using the originating expressions instead of the named fields and Having instead of Where(see attached mdb) and now the error message says, “Run-time error’-2147217887 (80040e21)’:You tried to execute a query that doesn’t include the specified expression ‘MEDIEGIO.Anno Between 1997 and 2000 And Nz(Avg(MEDIEGIO.Cassiglio))/24>=0’ as part of an aggregate function.”
Ciao -
WSMarkLiquorman
AskWoody LoungerAugust 9, 2001 at 3:00 pm #536774>>’MEDIEGIO.Anno Between 1997 and 2000 <<
This part needs to be in the WHERE section. Look at it this way. Use the WHERE section to specify conditions that determine which of the underlying records are to be included/excluded. Use the HAVING section to determine which of the Groups are to be included/excluded. So you want to include records with Anno between 1997 and 2000; but you want to exclude groups based on that Avg/24.
-
WSgrovelli
AskWoody LoungerAugust 9, 2001 at 3:10 pm #536776Thanks Mark, but Rory already covered .
Now">this.
Can you please lend a hand?
Ciao -
WSMarkLiquorman
AskWoody LoungerAugust 9, 2001 at 6:15 pm #536800Actually, I don’t understand what your code is doing. Perhaps you could give a description. You are opening a recordset with each iteration of the For…Next loop. To get the recordcount of the recordset, you must to a rs.MoveLast, which your initial code didn’t include. Why are you then looping through the records in the recordset? You initial code doesn’t show anything like that.
-
WSgrovelli
AskWoody Lounger -
WSMarkLiquorman
AskWoody LoungerAugust 10, 2001 at 3:14 pm #536917 -
WSgrovelli
AskWoody Lounger -
WSrory
AskWoody LoungerAugust 13, 2001 at 1:45 pm #537117Hi,
I think the easiest way for you to get what you want will involve writing the values you want to a table after opening each recordset (given that you’re not actually interested in the individual values in the recordset but rather the recordcount). You can then base your report on this table and you should get a row for each returned recordcount and not just the last one.
Hope that helps. -
WSgrovelli
AskWoody Lounger -
WSgrovelli
AskWoody Lounger -
WSgrovelli
AskWoody Lounger -
WSrory
AskWoody LoungerAugust 17, 2001 at 1:35 pm #537912Hi,
As far as I know, the open event of a report fires before the report retrieves its data (unlike a form) whereas the Activate event does not. Effectively I guess the report was obtaining a value for its 3 bound controls, then the recordsource was being changed, so when you go to print or preview, it then has to go back and retrieve the new data, from the first record. Hence you end up with the first row duplicated. There’s a fair bit of speculation in there though!
As for the default value question, yes it is related to the other post. The value of the textbox isn’t set to its default value until the record is saved – otherwise if you had default values in a form, as soon as you moved to a new record, it would be created even if you didn’t actually enter anything or manually save it.
Hope that helps.
-
-
-
-
-
WSgrovelli
AskWoody LoungerAugust 7, 2001 at 7:01 am #536344I’ve followed your advice and edited the first post.
Could the mdb files I attach be prone to carrying viruses? I use the corporate edition of Norton AntiVirus installed on my notebook plus a corporate firewall and I’ve never had any problems, passing scot-free through Love Bug, Melissa and similar amenities.
Ciao -
WSMarkLiquorman
AskWoody LoungerAugust 7, 2001 at 11:23 am #536363It is possible for a .mdb file to contain a virus, though it is unlikely. Primarily because few people distribute .mdb files, so it has no appeal to virus makers. I just take all the precautions I can, and I’ve gone unscathed also (except for some years ago, when I got a Word macro virus).
Additionally, for those of us who see ALOT of messages, downloading a file and trying to find the exact spot you are talking about is bothersome. We (at least me) tend to skip over them, thinking we will possibly get to it later. This is not to say we don’t download databases, just not routinely.
-
Viewing 1 reply thread -

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
-
Uninstalr Updates
by
jv16
5 hours, 44 minutes ago -
Apple zero days for April
by
Susan Bradley
1 hour, 2 minutes ago -
CVE program gets last-minute funding from CISA – and maybe a new home
by
Nibbled To Death By Ducks
6 hours, 32 minutes ago -
Whistleblower describes DOGE IT dept rumpus at America’s labor watchdog
by
Nibbled To Death By Ducks
18 hours, 22 minutes ago -
Seeing BSOD’s on 24H2?
by
Susan Bradley
1 hour, 16 minutes ago -
TUT For Private Llama LLM, Local Installation and Isolated from the Internet.
by
bbearren
8 hours, 46 minutes ago -
Upgrade from Windows 10 to 11
by
Holdsworth8
1 day, 3 hours ago -
Microsoft : AI-powered deception: Emerging fraud threats and countermeasures
by
Alex5723
1 day, 5 hours ago -
0patch
by
WSjcgc50
6 hours, 50 minutes ago -
Devices might encounter blue screen exception with the recent Windows updates
by
Susan Bradley
23 hours, 14 minutes ago -
Windows 11 Insider Preview Build 22631.5261 (23H2) released to Release Preview
by
joep517
1 day, 8 hours ago -
Problem opening image attachments
by
RobertG
1 day, 10 hours ago -
advice for setting up a new windows computer
by
routtco1001
2 days, 1 hour ago -
It’s Identity Theft Day!
by
Susan Bradley
1 day, 5 hours ago -
Android 15 require minimum 32GB of storage
by
Alex5723
2 days, 6 hours ago -
Mac Mini 2018, iPhone 6s 2015 Are Now Vintage
by
Alex5723
2 days, 6 hours ago -
Hertz says hackers stole customer credit card and driver’s license data
by
Alex5723
2 days, 6 hours ago -
Firefox became sluggish
by
Rick Corbett
2 days, 3 hours ago -
Windows 10 Build 19045.5794 (22H2) to Release Preview Channel
by
joep517
2 days, 10 hours ago -
Windows 11 Insider Preview Build 22635.5235 (23H2) released to BETA
by
joep517
2 days, 11 hours ago -
A Funny Thing Happened on the Way to the Forum
by
bbearren
1 day, 8 hours ago -
Download speeds only 0.3Mbps after 24H2 upgrade on WiFi and Ethernet
by
John
5 hours, 17 minutes ago -
T-Mobile 5G Wireless Internet
by
WSmmi16
1 day, 8 hours ago -
Clock missing above calendar in Windows 10
by
WSCape Sand
1 day, 10 hours ago -
Formula to Calculate Q1, Q2, Q3, or Q4 of the Year?
by
WSJon5
3 days, 1 hour ago -
The time has come for AI-generated art
by
Catherine Barrett
2 days, 5 hours ago -
Hackers are using two-factor authentication to infect you
by
B. Livingston
2 days, 15 hours ago -
23 and you
by
Max Stul Oppenheimer
2 days, 22 hours ago -
April’s deluge of patches
by
Susan Bradley
1 day, 2 hours ago -
Windows 11 Windows Updater question
by
Tex265
16 hours, 40 minutes 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.