473,568 Members | 2,850 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 #1
58 4283
twinnyfo
3,653 Recognized Expert Moderator Specialist
TrevorJ,

Welcome to Bytes!

What you are asking is very simple. There are two parts: the first part is to simply filter the report by the Customer Code; the second part is to export each of those filtered reports as a PDF.

All of this is readily available using VBA.

Have you made any attempts at doing this yourself? Please post your attempted code.
Mar 11 '18 #2
TrevorJ
62 New Member
Many thanks for you welcome and taking the time to look at my question. As I said I'm not very good with VBA although I am doing an online course. I have found some snippets on the forums and started to modify them but I'm not sure where to put the recordset, how to reference the email addresses in my tblCustomers.em ail and so on. This is what I've got so far;

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command#_Click()
  2. DoCmd.OpenReport "rptAccount"
  3. Dim stReport As String
  4. Dim stSubject As String
  5. Dim stEmailMessage As String
  6. Dim stCaption As String   'not sure what caption this is
  7. Dim myPath As String
  8.  
  9. stEmailMessage = "Your account from LPS for this month is attached."
  10. stSubject = "Monthly account"
  11. stReport = "tblAccount.rptAccount"
  12.  
  13. stCaption = "Send Account")
  14. myPath = "C:\DocumentsLaura_Access\LPS\Access Database\"
  15.  
  16. DoCmd.SendObject acSendReport, stReport, acFormatPDF, , , , stSubject, stEmailMessage, True, ""
  17. DoCmd.OutputTo acOutputReport, stReport, acFormatPDF, myPath & stCaption & ".pdf", False, , ,
  18. 'this would be a different path to that defined above, i.e. not in 'the database
  19. DoCmd.CloseReport

So not sure where to put the part that loops through the rptAccount.Cusc ode until it finds no more records. Cuscode by the way is a 3 letter character field in tblCustomers, tblAccount and rptAccount.
Regards,
Trevor.
Mar 12 '18 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
Trevor,

Thanks for the additional information.

First, one of the requirements on this forum is to use code tags whenever you post code to the site. All you have to do is simply highlight your code and click the [CODE/] button in your editor. I've done that for your code above.

Second, I want to begin by saying that I would rather teach you proper (or better) ways to do things, from the perspective that if I can teach you how to do something well (and you understand what you are doing), you are better off in the long run. So, I will make some comments and recommendations about your code which may not particularly answer your question, but may help you understand things better.

You say you are a novice in VBA--we've all been novices at one point, but we all keep learning! On to your code!

1. When posting procedures, it is helpful to us that you post the entire procedure from "Private Sub XXXX()" through the end "End Sub". You may feel free to delete irrelevant code, but this lets us know you have an initial procedure built that we are trying to troubleshoot for you.

2. I want to encourage you that your procedure Command#_Click( ) demonstrates that you are at least thinking about what needs to be done. I would recommend, however, that the for the naming convention of the controls on your forms (the Command Button Command#, in this case) refrain from using special characters, if at all possible. Also, that you have a standard "naming convention" for each type of object you name. For example a Command Button would be named "cmd[Description]", Text boxes could be named "txt[Description]". In your particular case, your Command Button could be named "cmdSendReports ". I don't endorse any particular naming convention, only that you have one--and stick with it. This is important when it comes to the default naming convention that MS Access uses for your field controls, as the Field CustomerName from a Table, when added to a Form as a Text Box control will have the name "CustomerNa me." Then, when you refer to "CustomerNa me" in your VBA behind that form, technically speaking, you are being ambiguous, because the VBA doesn't know whether Me.CustomerName means the Control named "CustomerNa me" or the underlying Field named "CustomerNa me." This does not always (or even usually) cause problems, but it can. Again, this is stuff I've learned over the years and pass along to you.

3. When building your code, think through what you want to do. Again, it looks like you've tried to do that. Here is how I would approach your particular issue:
  • 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)

So, in my first post, I recommended that you filter your reports, which is a built in feature of Reports. However, as you play around with the OutputTo and SendObject methods in Access, you will see that filtering at runtime is not an option here. If you were simply opening a Report (using the OpenReport method) this would be available. So, how do you do that? I will show you a little trick that you can use across your projects.

First, you need to have a Filter available to your Report based upon the Customer Code. This Filter will be a text string in the form of CustomerCode = 012. It is unclear from your original post if your Customer Codes are true number (as they have leading zeroes) or text strings. If numeric, the above example will work. If text, it will look like this: CustomerCode = '012'. Formatting and syntax is everything!

