473,385 Members | 1,309 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,385 software developers and data experts.

Filter a report in an auto email code

DJRhino1175
221 128KB
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Close()
  2.  
  3. If Me.[REJECT AREA].Value = "Purchased" Then
  4.  
  5. Dim strEMail As String
  6. Dim oOutlook As Object
  7. Dim oMail As Object
  8. Dim strAddr As String
  9. Dim MyDB As DAO.Database
  10. Dim rstEMail As DAO.Recordset
  11. Dim strReportName As String
  12.  
  13. Set oOutlook = CreateObject("Outlook.Application")
  14. Set oMail = oOutlook.CreateItem(0)
  15.  
  16. strReportName = "Purchased New Reject Tag Report"
  17.  
  18. 'This has the filter I need to use
  19. 'DoCmd.OpenReport "Reject Tag Report", acViewPreview, , [REJECT TAG NUMBER] = [Forms]![Reject Tag Form]![REJECT TAG NUMBER], acHidden
  20. DoCmd.OutputTo acOutputReport, "Reject Tag Report", acFormatPDF, "N:\Inspect\New Reject Tag Database\Reports" & _
  21.                "\" & strReportName & "-" & Format(DATE, "mm-dd-yy") & ".pdf", False, , , acExportQualityPrint
  22.  
  23. 'Retrieve all E-Mail Addressess in tblEMailAddress
  24. Set MyDB = CurrentDb
  25. Set rstEMail = MyDB.OpenRecordset("Select * From tblEMailPurchased", dbOpenSnapshot, dbOpenForwardOnly)
  26.  
  27. With rstEMail
  28.   Do While Not .EOF
  29.     'Build the Recipients String
  30.     strEMail = strEMail & ![EmailAddress] & ";"
  31.       .MoveNext
  32.   Loop
  33. End With
  34. '--------------------------------------------------
  35.  
  36. With oMail
  37.   .To = Left$(strEMail, Len(strEMail) - 1)        'Remove Trailing ;
  38.   .Body = "Please review the attached report."
  39.   .Subject = Replace(Replace("REJECT TAG#|1 P/N: |2", "|1", Nz(Space(1) & [REJECT TAG NUMBER], "")), "|2", Nz([Part Number] & Space(1) & [Descriptive Reason], ""))
  40.   .Display
  41.   .Attachments.Add "N:\Inspect\New Reject Tag Database\Reports" & "\" & strReportName & "-" & Format(DATE, "mm-dd-yy") & ".pdf"
  42.  
  43. End With
  44.  
  45. Set oMail = Nothing
  46. Set oOutlook = Nothing
  47.  
  48. rstEMail.Close
  49. Set rstEMail = Nothing
  50.  
  51. Dim aFile As String
  52. aFile = "N:\Inspect\New Reject Tag Database\Tables\Emailed reports\Purchased New Reject Tag Report.pdf"
  53. If Len(Dir$(aFile)) > 0 Then
  54.      Kill aFile
  55. End If
  56. End If
  57.  
  58. End Sub
99.9% of this code works, the issue I'm having is the output of the report to filter out everything but the current report.

This is the section I'm sure I have something wrong -
Expand|Select|Wrap|Line Numbers
  1. 'This has the filter I need to use
  2. 'DoCmd.OpenReport "Reject Tag Report", acViewPreview, , [REJECT TAG NUMBER] = [Forms]![Reject Tag Form]![REJECT TAG NUMBER], acHidden
  3. DoCmd.OutputTo acOutputReport, "Reject Tag Report", acFormatPDF, "N:\Inspect\New Reject Tag Database\Reports" & _
  4.                "\" & strReportName & "-" & Format(DATE, "mm-dd-yy") & ".pdf", False, , , acExportQualityPrint
If I run this as is it exports hundreds of reports.

Any help will be greatly appreciated.

Thanks,

Rhino
May 29 '19 #1
19 1478
zmbd
5,501 Expert Mod 4TB
Good Morning:

Best method is to have the report based upon a parameter query that uses the form control value for the WHERE clause then use the DoCmd.OutputTo

Using the code you've posted
- Open the report in preview - you may have to have the report visible...
- Remove the explicit call to the report object. You've already opened it in preview. When one uses the explicit call to the report, the command opens that report as a new run not as the currently open, filtered, report. :)

- You know I usually recommend building the strings outside of the function so that you have something like:
Expand|Select|Wrap|Line Numbers
  1. '(...)Air Code so you should double check it
  2. '
  3. Dim myReportWhere As String
  4. '
  5. myReportWhere = "[REJECT TAG NUMBER] = " _
  6.   & [Forms]![Reject Tag Form]![REJECT TAG NUMBER]
  7. '
  8. DoCmd.OpenReport _
  9.   ReportName:="Reject Tag Report", _
  10.   View:=acViewPreview, _
  11.   WhereCondition:=myReportWhere, _
  12.   WindowMode:=acHidden  '<You may have to have the report visible!
  13. '
  14. Dim myOutPutFile As String
  15. '
  16. myOutPutFile = "N:\Inspect\New Reject Tag Database\Reports" _
  17.   & " \" & strReportName & "-" _
  18.   & Format(Date, "mm-dd-yy") & ".pdf"
  19. '
  20. DoCmd.OutputTo _
  21.   objecttype:=acOutputReport, _
  22.   Outputformat:=acFormatPDF, _
  23.   Outputfile:=myOutPutFile, _
  24.   AutoStart:=False, _
  25.   Outputquality:=acExportQualityPrint
  26. '(...)
May 30 '19 #2
DJRhino1175
221 128KB
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo _
  2.   objecttype:=acOutputReport, _
  3.   Outputformat:=acFormatPDF, _
  4.   Outputfile:=myOutPutFile, _
  5.   AutoStart:=False, _
  6.   Outputquality:=acExportQualityPrint
I put the changes in that you suggested and it errors out at the above code section. This is a different format then I'm use to and having a hard time debugging it.

Error code is 2487 The object argument for the action or method is blank or invalid.

Is this because the report is hidden?
May 30 '19 #3
DJRhino1175
221 128KB
I got the above to work but getting an error now at
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputReport, "Reject Tag Report", acOutputReport, myOutPutFile, False, , , acExportQualityPrint
Iget run-time error 2302 Microsoft Access can't save the output data to the file you've selected

Here is the full code
Expand|Select|Wrap|Line Numbers
  1. If Me.[REJECT AREA].Value = "Purchased" Then
  2.  
  3. Dim strEMail As String
  4. Dim oOutlook As Object
  5. Dim oMail As Object
  6. Dim strAddr As String
  7. Dim MyDB As DAO.Database
  8. Dim rstEMail As DAO.Recordset
  9. Dim strReportName As String
  10. Dim myReportWhere As String
  11. Dim myOutPutFile As String
  12.  
  13. Set oOutlook = CreateObject("Outlook.Application")
  14. Set oMail = oOutlook.CreateItem(0)
  15.  
  16. strReportName = Replace(Replace("REJECT TAG#|1 P/N: |2", "|1", Nz(Space(1) & [REJECT TAG NUMBER], "")), "|2", Nz([Part Number] & Space(1) & [Descriptive Reason], ""))
  17.  
  18.  
  19. myReportWhere = "[REJECT TAG NUMBER] = [Forms]![Reject Tag Form]![REJECT TAG NUMBER]"
  20.  
  21. DoCmd.OpenReport "Reject Tag Report", acViewPreview, , myReportWhere, acWindowNormal
  22.  
  23. myOutPutFile = "N:\Inspect\New Reject Tag Database\Reports" & "\" & strReportName & ".PDF"
  24.  
  25. DoCmd.OutputTo acOutputReport, "Reject Tag Report", acOutputReport, myOutPutFile, False, , , acExportQualityPrint
  26.  
May 30 '19 #4
zmbd
5,501 Expert Mod 4TB
As I thought, you have to have the report visible.
Expand|Select|Wrap|Line Numbers
  1. '(...)Air Code so you should double check it
  2.  '
  3.  Dim myReportWhere As String
  4.  '
  5.  myReportWhere = "[PK_Students] = 1"
  6.  '
  7.  DoCmd.OpenReport _
  8.    ReportName:="ReportStudents", _
  9.    View:=acViewPreview, _
  10.    WhereCondition:=myReportWhere, _
  11.    WindowMode:=acWindowNormal '<You have to have the report visible
  12. '
  13.  Dim myOutPutFile As String
  14.  '
  15.  myReportWhere = "[REJECT TAG NUMBER] = " _
  16.   & [Forms]![Reject Tag Form]![REJECT TAG NUMBER]
  17.  '
  18.  DoCmd.OutputTo _
  19.    Objecttype:=acOutputReport, _
  20.    Outputformat:=acFormatPDF, _
  21.    Outputfile:=myOutPutFile, _
  22.    AutoStart:=False, _
  23.    Outputquality:=acExportQualityPrint
  24. '
  25.  DoCmd.Close _
  26.    Objecttype:=acReport, _
  27.    ObjectName:="Reject Tag Report", _
  28.    Save:=acSaveNo
  29. '(...)
