Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the Current Default Printer

Status
Not open for further replies.

Dryseals

Technical User
Oct 17, 2002
38
US
OK, pulling my hair out here. I have a db with a field that contains a 9 digit number that I want to send in bar code to a special printer on LPT1. I have everything I need, fonts, conversions etc.etc.. I cannot seem to find a way in Access VB to send this to LPT1. Is there a special library I need? This would/should seem like a simple task, but its eating my lunch. In the old DOS world a copy to LPT1 would do it. All the info I find goes through elaborate ways to identify the printers which seems to be a long way around.

Am I over looking something simple?

I just need to print a string to LPT1?
 
In the case of Windows 2000 and later, it is no longer possible to open LPT1 for output to print to directly. I recommend use of the Win32 API to send information more directly to the printer. The example shows how to achieve this by using API functions that bypass printer drivers to communicate directly with the print spooler.


Try the following:

1. Create a new form in Access (Form1).
2. Place a Command Button on the form (Command1).
3. Add the following code to the Form1 code window:
Code:
Private Type DOCINFO
    pDocName As String
    pOutputFile As String
    pDatatype As String
End Type

Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
   "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
    ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias _
   "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
   pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
   pcWritten As Long) As Long

Private Sub Command1_Click()
    Dim lhPrinter As Long
    Dim lReturn As Long
    Dim lpcWritten As Long
    Dim lDoc As Long
    Dim sWrittenData As String
    Dim MyDocInfo As DOCINFO
    lReturn = OpenPrinter("Okidata ML 590", lhPrinter, 0)
    ' Substitute [b]Okidata ML 590[/b] with the name of your printer in Windows
    If lReturn = 0 Then
        MsgBox "The Printer Name you typed wasn't recognized."
        Exit Sub
    End If
    MyDocInfo.pDocName = "AAAAAA"
    MyDocInfo.pOutputFile = vbNullString
    MyDocInfo.pDatatype = vbNullString
    lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
    Call StartPagePrinter(lhPrinter)
    sWrittenData = "How's that for Magic !!!!" & vbFormFeed
    lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
       Len(sWrittenData), lpcWritten)
    lReturn = EndPagePrinter(lhPrinter)
    lReturn = EndDocPrinter(lhPrinter)
    lReturn = ClosePrinter(lhPrinter)
End Sub


The above was an adaptation of the Microsoft Knowledge Base Article - Q154078. You should be able to adapt this code for your needs. See if this works for you.
 
Thank You, thats kind of what I was seeing every where, lots of text for a little work, but progress is progress. So now the other question is the Win32 API? Is this a resource I need to add to my list, if so then where might it be hidden. I looked for it yesterday and found nothing.

Part of the code that came with the bar code info showed...

printer.print FF("test")

FF being a function. This seems to imply that I can some how identify "printer" as some sort of object.

Bear with me here, I have no formal training in this but over the past years I have managed to interface all kinds of things with Access software wise. I normally do control systems which is alot of db management and software interfacing. This printer port thing has me stumped. Seems like something that should be so simple.
 
Hi

Which version of Access?

Access2003 (and maybe 2002), has a printers collection. You can detect and change the default printer (see the Application object)

It is possible in Access97 to, but via the more convoluted PrintDev syntax

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
OK Thanks so far for the help. I've gotten the code into the project. Everything seems to be working as far as the code goes. But nothing seems print? I can watch the spool and see it heading that way and it seems to be getting there as it then clears. Pull the cable and it wil time out.

I'm getting all none zero values for each print command. I print to the printer from note pad and it works fine.
I'm thinking that I'm missing some printer codes to make the printer work.

This is not a normal printer, it is a Brady 2461 Label printer.
Any thoughts?
 
I'm Using the code method. I tried using the application method, but PrintDev nor any other reference to a printer seems available. This is Access 2000.

Thank You
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top