ACC: Print Text to Default Printer with Access Basic (1.x/2.0)

Last reviewed: May 8, 1997
Article ID: Q105662
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

In Microsoft Access, there is no standard way of sending text to the default printer without printing a form or report. This article demonstrates a method of using Windows API functions to send text to the printer.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0. This article also assumes that you are familiar with programming in the Windows environment. For more information on programming in the Windows environment using API functions, please refer to the Microsoft Windows Software Development Kit manuals.

MORE INFORMATION

Unlike Microsoft Visual Basic, Microsoft Access does not have a printer object for sending text to the printer. The following example demonstrates how to use Windows API functions to send text to the printer.

Notes

  • This article does not describe the API function arguments. For details on the API functions, please refer to the Microsoft Windows Software Development Kit manuals.
  • You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code. You should also alias any API function declarations. For more information on using aliases, search for "alias" then "declare statement" using the Microsoft Access Help menu.
  • In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.

          '-------------------------------------------------------------------
          'Declarations Section
          '-------------------------------------------------------------------
    
          Option Compare Database   'Use database order for string comparisons
          Option Explicit
    
          Type DOCINFO
             cbSize As Integer
             lpszDocname As Long
             lpszOutPut As Long
          End Type
    
          Declare Function GetProfileString% Lib "Kernel" (ByVal lpAppName$, _
                           ByVal lpkeyname$, ByVal lpDefault$, _
                           ByVal lpReturnedString$, ByVal nsize%)
          Declare Function CreateDC% Lib "GDI" (ByVal lpDriverName$, _
                           ByVal lpDeviceName$, ByVal lpOutput$, _
                           lpInitData As Any)
          Declare Function DeleteDC% Lib "GDI" (ByVal hDC%)
          Declare Function TextOut% Lib "GDI" (ByVal hDC%, ByVal X%, ByVal _
                           Y%, ByVal lpString$, ByVal nCount%)
          Declare Function Mylstrcpy& Lib "Kernel" Alias "lstrcpy" ( _
                           ByVal lpString1 As Any, ByVal lpString2 As Any)
          Declare Function StartDoc% Lib "GDI" (ByVal hDC%, lpdi As DOCINFO)
          Declare Function StartPage% Lib "GDI" (ByVal hDC%)
          Declare Function EndPage% Lib "GDI" (ByVal hDC%)
          Declare Function EndDocAPI% Lib "GDI" Alias "EndDoc" (ByVal hDC%)
          Declare Function Rectangle% Lib "GDI" (ByVal hDC%, ByVal X1%, _
                           ByVal Y1%, ByVal X2%, ByVal Y2%)
    
          '-------------------------------------------------------------------
          'Start of Function
          '-------------------------------------------------------------------
    
          Function Printer ()
             Dim lpReturnedString$
             Dim MyDoc As DOCINFO
             Dim MyDocumentname$
             Dim nPrinter, nDriver, nDevice
             Dim szDevice, szDriver, szOutPut
             Dim hDC%, X%, MyString$
    
    
             MyDocumentname$ = "My Document"
    
             '------------------------------------------
             ' Retrieve the currently selected printer as
             ' establish with the Control panel.
             ' Sample string as returned by lpReturnedString$:
             '
             '    HP LaserJet IIISi PostScript,pscript,LPT1:
             '------------------------------------------
             lpReturnedString$ = Space$(128)
             nPrinter = GetProfileString("windows", "device", ",,,", _
                        lpReturnedString$, Len(lpReturnedString$))
    
             '-----------------------------------------
             ' Parse the string of its three components
             '-----------------------------------------
             nDevice = InStr(lpReturnedString$, ",")
             nDriver = InStr(nDevice + 2, lpReturnedString$, ",")
             szDevice = Mid$(lpReturnedString$, 1, nDevice - 1)
             szDriver = Mid$(lpReturnedString$, nDevice + 1, nDriver - _
                        nDevice - 1)
             szOutPut = Mid$(lpReturnedString$, nDriver + 1)
    
             '------------------------------------------
             ' Create the DOCINFO structure for StartDoc()
             '  - lpszDocname is name displayed in PRINTMAN
             '  - lpszOutPut is not used and set to NULL
             '------------------------------------------
             MyDoc.cbSize = Len(MyDoc)
             MyDoc.lpszDocname = Mylstrcpy(MyDocumentname$, MyDocumentname$)
             MyDoc.lpszOutPut = 0&
    
             '------------------------------------------
             ' Create the device context
             '------------------------------------------
             hDC% = CreateDC(szDriver, szDevice, szOutPut, 0&)
    
             X% = StartDoc(hDC%, MyDoc)
                X% = StartPage(hDC%)
    
             '------------------------------------------
             ' Rectangle arguments are X, Y, cX, cY
             '------------------------------------------
             X% = Rectangle(hDC%, 10, 10, 1000, 150)
             MyString$ = "Derek and Robyn are brother and sister!"
    
             '------------------------------------------
             ' Second and third arguments are the X and Y
             ' coordinates on paper.
             '------------------------------------------
             X% = TextOut(hDC%, 30, 20, MyString$, Len(MyString$))
    
             X% = EndPage(hDC%)
             X% = EndDocAPI(hDC%)
    
             '------------------------------------------
             ' Release the device context when done
             '------------------------------------------
             hDC% = DeleteDC(hDC%)
    
          End Function
    
          '-------------------------------------------------------------------
          'End of Function
          '-------------------------------------------------------------------
    
    

REFERENCES

Microsoft Windows Software Development Kit "Guide to Programming," version 3.1, page 254-255

Microsoft Windows Software Development Kit "Programmer's Reference Volume 2: Functions," version 3.1


Keywords : kbprint PtrOthr
Version : 1.0 1.1 2.0
Platform : WINDOWS
Hardware : X86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.