This is a different format then I'm use to and having a hard time debugging it.
It's the named augment form of the function.
Instead of needing something like
Obj.Command first,,,,,Sixth
You can do something like
Obj.Command Augment1:=First, Augment6:=Sixth
Which I find more pleasing to the eye, often easier to follow down the road when it's a function I don't use often, and has the benefit that if you are missing a comma (or have too many) there are no unexpected errors that are hard to pin-down. :-)
May 30 '19 #5
DJRhino1175
221 128KB
Success, I had this part incorrect:
Expand|Select|Wrap|Line Numbers
  1. strReportName = Replace(Replace("REJECT TAG#|1 P/N: |2", "|1", Nz(Space(1) & [REJECT TAG NUMBER], "")), "|2", Nz([Part Number] & Space(1) & [Descriptive Reason], ""))
You cannot have "\", "/" or ":" in your file naming construct.
May 30 '19 #6
NeoPa
32,556 Expert Mod 16PB
@DJ.
Not sure what you hope to achieve with the part of your code that reads :
Expand|Select|Wrap|Line Numbers
  1. Nz([Part Number] & Space(1) & [Descriptive Reason], "")
The Space(1) will necessarily ensure that the first parameter passed to the Nz() function is not Null.

Also, using Space(1) is a complicated way of saying " " in VBA. The only reason I can imagine for that is if you worry someone will try to re-use your code and you don't want them to understand what you're doing.

If you want a string that resolves to an empty string ("") then you can take advantage of Null Propagation (Where's a good link when you want one?) by saying :
Expand|Select|Wrap|Line Numbers
  1. ([Part Number] + " ") & [Descriptive Reason] & ""
This ensures that an extra space is added before [Descriptive Reason] - unless [Part Number] is Null.
Expand|Select|Wrap|Line Numbers
  1. Null + "Any string" ==> Null
  2. Null & "Any string" ==> "Any string"
I've added the empty string at the end in case both Field values can be Null.
May 30 '19 #7
DJRhino1175
221 128KB
Space(1) is the number of spaces between Part Number and Descriptive Reason.
I did a google search on how to do a space between the to items and this is what I was told to do.
Jun 3 '19 #8
twinnyfo
3,653 Expert Mod 2GB
DJ - part of the logic behind this is very simple. Which is easier:

Expand|Select|Wrap|Line Numbers
  1. strTest = "This is" & Space(1) & "a test"
or
Expand|Select|Wrap|Line Numbers
  1. strTest = "This is" & " " & "a test"
The most useful purpose of the Space() function is to be able to use a variable in order to add an unknown number of spaces--you can have a variety of reasons for doing this.

However, if you know that there is always only one space needed, using " " is much easier.

You should find the site that told you to use Space(1) and tell them it's not the best idea.

However, you should really look closely at what NeoPa is advising concerning the string you are trying to create. Anything & " " will not be Null

I would think (I would hope so) that if the Part Number were Null--meaining you can't find a corresponding part number, that you would not send out an e-mail with a report on a particular part number. But, if there is ALWAYS a part number, then the string will NEVER be Null.

Just want you to think through what it is you are trying to do.

Hope this hepps!
Jun 3 '19 #9
DJRhino1175
221 128KB
I got you on the null part, was trying to figure what you guys ment. NZ() part, after looking closer Nz() doesn't even need to be there at all. Good catch. I will also change the Space(1) to "". For some reason I thought I did that and it did not work, but I'll give it another shot.
Jun 3 '19 #10
DJRhino1175
221 128KB
Ok, the reason it did not work for me is I did not add a "Space" between the quotes...and for some reason the NZ() needs to be there, as when I took then out the code quit working. So I put them back in and all is good.

Thanks
Jun 3 '19 #11
twinnyfo
3,653 Expert Mod 2GB
Yeah, you need a "space" in your quotes.

:-)
Jun 3 '19 #12
zmbd
5,501 Expert Mod 4TB
home > topics > microsoft access / vba > insights > what is null?
DJRhino1175, you might find this article to be helpful for your string... especially the conversation regarding the propagation of null thru the string.
Jun 3 '19 #13
NeoPa
32,556 Expert Mod 16PB
DJRhino:
Good catch. I will also change the Space(1) to "". For some reason I thought I did that and it did not work, but I'll give it another shot.
This is one of the reasons why we insist code is always included in the [CODE] tags (although such short strings are allowed exceptions) - See "" in ordinary text and exactly what's there is hard to see. "" on the other hand, shouts at you that you've missed out the space. Experience helps too, but let the rules help you. It's why we also recommend strongly that you never edit code in anything but a text editor (which typically use non-proportional fonts). So many problems are overlooked because people use the wrong tools. Word, for instance, great software though it truly is, is absolutely rubbish for editing code with.
Jun 3 '19 #14
NeoPa
32,556 Expert Mod 16PB
DJRhino:
for some reason the NZ() needs to be there, as when I took then out the code quit working. So I put them back in and all is good.
It may well be that the version you tried after removing the Nz() failed to work correctly but I can assure you that the reason wasn't down to removing the Nz(). Something else also changed as Nz() in that context would have no effect anyway.

