Microsoft KB Archive/105662

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


Article ID: 105662

Article Last Modified on 5/6/2003



APPLIES TO

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition



This article was previously published under Q105662

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: kbhowto kbprint KB105662