473,387 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unable to get filter to work on DoCmd.OpenReport

204 128KB
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
Expand|Select|Wrap|Line Numbers
  1. Private Sub lblProduceLettersButton_Click()
  2. '
  3. '   Produce the LOIs for the coming Walks, if Walk details and STC and Registrar are all defined
  4. '
  5. Dim EmailSalutation As String, EmailBody As String, EmailSignOff As String, EmailText As String
  6. Dim rsLOI2 As Recordset, strFilter As String, strLOIFile As String
  7.  
  8. Select Case SendOption
  9.     Case 1                    ' Send option = Print & post
  10. ... (irrelevant)
  11.  
  12.     Case 2                      ' Send option = email = we'll have to do the letters individually
  13.         Set rsLOI2 = CurrentDb.OpenRecordset("qry_LOI2", dbOpenForwardOnly)
  14.         '   Loop thru qry_LOI2 records and do letters one by one
  15.         If (rsLOI2.BOF And rsLOI2.EOF) Then
  16.             MsgBox "There are no letters to print. " & vbCrLf & _
  17.                    "Check that next Walks, STC and Registrar are all defined."
  18.         Else
  19.             strLOIFile = Environ("TEMP") & "\LOIFile.PDF"
  20.             Do Until (rsLOI2.EOF = True)
  21.                 strFilter = "PersonID=" & rsLOI2!PersonID
  22.                 DoCmd.OpenReport "31: Letters of Invitation", acViewPreview, , strFilter, , R31OpenArgs
  23. Debug.Print rsLOI2!PersonID, rsLOI2!First_name             '''''''''''''''''''''''''''''''''''''
  24.                 DoCmd.OutputTo acOutputReport, , acFormatPDF, strLOIFile
  25. ...
  26. ...   (irrelevant - sends the LOI file as an attachment by email)
  27. ...
  28.                 Kill strLOIFile                ' Delete the file
  29.                 rsLOI2.MoveNext
  30.             Loop
  31.             Me!lblEmailsDone.Visible = True
  32.         End If
  33.  
  34. End Select
  35.  
  36. End Sub
  37.  
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.
Jan 16 '20 #1

✓ answered by NeoPa

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?
Jan 16 '20 #2
Petrol
204 128KB
Not sure that I fully understood that ... do you mean it would be more helpful to say
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport ReportName:="31: Letters of Invitation", View:=acViewPreview,,WhereCondition:=strFilter,,OpenArgs:=R31OpenArgs
  2. 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:
Expand|Select|Wrap|Line Numbers
  1. PersonID=28    28           Althea
  2. PersonID=29    29           Brenda
  3. PersonID=30    30           Corrie
  4. PersonID=31    31           Di
  5. PersonID=32    32           Evelyn
  6. PersonID=33    33           Francine
  7. PersonID=34    34           Grace
  8.  
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!
Jan 16 '20 #3
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.
Jan 16 '20 #4
Petrol
204 128KB
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. :)
Jan 16 '20 #5
NeoPa
32,556 Expert Mod 16PB
Yes please :-)
Jan 16 '20 #6
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.
Jan 16 '20 #7
Petrol
204 128KB
NeoPa, you're a legend!
Post #4 (close report) did the trick.
Thank you.
Jan 17 '20 #8
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 ;-)
Jan 17 '20 #9

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

Similar topics

1
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...
1
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
1
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...
2
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...
3
by: enough2Bdangerous | last post by:
access runtime error 3011 on docmd.openreport -------------------------------------------------------------------------------- Access database (file format 2002-2003) generates reports with...
4
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...
19
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:...
4
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...
5
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 ...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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
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...
0
jinu1996
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...

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.