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
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 !
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.
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!
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!
This is my offering at the end of today; - Option Compare Database
-
Option Explicit
-
-
Private Sub Test_Click()
-
-
Dim i As Integer
-
Dim db As Database
-
Dim rs As Recordset
-
' Dim ccrs As Recordset
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("tblcustomers")
-
-
'While Not rs.EOF
-
For i = 0 To rs.RecordCount - 25
-
Debug.Print rs.Fields("cuscode")
-
Debug.Print rs.Fields("email")
-
rs.MoveNext
-
Next i
-
'DoCmd.OpenReport "rptAccount", acViewReport, acFormatPDF, 'rs.Fields("cuscode")
-
'DoCmd.SendObject acSendReport, , acFormatPDF, rs!email, , , "Your monthly account", "Your account is attached", True
-
'DoCmd.OutputTo acOutputReport, "rptAccount", acFormatPDF, "C:pdfs\test.pdf", True
-
-
-
'DoCmd.SendObject acSendReport, , acFormatPDF, rs!email, , , stSubject, stEmailMessage, True, ""
-
'DoCmd.OutputTo acOutputReport, , acFormatPDF, myPath & stCaption & ".pdf", False, , , acExportQualityPrint
-
-
'Wend
-
'DoCmd.Close acReport, stReport, acSaveNo
-
-
rs.Close
-
Set rs = Nothing
-
db.Close
-
End Sub
-
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.
Forgot to ask what the gstr was for, I assume it stands for global string, why global?
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! - Public Function ErrorSub()
-
'Just useful and quick way to add Error Handling to Code
-
-
Debug.Print "On Error GoTo EH" & vbCrLf
-
Debug.Print " Exit Sub"
-
Debug.Print "EH:"
-
Debug.Print " MsgBox " & Chr(34) & _
-
"There was an error ....! " & Chr(34) & " & _"
-
Debug.Print " " & Chr(34) & _
-
"Please contact your Database Administrator." & _
-
Chr(34) & ", vbCritical, " & Chr(34) & "WARNING!" & Chr(34)
-
Debug.Print " Exit Sub"
-
-
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: - Option Compare Database
-
Option Explicit
-
-
'Remember this????
-
Public gstrReportFilter As String
-
-
Private Sub Test_Click()
-
On Error GoTo EH
-
Dim db As Database
-
Dim rs As Recordset
-
Dim strPath As String
-
Dim strReport As String
-
Dim strFilename As String
-
Dim strSubject As String
-
Dim strMessage As String
-
-
strPath = "C:\pdfs\"
-
strSubject = "Your monthly account"
-
strMessage = "Your account is attached"
-
-
Set db = CurrentDb
-
Set RS = db.OpenRecordset("tblcustomers")
-
'Save some typing and use "With rs"
-
With rs
-
'First, check to see if there are records
-
If Not .RecordCount = 0 Then
-
'Always move to the top!
-
.MoveFirst
-
Do While Not .EOF
-
'This is stuff you already had--I'm not really adding anything
-
Debug.Print !cuscode
-
Debug.Print !Email!
-
'Hey, you've got the cuscode, set the global variable
-
'gstrreportfilter = (you fill in the rest.....)
-
'Hey, you've got the cuscode, save the report
-
'strReport = (you fill in the rest.......)
-
strFilename = strPath & strReport
-
DoCmd.OutputTo acOutputReport, "rptAccount", _
-
acFormatPDF, strFilename
-
'At this point, you've got the PDF
-
'You've also got the e-mail address
-
'Now just send the report
-
DoCmd.SendObject acSendReport, _
-
"rptAccount", acFormatPDF, _
-
!Email, , , strSubject, strMessage, True
-
RS.MoveNext
-
Loop
-
End If
-
.Close
-
End With
-
db.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
Exit Sub
-
EH:
-
MsgBox "There was an error sending the reports! " & _
-
"Please contact your Database Administrator.", vbCritical, "WARNING!"
-
Exit Sub
-
End Sub
Like I said, you are at the 99% point. But, I hope you've learned a few things......
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.
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: - Exit Sub
-
EH:
-
MsgBox "There was an error sending the reports! " & _
-
"Please contact your Database Administrator.", vbCritical, "WARNING!"
-
Exit Sub
-
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!
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. - Option Compare Database
-
Option Explicit
-
-
Public gstrReportFilter As String
-
-
Private Sub Test_Click()
-
On Error GoTo trouble
-
-
Dim db As Database
-
Dim rs As Recordset
-
Dim strPath As String 'Path to store pDFs
-
Dim strReport As String 'Name of report
-
Dim strFilename As String 'Name of file to store
-
Dim strSubject As String 'Email subject Your monthly...
-
Dim strMessage As String 'Email message Your account is attached
-
-
strPath = "C:\pdfs\"
-
strSubject = "Your account"
-
strMessage = "Your monthly account is attached"
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("tblcustomers")
-
-
With rs
-
If Not .RecordCount = 0 Then
-
.MoveFirst
-
Do While Not .EOF
-
Debug.Print !Cuscode
-
Debug.Print !Email
-
'gstrReportFilter = "rptaccount" & !Cuscode
-
Debug.Print gstrReportFilter
-
strReport = "Account " & !Cuscode & ".pdf"
-
strFilename = strPath & strReport
-
DoCmd.OutputTo acOutputReport, "rptAccount", acFormatPDF, strFilename
-
rs.MoveNext
-
Loop
-
End If
-
.Close
-
End With
-
db.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
Exit Sub
-
-
trouble:
-
MsgBox "There was a problem handling the reports" & ", please contact your Dad", vbCritical, "WARNING!"
-
Exit Sub
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Konrad |
last post by:
Hi
I would like to know does
is available Cystal Reports customer designer for VS.NET 2003?
Thanks
Konrad
|
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
|
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...
|
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...
|
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
| |
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
|
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.
|
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...
|
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?
|
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.
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |