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

Printing Multiple Copies of a Report

Hi,

I'm having a nightmare trying to use a form as the basis of capturing the number of copies that a user requires of a report to be printed. I originally used the input box as a solution, but wanted something more elegent by using a txt control on a form instead.

The input box code is like this:


' Accepts user input in order that the number of copies required for an exam may be input


Dim Message, Title, Default, MyValue

Message = "How many Prints would you like?"

Title = "Print Reports"
Default = "1"
MyValue = InputBox(Message, Title, Default)

DoCmd.OpenReport "RptTest", acViewPreview

DoCmd.SelectObject acReport, "RptTest", True

DoCmd.PrintOut acPages, , , , MyValue

DoCmd.Close acReport, "RptTest"


My question is how can I get a form to do the same as the above. As you can guess I am a 'newbie' but am enjoying the steep learning curve!..

Kind Regards,

Dave
Sep 14 '07 #1
5 2468
Scott Price
1,384 Expert 1GB
The general syntax when referring to a control on a form is:
Expand|Select|Wrap|Line Numbers
  1. Forms!frmInput!txtInput
Where frmInput is the name of your form, and txtInput is the name of your text box.

By the way, a couple of things that are different with vba from a number of other programming languages... If you do not explicitly specify the data type of your variables vba assumes them to be of Variant type. This is handy when you don't know what type to declare them as, however, it slows things down because then vba has to programatically determine the best data type for the data that you enter. Also, declaring variables with one Dim statement can be tricky!
Expand|Select|Wrap|Line Numbers
  1. Dim MyValue, Default, MyEtc As String
As an example... This statement results in the first two variables being declared as Variants and the third declared as String, NOT all three being declared as String! VBA evaluates each variable, not each Dim line...

A second point I noticed is you are setting your Default variable to a string value or "1", not a number value of 1. You might run into troubles with this later in your code since acPages is presumably expecting a number format. However, if this code is actually working for you it means that Access has been smart enough to interpret what you want from what you are telling it :-)

So to sum this up: To use this code with a form reference you will need a few changes...
Expand|Select|Wrap|Line Numbers
  1. Dim Message, Title, Default, MyValue
  2.  
  3. Message = "How many Prints would you like?"
  4.  
  5. Title = "Print Reports"
  6. Default = "1"
  7. MyValue = InputBox(Message, Title, Default)
  8.  
  9. DoCmd.OpenReport "RptTest", acViewPreview
  10.  
  11. DoCmd.SelectObject acReport, "RptTest", True
  12.  
  13. DoCmd.PrintOut acPages, , , , MyValue
  14.  
  15. DoCmd.Close acReport, "RptTest"
This is what I would try first (not tested, but off top of head, so use with that in mind :-) Place this code in the declarations section of the code module behind your form.

Expand|Select|Wrap|Line Numbers
  1. Public Message As String, Title As String, Default As Integer
  2.  
  3. Message = "How many Prints would you like?"
  4.  
  5. Title = "Print Reports"
  6. Default = 1
Then in the form On_Open event:


Expand|Select|Wrap|Line Numbers
  1. Me!labelMessage.Caption = Message
  2. Me!labelTitle.Caption = Title
  3. Me!textPrintouts = Default
Then in the AfterUpdate event of your text box, or alternatively in the Click event of a command button:
Expand|Select|Wrap|Line Numbers
  1. Default = Me!textPrintouts
  2. DoCmd.OpenReport "RptTest", acViewPreview
  3.  
  4. DoCmd.SelectObject acReport, "RptTest", True
  5.  
  6. DoCmd.PrintOut acPages, , , , Default
  7.  
  8. DoCmd.Close acReport, "RptTest"
Obviously, on this form you will need two labels and one unbound text box named the same as I have named them in the code.

Give this a try!

Regards,
Scott
Sep 14 '07 #2
The general syntax when referring to a control on a form is:
Expand|Select|Wrap|Line Numbers
  1. Forms!frmInput!txtInput
Where frmInput is the name of your form, and txtInput is the name of your text box.

By the way, a couple of things that are different with vba from a number of other programming languages... If you do not explicitly specify the data type of your variables vba assumes them to be of Variant type. This is handy when you don't know what type to declare them as, however, it slows things down because then vba has to programatically determine the best data type for the data that you enter. Also, declaring variables with one Dim statement can be tricky!
Expand|Select|Wrap|Line Numbers
  1. Dim MyValue, Default, MyEtc As String
As an example... This statement results in the first two variables being declared as Variants and the third declared as String, NOT all three being declared as String! VBA evaluates each variable, not each Dim line...

A second point I noticed is you are setting your Default variable to a string value or "1", not a number value of 1. You might run into troubles with this later in your code since acPages is presumably expecting a number format. However, if this code is actually working for you it means that Access has been smart enough to interpret what you want from what you are telling it :-)