So, why not declare a variable that can be accessed from anywhere in the DB? to do this, you add a statement declaring this variable right under your top lines of the VBA module for your Form:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public gstrReportFilter As String
This tells the DB that there is a variable that will be available all the time (the Public keyword), named gstrFilter (g, designating "global"; str, designating to you that it is a text string; ReportFilter, telling you what the string is for; and As String, telling the DB what kind of values are stored in this variable. Make sense so far?

The only thing you will have to change is adding a small procedure to your Report like such:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_Open(Cancel As Integer)
  2. On Error GoTo EH
  3.  
  4.     If Len(gstrReportFilter) <> 0 Then
  5.         With Me
  6.             .Filter = gstrReportFilter
  7.             .FilterOn = True
  8.         End With
  9.     End If
  10.  
  11.     Exit Sub
  12. EH:
  13.     MsgBox "There was an error initializing the Report!  " & _
  14.         "Please contact your Database Administrator.", vbCritical, "WARNING!"
  15.     Exit Sub
  16. End Sub
This code tells the DB that whenever this report is opened, it will look for a filter. If the filter contains information, then filter the report, otherwise, open it with all records.

Now, you may be a bit disappointed here, but I am not going to give you the anser to this, but I will guide you through working toward a solution--again, so that you understand what you are doing. Let's give you a skeleton of your code that you will use.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSendReports_Click()
  2. On Error GoTo EH
  3.     Dim stReport        As String
  4.     Dim stSubject       As String
  5.     Dim stEmailMessage  As String
  6.  
  7.     'Set the Filter to empty to start out
  8.     gstrReportFilter = ""
  9.  
  10.     'Set the Report Name
  11.     stReport = "tblAccount.rptAccount"
  12.     stEmailMessage = "Your account from LPS for this month is attached."
  13.     stSubject = "Monthly account"
  14.  
  15.     'Get a list of Customer Codes and e-mail Addresses
  16.  
  17.     'Loop through this list, one at a time
  18.  
  19.         gstrReportFilter = "CustomerCode = " & 'Your Customer Code
  20.         DoCmd.SendObject acSendReport, stReport, _
  21.             acFormatPDF, "You need e-mail addresses", , , _
  22.             stSubject, stEmailMessage, True
  23.  
  24.     'Go to the next record
  25.  
  26.     'Reset your filter so you can open the Report normally
  27.     gstrReportFilter = ""
  28.  
  29.     Exit Sub
  30. EH:
  31.     MsgBox "There was an error sending the Reports!  " & _
  32.         "Please contact your Database Administrator.", vbCritical, "WARNING!"
  33.     Exit Sub
  34. End Sub
Again, I want to point out that little in the code you provided has changed. I added erorr handling (which is a habit all good DBAs should get accustomed to). At this point I did not "solve" your problem, and that is intentional.

Your next step is to work through "getting alist of customer codes and e-mail addresses". My guidance to you is to create a recordset based upon the table that contains that information. Give it your best shot, test it, and let me know what kinds of errors (if any) you receive. I'll be glad to help troubleshoot with you.

This is much longer than you (or I expected), but I remember being in your shoes wishing I had someone to show me how to do things, and not just what to do.

Hope this hepps!
Mar 12 '18 #4
TrevorJ
62 New Member
Hello twinnyfo,
Thanks for your answer. I welcome your approach so long as you're prepared to tolorate a slow learner.
I get it re. the code window.
When I post my next offering I will include the whole procedure, I also accept your recommendations about naming. I did actually mean Command(whateve r)_Click() but I'll be more precise. Working through your post, I don't exactly know what I should be naming items in different places, so can you explain if I had a table field called FirstName in my Customer table, I would refer to it as tblCustomers.Fi rstName correct? Would I use square brackets in VBA, and with both parts?
When I wanted to place a control on a report I have used the same name for several places where I wanted that value, is that o.k?
Moving on, I think that it would be better if we start with creating and saving the reports as PDFs first. It may well be that they will be checked in this form and sent later. I undertand the logic in the list though.
When you say about a global filter, I understand what you've written about what the filter says, but not why we need a filter, or is it to later filter out each record as we go through?
The customer codes are short text and I understand that they must be referred to in double quotes. Regarding your tip for a Public declaration of the strReportFilter , I can see that. Why do we put (Cancel As Integer) after Private Sub Report_Open? So everything makes sense so far, except for the above. Can I leave it there for your comments, and I'll move on in the meantime?
Thank you again,
Trevor.
Mar 12 '18 #5
twinnyfo
3,653 Recognized Expert Moderator Specialist
I'll try to answer your questions in order, as best as I can:

Working through your post, I don't exactly know what I should be naming items in different places, so can you explain if I had a table field called FirstName in my Customer table, I would refer to it as tblCustomers.Fi rstName correct?
tblCustomers is a standard naming convention. "FirstName" is a good field name, but "Name" is not a good field name, because it is a reserved word. When you build a Query in the Query Builder, it always refers to fields explicitly, including the Table Name, thus tblCustomers.Fi rstName is what you will see in queries. When building queries in VBA, it's not a bad habit to get into, but if there is only one table, then the following will work perfectly fine:

Expand|Select|Wrap|Line Numbers
  1. SELECT FirstName FROM tblCustomers;
Would I use square brackets in VBA, and with both parts?
Square brackets are required if there are spaces in the names of your tables/fields. Thus, brackets required for [Customers Table].[First Name], but not tblCustomers.Fi rstName.

When I wanted to place a control on a report I have used the same name for several places where I wanted that value, is that o.k?
You should be unable to have duplicate controls with the same name. But, you could have multiple controls with the same Record Source, but named differently. For example, you want the Customer ID on all four corners of your report. You could have four Text Boxes with the following names: txtCustIDTopRig ht, txtCustIDTopLef t, txtCustIDBotLef t, txtCustIDBotRig ht. Make sense?

Moving on, I think that it would be better if we start with creating and saving the reports as PDFs first. It may well be that they will be checked in this form and sent later. I undertand the logic in the list though.
No problem. The general outline for your procedure will stay the same.

When you say about a global filter, I understand what you've written about what the filter says, but not why we need a filter, or is it to later filter out each record as we go through?
A "filter" shows only particular records on a table, form or report. When you view your master report, you are seeing all your customer IDs. If we filter by one, particular Customer ID, we only see the information pertinent to that customer. Then, if we save it as a PDF, that customer's report can be mailed. This is why understanding filters is important in the long run. In your case, if we cycle through the Customer IDs, save each report with a unique name, and then send it as an attachment, we save your monthly efforts of repetition.

Why do we put (Cancel As Integer) after Private Sub Report_Open?
When you open a report, you have the option to "Cancel" if there is no data and it will close automatically. This is built into your Report via MS Access.

So everything makes sense so far, except for the above. Can I leave it there for your comments, and I'll move on in the meantime?
Yes. Standing by for further assistance.

Hope all this made sense.

BTW, I am exercising a bit of leeway with this thread. Typically, we don't allow multiple questions on a single thread, but I am hoping that due to the nature of this training opportunity, that your question will be viewed by other novices who will also benefit from these explanations. Other Mods are free to chime in and slap my knuckles with a ruler if I need to cease and desist.

:-)
Mar 12 '18 #6
TrevorJ
62 New Member
twinnyfo
Hello again,
I thought I would try to make a start with something (I thought) was simple and came unstuck straight away. I was just starting step 1 - saving the report. I know it will save the whole report in 1 at the moment but it's no good going on if I can't get this to work;
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Command0_Click()
  5. DoCmd.OutputTo acReport, "tblAccount.rptAccount", acFormatPDF, "C:\PDFs", True
  6. End Sub
  7.  
The error is 'can't find '|1'
I did a long internet repair on access last night and it's still the same now. The debug highlights the \ key after the C: as though I was holding down shift to get the vertical bar ! The folder C:\PDFs does exist.
Trevor.
Mar 14 '18 #7
twinnyfo
3,653 Recognized Expert Moderator Specialist
You need a complete Path\Filename, not just a path.

Also, rememeber that you will want to save each file so that it is unique to each customer (i.e., "Cust ID 123 Report.pdf").
Mar 14 '18 #8
TrevorJ
62 New Member
Still the same!

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Command0_Click()
  5. DoCmd.OutputTo acReport, "tblAccount.rptAccount", acFormatPDF, "C:\PDFs\cust001.pdf", True
  6. End Sub
  7.  
  8.  
It has to be something wrong with the path though because if I leave it blank, the browse for saving comes up, but whatever filename I use (with or without the .pdf I still get the same error message I have also tried it with quotes within quotes for the filename.
Mar 14 '18 #9
twinnyfo
3,653 Recognized Expert Moderator Specialist
Is "tblAccount.rpt Account" the name of the report? Or is it simply"rptAccou nt"?
Mar 14 '18 #10

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

Similar topics

0
868
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
13308
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
1356
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...
28
2033
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. ...
9
9686
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
15592
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...
1
3568
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
2274
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...
1
2481
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
8709
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
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
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. ...
0
8117
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...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
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...
0
3651
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...
1
2101
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
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.