That's not to say you need to go back and have another go if you don't want to bother. That's up to you. I just wanted to make sure you understood that the issue was not with the Nz().
Jun 3 '19 #15
DJRhino1175
221 128KB
I'm leaving the NZ() in there as it works with no issue as for what I need it to do. But I need to start a new thread for a different issue I'm having with coda associated with this.

Thanks for all your inputs as I'm learning a lot. I'm creating .txt documents to keep a lot of the special codes created for any future projects that may need them so I do not have to reinvent the wheel.
Jun 4 '19 #16
twinnyfo
3,653 Expert Mod 2GB
DJ,

Another tip--instead of saving as a text file, you can have a separate module (or even a separtae Database) that holds some frequently used Code. This way you can simply cut and paste into your new projects.

All of us here at Bytes are glad you are beginning to look at the bigger picture of DB design, and over time this will benefit you.

Looking forward to working with you on your future projects!
Jun 4 '19 #17
DJRhino1175
221 128KB
Thanks twinnyfo, never though of just making a DB to hold all of them. This will definitely be helpfully as I have accumulated several .txt docs.
Jun 4 '19 #18
NeoPa
32,556 Expert Mod 16PB
If this were Facebook I'd add 'Like's to both posts #16 & #17.

Good stuff in both and congratulations to DJ on handling his learning & progression so well.
Jun 4 '19 #19
zmbd
5,501 Expert Mod 4TB
Oh, yes... I have a database with modules named for the type of VBA (email, excel, outlook, encryption, etc....) contained. I used to have them in *.bas files however I have so many of them now that it was becoming a pain to search
Jun 4 '19 #20

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

Similar topics

2
by: Ryan Liu | last post by:
Hi, What is the best practise to sove this problem: property with defalut value will not be called in auto-generated code ? for example
1
by: jrshack | last post by:
I am needing a script that will send an auto-response email back to the email supplied in the registration html form. I have an html form mail that ask for information and an email address from the...
1
CougarMutt
by: CougarMutt | last post by:
Is this even possible? At 6am every morning to automatically run a report and email it using Groupwise? Chris
2
by: PerumalSamy | last post by:
HI I done a project in asp.net with VB coding and sql server 2005 as back end. Now i need to generate an auto email based on current date from one of the date field in my database table on...
1
by: PerumalSamy | last post by:
HI I done a project in asp.net with VB coding and sql server 2005 as back end. Now i need to generate an auto email everyday for current date based on the date field in my database table . ...
1
by: Ivan Ven Osdel | last post by:
Not really, I have just worked with them more. ----- Original Message ----- From: "Ali Servet Dönmez" <asd@pittle.org> To: python-list@python.org Sent: Wednesday, July 2, 2008 1:15:04 PM GMT...
2
by: phill86 | last post by:
Hi, I am filtering a report using the form filter with the following code DoCmd.OpenReport "report1", acViewReport, , Me.filter Which works fine untill I filter the form on one of the...
3
by: epifinygirl | last post by:
I currently have a report that summarizes my data for each client from the table (each page summarizes the information for each client). I have VBA code all set that can filter each client and print...
6
by: eneyardi | last post by:
I want to filter report to show only field you selected from a listbox in a form. For example I have query which field are ID, Ni, Fe, Mg and Sc The field ID is primary key. it has value of 1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.