So to sum this up: To use this code with a form reference you will need a few changes...
Expand|Select|Wrap|Line Numbers
  1. Dim Message, Title, Default, MyValue
  2.  
  3. Message = "How many Prints would you like?"
  4.  
  5. Title = "Print Reports"
  6. Default = "1"
  7. MyValue = InputBox(Message, Title, Default)
  8.  
  9. DoCmd.OpenReport "RptTest", acViewPreview
  10.  
  11. DoCmd.SelectObject acReport, "RptTest", True
  12.  
  13. DoCmd.PrintOut acPages, , , , MyValue
  14.  
  15. DoCmd.Close acReport, "RptTest"
This is what I would try first (not tested, but off top of head, so use with that in mind :-) Place this code in the declarations section of the code module behind your form.

Expand|Select|Wrap|Line Numbers
  1. Public Message As String, Title As String, Default As Integer
  2.  
  3. Message = "How many Prints would you like?"
  4.  
  5. Title = "Print Reports"
  6. Default = 1
Then in the form On_Open event:


Expand|Select|Wrap|Line Numbers
  1. Me!labelMessage.Caption = Message
  2. Me!labelTitle.Caption = Title
  3. Me!textPrintouts = Default
Then in the AfterUpdate event of your text box, or alternatively in the Click event of a command button:
Expand|Select|Wrap|Line Numbers
  1. Default = Me!textPrintouts
  2. DoCmd.OpenReport "RptTest", acViewPreview
  3.  
  4. DoCmd.SelectObject acReport, "RptTest", True
  5.  
  6. DoCmd.PrintOut acPages, , , , Default
  7.  
  8. DoCmd.Close acReport, "RptTest"
Obviously, on this form you will need two labels and one unbound text box named the same as I have named them in the code.

Give this a try!

Regards,
Scott

Hi Scott,

I'll get on this right away!. I really do appreciate the time you have extended to me in replying to this. Like I said i'm having a really great time getting into all this and love the directions it can take you and, equally, the frustrations :-).

I'll post the results from this.

Kindest regards

Dave
Sep 16 '07 #3
Hi Scott,

I have tried the suggested code and all looks ok to me apart from the usage of two lables with the unbound control. I can't get my head round why the second label is used.

1. Me!labelMessage.Caption = Message
2. Me!labelTitle.Caption = Title
3. Me!textPrintouts = Default

I also get the following error: "Invalid attribute in sub or Function"
with the statement provided for the declarations of the form:

Public Message As String, Title As String, Default As Integer

Message = "How many Prints would you like?"

Title = "Print Reports"
Default = 1

The "Public" line entry being highlighted asa the problem.


As i said before I greatly appreciate this assistance and it has given me alot to think about in other areas as well.

Kindest regards,

Dave
Sep 17 '07 #4
Scott Price
1,384 Expert 1GB
The Public declaration needs to take place above any of the Subs or Functions in your form module. As you look at the vba editor window with this form module selected at the very top you will see Option Compare Database. There should also be Option Explicit on the very next line below. IF there isn't, then type it in. This tells vba to require you to explicitly declare each and every variable. (otherwise you can make a spelling mistake down in your code somewhere and never find out about it since vba will accept it as a new variable and assign it a variant data type :-)

On the lines just below these two (but above any subs or functions in the form module) you will name these public variables. As for why two labels? Really not need, unless you wanted to assign the form caption separately, or want to place the two labels in different locations on the report.

And I forgot, but you will have to assign the text to these labels further down in the form module. The Public declaration goes at the top like I mentioned, but assigning text to them goes further down.

Regards,
Scott
Sep 17 '07 #5
Thanks Scott!, Many thanks for getting back so quick. I'll attack this when I'm at home this evening,

Dave
Sep 18 '07 #6

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

Similar topics

0
by: nikolaos | last post by:
Hi all!! i am relatively new to this forum. i am working on a big project in athens greece. it is all about office and vba. basically it is an automated testing software that tests whetther...
1
by: Dreamtime | last post by:
Hi I am using Visual Studio 2005 and the bundled Crystal Reports (previously I used .net 2003 and bundled Crystal Reports for 2 years - same issues!) I have a report which is displayed in the...
6
by: MJ | last post by:
Is it possible to print varying numbers of labels from Access?
4
by: bmdavis | last post by:
Hello, Currently I have a form that I want to print, 5 times for each AUTONUMBER I specify (i.e. print #654 5 times). I have created 5 reports, each with a different header/footer according to my...
11
alpnz
by: alpnz | last post by:
Hi, I am sure someone has managed this one before. I have a report, which I call from a button on a form, which invokes the printing of 4 copies of a report. I would like to have a box on the...
4
ADezii
by: ADezii | last post by:
Recently, there seems to be several questions specifically related to Printers and changing Printing characteristics for Forms and Reports. For this reason alone, I decided to dedicate this week's...
3
by: franc sutherland | last post by:
Hi, Is there a way to make a report print out more than once using the VBA code behind a button, without having to put the code in multiple times? I want to print out the same report four...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
1
by: ccmanc68 | last post by:
I would like to print two reports a once. The first report is a sign sheet; the second is an order form. The sign sheet contains the same information as the order form but has a different layout. ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.