473,698 Members | 2,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Store and send reports by customer number Access 2016

62 New Member
I've looked through the site for this but nothing quite matches. I have an Access report for monthly accounts called rptAccount.
Each report is grouped by Customer code number, a 3 character number string (012) etc..
Is there a quick and easy (ish) way to send these accounts each month. They print separately but send as one big report. That is fine for archive. At the moment I have to go to Print Preview, save each report page as a pdf which requires about 6 clicks, then I have to go back to the archived pdfs and 'send' to email recipient. It's a bit long winded because there are usually 30+. Thanks for any help, macro if possible, not very conversant with VBA.
Mar 9 '18
58 4304
TrevorJ
62 New Member
Yes, that was it thanks. I started off with just the "rptAccount " but put the tblAccount in when it didn't work. Then when you said complete path and filename I didn't try it without the tblAccount part.
At least I have started !
Mar 14 '18 #11
twinnyfo
3,653 Recognized Expert Moderator Specialist
So, we are making progress!!

Let's take another look at what we want our code to do:
  • I need a list of Customer Codes
  • I need a list of e-mail addresses
  • I need to either a) save the reports to PDF first, then send them, or b) send those reports as PDFs (either way would work)
You have the basics of the last step down, although we will have to make some modifications so you can save each customer's report as an individual report that you can then send via-email.

The first two are what we must work on, now. I can only presume that your Customers are in a Table. And hopefully, the Customer ID and the contact e-mail are in the same table? (Please say "yes").

If you've got this customers table, what is your code going to look like to get your Cutomer IDs and E-mail addresses?

