I need to create a series of separate reports with content derived from a query, qry_LOI2. So I loop through a recordset based on the query, with a filter selecting one record at a time. At least, that's the idea. In practice it produces the same report each time, always based on the first record in the recordset. My code is - Private Sub lblProduceLettersButton_Click()
-
'
-
' Produce the LOIs for the coming Walks, if Walk details and STC and Registrar are all defined
-
'
-
Dim EmailSalutation As String, EmailBody As String, EmailSignOff As String, EmailText As String
-
Dim rsLOI2 As Recordset, strFilter As String, strLOIFile As String
-
-
Select Case SendOption
-
Case 1 ' Send option = Print & post
-
... (irrelevant)
-
-
Case 2 ' Send option = email = we'll have to do the letters individually
-
Set rsLOI2 = CurrentDb.OpenRecordset("qry_LOI2", dbOpenForwardOnly)
-
' Loop thru qry_LOI2 records and do letters one by one
-
If (rsLOI2.BOF And rsLOI2.EOF) Then
-
MsgBox "There are no letters to print. " & vbCrLf & _
-
"Check that next Walks, STC and Registrar are all defined."
-
Else
-
strLOIFile = Environ("TEMP") & "\LOIFile.PDF"
-
Do Until (rsLOI2.EOF = True)
-
strFilter = "PersonID=" & rsLOI2!PersonID
-
DoCmd.OpenReport "31: Letters of Invitation", acViewPreview, , strFilter, , R31OpenArgs
-
Debug.Print rsLOI2!PersonID, rsLOI2!First_name '''''''''''''''''''''''''''''''''''''
-
DoCmd.OutputTo acOutputReport, , acFormatPDF, strLOIFile
-
...
-
... (irrelevant - sends the LOI file as an attachment by email)
-
...
-
Kill strLOIFile ' Delete the file
-
rsLOI2.MoveNext
-
Loop
-
Me!lblEmailsDone.Visible = True
-
End If
-
-
End Select
-
-
End Sub
-
The qry_LOI2 definitely has seven records with unique numeric identifiers PersonID. The Debug.Print statement lists all seven of them.
Any help much appreciated.
Baffling indeed. But - you've answered all my questions which is good. I'm finding you increasingly easy to work with.
In the rest of the news it's still unclear why it isn't working :-( The WhereCondition value appears perfectly set and the call is set up correctly (It was before but it was just harder work to check with positional parameters).
Ah. Hang on. We don't have all your code within the loop. I'm guessing the Report is never closed from one iteration to the next. If you open a Report that is already open the effect is simply to switch focus to the existing report. If you want to run it with different parameters then you will certainly need to close it before opening it again.
I don't see where the documentation makes that clear but I found it to be the case in my testing.
8 2158 NeoPa 32,556
Expert Mod 16PB
Hi Petrol.
It's generally helpful to use named parameters in code when asking for help with that code - especially for calls of complicated procedures with multiple parameters, and more especially still when some are optional. That's just a tip for the future; not a chastisement ;-)
In this case the parameters are expressed in the right order at least. So, nothing obviously wrong. It would be helpful to see the Debug info, but also to ensure you include strFilter in your Debug command.
Another question for you is "Have you traced the code and checked whether or not the Report opens to the correct page within Access at least?". You don't say explicitly but I suspect you are seeing the first record every time when you look at the results of the DoCmd.OutpuTo() call?
Not sure that I fully understood that ... do you mean it would be more helpful to say - DoCmd.OpenReport ReportName:="31: Letters of Invitation", View:=acViewPreview,,WhereCondition:=strFilter,,OpenArgs:=R31OpenArgs
-
DoCmd.OutputTo ObjectType:=acOutputReport,, ObjectFormat:=acOutputPDF, OutputFile:=strLOIFile]
?
I must admit I haven't really used that form, but hopefully this will make it clearer.
Thanks for the tip about adding strFilter to the Debug.Print. I had checked it before in the immediate window, but I've now added it to the Debug.Print statement. The result is below: - PersonID=28 28 Althea
-
PersonID=29 29 Brenda
-
PersonID=30 30 Corrie
-
PersonID=31 31 Di
-
PersonID=32 32 Evelyn
-
PersonID=33 33 Francine
-
PersonID=34 34 Grace
-
Finally, yes it's possible that the OutputTo is just outputting the first report over and over, but I did put a breakpoint on the OutputTo statement (line 24) and at each iteration of the loop it was the first report that was shown on screen. Baffling!
NeoPa 32,556
Expert Mod 16PB
Baffling indeed. But - you've answered all my questions which is good. I'm finding you increasingly easy to work with.
In the rest of the news it's still unclear why it isn't working :-( The WhereCondition value appears perfectly set and the call is set up correctly (It was before but it was just harder work to check with positional parameters).
Ah. Hang on. We don't have all your code within the loop. I'm guessing the Report is never closed from one iteration to the next. If you open a Report that is already open the effect is simply to switch focus to the existing report. If you want to run it with different parameters then you will certainly need to close it before opening it again.
I don't see where the documentation makes that clear but I found it to be the case in my testing.
Aha! That may well be it.
I won't have a chance to test it today, but it sounds like you may have hit upon the solution. I'll keep you posted. :)
NeoPa 32,556
Expert Mod 16PB NeoPa 32,556
Expert Mod 16PB
It looks like instead of adding the extra value of strFilter to the Debug.Print I should have asked for the value of Reports("31: Letters of Invitation").Filter . Or maybe even both actually. That would have shown the problem easily enough. I'll know for next time.
NeoPa, you're a legend!
Post #4 (close report) did the trick.
Thank you.
NeoPa 32,556
Expert Mod 16PB
Very pleased to hear that helped Petrol. Particularly as I had to learn something new myself in order to post it ;-)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andrew |
last post by:
Hi All:
I am using Access2000 and I find that the command to open an Access report
in preview mode is very slow: DoCmd.OpenReport rptABC, acViewPreview, "",
""
The scenario is this:
- The...
|
by: Filips Benoit |
last post by:
Dear All,
DoCmd.OpenReport RPT_TEST, A_PREVIEW, QueryAsFilter doesn't work correctly
in A97 but works OK in A2K.
Is this a bug for A97 ?
Filip
|
by: LoopyNZ |
last post by:
Hi there,
I've converted an Access 97 front end to Access 2000, but when I try to
run a VBA DoCmd.OpenReport (e.g. DoCmd.OpenReport
"rpt_programme_listing") line, Access completely crashes...
|
by: enough2Bdangerous |
last post by:
Access database (file format 2002-2003) generates reports with docmd.openreport but returns 3011 runtime error related to the printer. Switching Windows default printer is a work around but need to...
|
by: enough2Bdangerous |
last post by:
access runtime error 3011 on docmd.openreport
--------------------------------------------------------------------------------
Access database (file format 2002-2003) generates reports with...
|
by: Simon |
last post by:
Dear reader,
The syntax for Docmd.OpenReport is:
OpenReport(ReportName, View, FilterName, WhereCondition, WindowMode,
OpenArgs)
Example
The following example prints Sales Report while...
|
by: boliches64 |
last post by:
I am trying to open a report to view an invoice for a specific invoice number. I have looked on the net and todate have not been able to resolve my problem! Help please,
My code is:...
|
by: gazza10001 |
last post by:
Hi
i hope you can help my company uses access and has modified for its needs usually what happens is you serach for the invoice by its number and then it brings all the information up such as...
|
by: DAHMB |
last post by:
I have a command buttton on a form that calls a report and filters the report as follows:
Private Sub btnLetter_Click()
Dim strSQL As String
Dim stLetter As String
Dim stFilter As String
...
|
by: epifinygirl |
last post by:
With my code below, I am trying to filter the 1 report for each record set in the "Temp Table". The report is filtered from a query "Carrier Report".
Public Function OrgIDReports()
Dim db As...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |