Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Printer.Print does not work in Access VB

Posted on 2004-03-24
11
Medium Priority
?
689 Views
Last Modified: 2008-01-09
I am trying to print to the printer with VB in Access 2002. Have read that the following command will work...

printer.print "test"
printer.EndDoc

When I try this I get the following error...

"Object does not support this property or method"

The error occurs when I try to run the code.

I am thinking that a reference should be checked off under "tools->references" but I can't find anything that works. Also, I will need to be able to turn bold on and off so if you know how, that would be nice too.
0
Comment
Question by:xbww
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 10674411
You use
Docmd.Printout

The following example prints two collated copies of the first four pages of the active form or datasheet:

DoCmd.PrintOut acPages, 1, 4, , 2
0
 

Author Comment

by:xbww
ID: 10674442
I need to print directly to the printer without using a form or datasheet. I dont think DoCmd can do that..?
0
 
LVL 54

Expert Comment

by:nico5038
ID: 10674969
Fir this you can use:

Open "LPT1" For Output As #1    ' Open file for output.
Print #1, "This is a test"    ' Print text to file.
Print #1,    ' Print blank line to file.
Print #1, "Zone 1"; Tab ; "Zone 2"    ' Print in two print zones.
Print #1, "Hello" ; " " ; "World"    ' Separate strings with space.
Print #1, Spc(5) ; "5 leading spaces "    ' Print five leading spaces.
Print #1, Tab(10) ; "Hello"    ' Print word at column 10.
Close #1

Nic;o)
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:xbww
ID: 10675023
Thanks nico but I already print to file and it works well in many situations. But, in this situation I need to make some text "bold". I realize that I could embed PCL commands within the file but that wont work either. The bold text is random in the file. I am printing thousands of pages. I need to print directly to the printer so that I can turn bold on and off when required. If I can print directly to the printer, then I can complete the entire project in about 15 minutes. Now u see my motivation :)

The question was asked (and answered) on the forum regarding printing to the printer object directly. It has been indicated before that this is very easy to do. For some reason my system does not let me print to the printer object. This is the main problem I am trying to overcome.

Why can I NOT use the following commands?

printer.print "test"
printer.EndDoc

Why am I getting the following error?

"Object does not support this property or method"


0
 
LVL 1

Expert Comment

by:ajysen
ID: 10675070
first check whether the default printer is installed or not. The code u r using is perfect.
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 1050 total points
ID: 10675753
I dont think its possible to use the Printer object in VBA
It certainly exists for VB but I dont believe you can use it in VBA

What I suggest is you create a file then print it using ShellExecute

If you can create a file using your bold text etc then fine, otherwise have you thought of creating it as html file, add in your bold using tags ie. <b>BOLD TEXT</b>
dont know how familiar you are with html but its a breeze for simple text

then print your html file using ShellExecute

e.g. creating html file

<HTML>
<HEAD>
<TITLE>Sample Page</TiTLE>
</HEAD>

<BODY>

<H1>This is header one</H1>
<H2>This is header one</H2>
<B>Bold text</B>
<I>Italics</I>

<BR> 'new line (by the way comments in html are <!--- some text --->
<P>   ' new paragraph


then end with

</BODY>
</HTML>




0
 

Author Comment

by:xbww
ID: 10684496
Thanks rock, it looks like the answer to my question is the same as my question.  Print.print does NOT work in VBA. I am using Access XP 2002.

So, the question now is...what is the best way to format output for the printer WITHOUT using controls on a report or form in MS Access.  The suggestion to write an HTML file is certainly valid, but after a little more diggin I think the better option for me is to print to a blank report object (almost just like printing to the printer itself). Here is some code I found...

Dim rpt As Report
    Dim strMessage As String
    Dim intHorSize As Integer, intVerSize As Integer

    Set rpt = Me
   
    strMessage = "DisplayMessage"
    With rpt
        'Set scale to pixels, and set FontName and
        'FontSize properties.
'        .ScaleMode = 3
        .FontName = "Courier New"
        .FontSize = 24
    End With
    ' Horizontal width.
    intHorSize = rpt.TextWidth(strMessage)
    ' Vertical height.
    intVerSize = rpt.TextHeight(strMessage)
    ' Calculate location of text to be displayed.
    rpt.CurrentX = (rpt.ScaleWidth / 2) - (intHorSize / 2)
    rpt.CurrentY = (rpt.ScaleHeight / 2) - (intVerSize / 2)
    ' Print text on Report object.
    For x = 1 To 10
        rpt.Print strMessage & Str$(x)
        rpt.CurrentX = (rpt.ScaleWidth / 2) - (intHorSize / 2)
    Next x

Just place the code in the format event of the detail area of a report. The report is not bound to any data.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 10685406
cool, as long as you get your desired result,
your code looks quite straightforward as well,
0
 

Expert Comment

by:hokiewalrus
ID: 10833499
xbww - I tried the same code you posted there, but I keep getting a type mismatch error at runtime for:

Set rpt = Me

Am I missing something really easy or stupid here?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 10833638
rpt has been declared as a report

where r u running this code?

in a form, module or report
0
 

Author Comment

by:xbww
ID: 10836991
Code is run in a report.  

Specifically...place the code in the 'on format' event of the detail area of a report. The report is not bound to any data.

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question