I will begin by simply asking how familiar you are with working with Recordsets?--because that is how we are going to approach this solution. Again, this is more of a training session, so we will work through it step-by-step. Other novices will benefit from this process, I believe.
Mar 14 '18 #12
TrevorJ
62 New Member
Yes, the tblCustomers does have the email address field. At the moment I know I need a recordset to loop through, to find each report one at a time. I think I could use If Then Else, or While and While End. In English it's 'Start through the list, if the field is not empty, get the address, send the email (referring to the string's declared like subject etc.. When that's done it's return to the start until there is no longer a record (EOF).I will need the error handler that you mentioned before.
At the moment I can print the reports individually because they are grouped by Customer code so I assume I will 'call' the records in the report and customer table by that field. After that I 'm back to research but flushed with success that with your help, I can at least save the whole report!
Mar 14 '18 #13
twinnyfo
3,653 Recognized Expert Moderator Specialist
Yes, you can save the entire report--just make sure gstrReportFilte r = "".

So, your tasks:
  • Create a Recordset on the Customers Table
  • Using the Customer ID, create a string for the Path\FileName of that Customer's Report
  • Set the Report filter based upon the Customer ID
  • Save the Report
  • Using the e-mail address, send the Report as an attachment
Give it your best shot--even if you have to just go step by step without sending anything to see that each step works individually.

I'm still here to hepp!
Mar 14 '18 #14
TrevorJ
62 New Member
This is my offering at the end of today;
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Test_Click()
  5.  
  6.     Dim i As Integer
  7.     Dim db As Database
  8.     Dim rs As Recordset
  9.    ' Dim ccrs As Recordset
  10.     Set db = CurrentDb
  11.     Set rs = db.OpenRecordset("tblcustomers")
  12.  
  13. 'While Not rs.EOF
  14. For i = 0 To rs.RecordCount - 25
  15. Debug.Print rs.Fields("cuscode")
  16. Debug.Print rs.Fields("email")
  17. rs.MoveNext
  18. Next i
  19.     'DoCmd.OpenReport "rptAccount", acViewReport, acFormatPDF, 'rs.Fields("cuscode")
  20.     'DoCmd.SendObject acSendReport, , acFormatPDF, rs!email, , , "Your monthly account", "Your account is attached", True
  21.     'DoCmd.OutputTo acOutputReport, "rptAccount", acFormatPDF, "C:pdfs\test.pdf", True
  22.  
  23.  
  24. 'DoCmd.SendObject acSendReport, , acFormatPDF, rs!email, , , stSubject, stEmailMessage, True, ""
  25. 'DoCmd.OutputTo acOutputReport, , acFormatPDF, myPath & stCaption & ".pdf", False, , , acExportQualityPrint
  26.  
  27. 'Wend
  28. 'DoCmd.Close acReport, stReport, acSaveNo
  29.  
  30.     rs.Close
  31.     Set rs = Nothing
  32.     db.Close
  33. End Sub
  34.  
I have put some extra bits in that I think I'll need next. I understand up to the line after next i. I commented out the While and While End lines because I got mixed up in an endless loop !
The dim ccrs was to make another recordset for the cuscode field for the report but I don't know if I need it or whether it can all be done with the recordset that I have. What I do have works and gives me the cuscode and email address for each of the customer records so I'm happy so far.
I did move on to the email part but it only attaches the whole report and doesn't fill in or send to the email addresses. Note that I don't have Outlook.
I don't know exactly where to put the error handler.
Regards,
Trevor.
Mar 15 '18 #15
TrevorJ
62 New Member
Forgot to ask what the gstr was for, I assume it stands for global string, why global?
Mar 15 '18 #16
twinnyfo
3,653 Recognized Expert Moderator Specialist
Trevor,

Excellent! You are making progress. You are about 99% there. No, really!!

First, ask/answer a few questions:

The dim ccrs was to make another recordset for the cuscode field for the report but I don't know if I need it or whether it can all be done with the recordset that I have.
You have everything you need, as is. You will soon see.

I did move on to the email part but it only attaches the whole report and doesn't fill in or send to the email addresses.
Thisis because you haven't incorporated that Global Variable....

Note that I don't have Outlook.
How will you send e-mail using Access without Outlook? Office is a pretty standard suite of apps--I thought it came standard with an e-mail application.

I don't know exactly where to put the error handler.
An error handler tells the code what to do if it encounters an error of any type. For example, if you are working with mathematical calculations and try to divide a number by 0, that will cause an error. Without error handling, the code just stops (and breaks), which can cause your DB to crash in extreme cases, but it most likely will cause your app to hang or reset all your variables and a massive rift will develop in the intergalactic cosmic continuum vortex matrix (well, maybe not that). It's just bad. However, if you put error handling in all your procedures, if there is an error, you can "catch it" before it breaks anything and then tell the DB what to do. You can catch particular types of errors, too. If you are working with file management and a file is locked or not found, you can choose to skip that file or just exit your procedure.

Typically, error handling is the first line of my procedures, so that if anything goes wrong anywhere along the way, it takes care of it. But you can add different types of error handling at different parts of your procedures (more than you need to know now).

Because I am very lazy and don't like to type code repeatedly, I've created a small procedure (which does not have Error handling, by the way--shame on me!). I place it in an empty module. Then, in the immediate window of my VB editor, I type "ErrorSub" and it gives me the quick text for error handling. I copy and paste into my procedures and I am good to go!

Expand|Select|Wrap|Line Numbers
  1. Public Function ErrorSub()
  2. 'Just useful and quick way to add Error Handling to Code
  3.  
  4.     Debug.Print "On Error GoTo EH" & vbCrLf
  5.     Debug.Print "    Exit Sub"
  6.     Debug.Print "EH:"
  7.     Debug.Print "    MsgBox " & Chr(34) & _
  8.         "There was an error ....!  " & Chr(34) & " & _"
  9.     Debug.Print "        " & Chr(34) & _
  10.         "Please contact your Database Administrator." & _
  11.         Chr(34) & ", vbCritical, " & Chr(34) & "WARNING!" & Chr(34)
  12.     Debug.Print "    Exit Sub"
  13.  
  14. End Function
Please feel free to steal this code freely.

Forgot to ask what the gstr was for, I assume it stands for global string, why global?
Yes. Global. So you can access it from anywhere in the DB environment.

=============== =============== ======

Now a couple minor critiques (but not necessarily "correction s") to your code.

Line 6: Dim i As Integer - no need for an integer counter. If you are using large recordsets, an integer will break after record 32,767. Additionally, it uses additional resources. You have the basic idea in your While statement (more to follow on that).

Lines 14-17: It "can" be very dangerous to begin working with recordsets without first verifying that there are definitely records in the set. Also, some think using the .EOF property is "good enough", and in 99% of the cases it is. However, there are cases in which it is not quite right. Also, using the .RecordCount property "may" or "may not" give you the number of records in the recordset. Again, for large batches, sometimes the DB will only load a portoin of the records, just to get things started. Then, it requires a .MoveLast, followed by a .RecordCount. However, .RecordCount guarantees that if it is 0, there are no records, anything else means there are at least some records--that becomes your validation.

Then, the best way to run through the records is While Not .EOF. This ensures you get all the records.

So, based on your excellent opening volley, here are some recommended changes, with some notes included for you:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. 'Remember this????
  5. Public gstrReportFilter As String
  6.  
  7. Private Sub Test_Click()
  8. On Error GoTo EH
  9.     Dim db          As Database
  10.     Dim rs          As Recordset
  11.     Dim strPath     As String
  12.     Dim strReport   As String
  13.     Dim strFilename As String
  14.     Dim strSubject  As String
  15.     Dim strMessage  As String
  16.  
  17.     strPath = "C:\pdfs\"
  18.     strSubject = "Your monthly account"
  19.     strMessage = "Your account is attached"
  20.  
  21.     Set db = CurrentDb
  22.     Set RS = db.OpenRecordset("tblcustomers")
  23.     'Save some typing and use "With rs"
  24.     With rs
  25.         'First, check to see if there are records
  26.         If Not .RecordCount = 0 Then
  27.             'Always move to the top!
  28.             .MoveFirst
  29.             Do While Not .EOF
  30.                 'This is stuff you already had--I'm not really adding anything
  31.                 Debug.Print !cuscode
  32.                 Debug.Print !Email!
  33.                 'Hey, you've got the cuscode, set the global variable
  34.                 'gstrreportfilter = (you fill in the rest.....)
  35.                 'Hey, you've got the cuscode, save the report
  36.                 'strReport = (you fill in the rest.......)
  37.                 strFilename = strPath & strReport
  38.                 DoCmd.OutputTo acOutputReport, "rptAccount", _
  39.                     acFormatPDF, strFilename
  40.                 'At this point, you've got the PDF
  41.                 'You've also got the e-mail address
  42.                 'Now just send the report
  43.                 DoCmd.SendObject acSendReport, _
  44.                     "rptAccount", acFormatPDF, _
  45.                     !Email, , , strSubject, strMessage, True
  46.                 RS.MoveNext
  47.             Loop
  48.         End If
  49.         .Close
  50.     End With
  51.     db.Close
  52.     Set rs = Nothing
  53.     Set db = Nothing
  54.  
  55.     Exit Sub
  56. EH:
  57.     MsgBox "There was an error sending the reports!  " & _
  58.         "Please contact your Database Administrator.", vbCritical, "WARNING!"
  59.     Exit Sub
  60. End Sub
Like I said, you are at the 99% point. But, I hope you've learned a few things......
Mar 15 '18 #17
TrevorJ
62 New Member
Thank you for your comments and code, I'm working on them now but it seems more like 20% to me !
Just a couple of quickies;
Do I put you EH code into mine under my Private Sub Test_Click() without your Function header and footer or do I put the whole thing above my Private Sub Test_Click() ?
I bought a standalone, no subscription version of Access 2016 so that's all I have, I've read somewhere that Access will use the default email app. which for these experiments is has. If I need to get Outlook, which version please?
Thanks.
Mar 16 '18 #18
twinnyfo
3,653 Recognized Expert Moderator Specialist
Do I put you EH code into mine under my Private Sub Test_Click() without your Function header and footer or do I put the whole thing above my Private Sub Test_Click() ?
See how my code has the error handling? Just like that. Please note the last six lines:

Expand|Select|Wrap|Line Numbers
  1.     Exit Sub
  2. EH:
  3.     MsgBox "There was an error sending the reports!  " & _
  4.         "Please contact your Database Administrator.", vbCritical, "WARNING!"
  5.     Exit Sub
  6. End Sub
You want your Code to exit when it gets done, prior to going to EH: Otherwise, your code will "say" you are getting an error, even though there is none.


I bought a standalone, no subscription version of Access 2016 so that's all I have, I've read somewhere that Access will use the default email app. which for these experiments is has. If I need to get Outlook, which version please?
I knew that Access would use your default e-mail--my concern was that you were using a browser version of G-Mail or something. You should be fine.

---------------

Your mission, should you choose to accept it, is to complete lines 34 and 36 above (they'll have to be uncommented in your code, as well). That's it, brother!

Let me know how it works!
Mar 16 '18 #19
TrevorJ
62 New Member
I'm sorry, I knew I was further away than you thought. I seem to have tried a million things over 2 days and got nowhere, except that the error message and warning bell work - hooray !
I haven't even started with the email part because there seems little point at this stage.
I think I've tried every combination of rpt account and cuscode possible so I don't now even know if that's what gsttrreportfilt er should be.
So I've left it just to save all the reports under the incrementing customer codes but of course without a filter it saves every report to each cuscode.
I know there might be a wend missing but I've never let it get that far.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public gstrReportFilter As String
  5.  
  6. Private Sub Test_Click()
  7. On Error GoTo trouble
  8.  
  9.     Dim db As Database
  10.     Dim rs As Recordset
  11.     Dim strPath     As String 'Path to store pDFs
  12.     Dim strReport   As String 'Name of report
  13.     Dim strFilename As String 'Name of file to store
  14.     Dim strSubject  As String 'Email subject Your monthly...
  15.     Dim strMessage  As String 'Email message Your account is attached
  16.  
  17.     strPath = "C:\pdfs\"
  18.     strSubject = "Your account"
  19.     strMessage = "Your monthly account is attached"
  20.  
  21.     Set db = CurrentDb
  22.     Set rs = db.OpenRecordset("tblcustomers")
  23.  
  24.     With rs
  25.         If Not .RecordCount = 0 Then
  26.             .MoveFirst
  27.             Do While Not .EOF
  28.                 Debug.Print !Cuscode
  29.                 Debug.Print !Email
  30.                 'gstrReportFilter = "rptaccount" & !Cuscode
  31.                 Debug.Print gstrReportFilter
  32.                 strReport = "Account " & !Cuscode & ".pdf"
  33.                 strFilename = strPath & strReport
  34.                 DoCmd.OutputTo acOutputReport, "rptAccount", acFormatPDF, strFilename
  35.                 rs.MoveNext
  36.             Loop
  37.         End If
  38.         .Close
  39.     End With
  40.     db.Close
  41.     Set rs = Nothing
  42.     Set db = Nothing
  43.  
  44.     Exit Sub
  45.  
  46. trouble:
  47.     MsgBox "There was a problem handling the reports" & ", please contact your Dad", vbCritical, "WARNING!"
  48.     Exit Sub
  49. End Sub
I totally understand if you want to just recommend I go away and study for a year, and then come back.
Regards Trevor.
I thought I knew what frustration was until I tried this.
Mar 18 '18 #20

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

Similar topics

0
871
by: Konrad | last post by:
Hi I would like to know does is available Cystal Reports customer designer for VS.NET 2003? Thanks Konrad
33
13335
by: Prasad | last post by:
Hi, Can anyone please tell me how to store a 13 digit number in C language ? Also what is the format specifier to be used to access this variable ? Thanks in advance, Prasad P
0
1360
by: shwethatj | last post by:
Can anyone please help me..... In Crystal Reports , how to access data using PULL method .. I have got a table in server explorer which has 3 got fields. But i dont know how to access it and generate a report. Actually i have first selected ASP.NET website with c# language , then i added a crystal report using add item in solution explorer . Later i had setup a new data connection in server explorer. The new connection has got a database...
28
2044
by: DanicaDear | last post by:
I have set up a form to contain customer info. From that form, I have a button "Show Order" that links it to a Order Number form. That order number form contains a subform showing order details (qty, items). When I am on the customer info form and click SHOW ORDER it only shows me the orders associated with that particular customer number. (That is good.) But when I want to enter a new order, how can I get it to automatically fill in the...
9
9688
by: shalskedar | last post by:
Access can store how many maximum number of records for a db.. Wanted to know what is the limitation of total number of records that Access db can store
2
15628
by: CD Tom | last post by:
I've installed Office 365 and when I bring up Access the back ground color is White, I've looked in the File, Account, but when I click on the Office Theme I only get two choices Colorful, White, no matter what I choose I only get the white. In Access 2007 I had the I had a choice of Blue Silver or Black. How do I get my Blue back in Access 2016 any ideas. Thanks
1
3576
by: CD Tom | last post by:
I'm thinking about updating from Access 2007 to Access 2016 but have run into a problem. I can't find any place to compile my accdb to an accde. I've installed the office 2016 and can bring up my database with out any problems, but where is the make accde? Thanks for any help.
2
2281
by: Silver993 | last post by:
I have created Access 2016 Database with custom Icon. I can’t figure out what I am doing wrong. Here is the problem: When I tested the database in my developing Computer, it opens with my custom Icon and everything is great as expected. But when I move it to another computer that does not have Access 2016 installed and run my Database in Run-time (Accde), everything work OK. But the only problem is that my Application custom Icon got replaced...
1
2495
by: Murphy | last post by:
I am gettig ready to upgrade from Windows XP / Office 2007 to Windows 10 / Office Professional 2016. Are Access 2007 databases compatible with or convertible to Access 2016?
9
8715
by: mshakeelattari | last post by:
Is there any way to send SMS from MS access to Mobile phone numbers? I have Samsung Galaxy J5. Does it have ability to do so? I use MS Access 2016 on My laptop with Windows 10. Please give any shortcut possible way. Thanking in anticipation.
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.