Printer Location and Model

I am trying to figure out how i can populate a drop down with the printer device name in column 1 (i know how to do from before here) and location & " - " & model in column 2, either from the printers on that machine (local or not) or all of the printers available over the network. (VBA of Course)
Who is Participating?
MerrionConnect With a Mentor Commented:
You will probably need to dip into the API to get this information.  GetPrinter with PRINTER_INFO_2 is your best bet....

Private Type PRINTER_INFO_2
    pServerName As String
    pPrinterName As String
    pShareName As String
    pPortName As String
    pDriverName As String
    pComment As String
    pLocation As String
    pDevMode As Long 'LPDEVMODEA
    pSepFile As String
    pPrintProcessor As String
    pDatatype As String
    pParameters As String
    pSecurityDescriptor As Long 'PSECURITY_DESCRIPTOR
    Attributes As Long
    Priority As Long
    DefaultPriority As Long
    StartTime As Long
    UntilTime As Long
    Status As Long
    JobsCount As Long
    AveragePPM As Long
End Type

Private Declare Function GetPrinterApi Lib "winspool.drv" Alias _
                           "GetPrinterA" (ByVal hPrinter As Long, _
                                            ByVal Level As Long, _
                                            buffer As Long, _
                                            ByVal pbSize As Long, _
                                            pbSizeNeeded As Long) As Long

Private Declare Function OpenPrinter Lib "winspool.drv" _
        Alias "OpenPrinterA" (ByVal pPrinterName As String, _
        phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long

Private Declare Function ClosePrinter Lib "winspool.drv" _
        (ByVal hPrinter As Long) As Long

  pDatatype As String
  pDevMode As Long
  DesiredAccess As Long
End Type

'\\ Pointer validation in StringFromPointer
Private Declare Function IsBadStringPtrByLong Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long

Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Long, ByVal lpString2 As String) As Long
Private Declare Function lstrcpyToBuffer Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long

Private Function GetPrinterLocation(Byval DeviceName As String ) As String

Dim lRet As Long
Dim SizeNeeded As Long
Dim buffer() As Long
Dim mhPrinter As Long '\\Printer handle...must close after use.

lRet = OpenPrinter(Devicename, mhPrinter, pDef)

If mhPrinter <> 0 Then
  ReDim Preserve buffer(0 To 1) As Long
  lRet = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer), SizeNeeded)
  ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
  lRet = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer) * 4, SizeNeeded)

        With mPRINTER_INFO_2
            .pServerName = StringFromPointer(buffer(0), 1024)
            .pPrinterName = StringFromPointer(buffer(1), 1024)
            .pShareName = StringFromPointer(buffer(2), 1024)
            .pPortName = StringFromPointer(buffer(3), 1024)
            .pDriverName = StringFromPointer(buffer(4), 1024)
            .pComment = StringFromPointer(buffer(5), 1024)
            .pLocation = StringFromPointer(buffer(6), 1024)
            .pDevMode = buffer(7)
            .pSepFile = StringFromPointer(buffer(8), 1024)
            .pPrintProcessor = StringFromPointer(buffer(9), 1024)
            .pDatatype = StringFromPointer(buffer(10), 1024)
            .pParameters = StringFromPointer(buffer(11), 1024)
            .pSecurityDescriptor = buffer(12)
            .Attributes = buffer(13)
            .Priority = buffer(14)
            .DefaultPriority = buffer(15)
            .StartTime = buffer(16)
            .UntilTime = buffer(17)
            .Status = buffer(18)
            .JobsCount = buffer(19)
            .AveragePPM = buffer(20)
        End With
       GetPrinterLocation = mPRINTER_INFO_2.pLocation
       Call ClosePrinter(mhPrinter)
  GetPrinterLocation = "Printer " & Devicename & " not found"
End If

End Function

'\\ --[StringFromPointer]-------------------------------------------------------------------
'\\ Returns a VB string from an API returned string pointer
'\\ Parameters:
'\\   lpString - The long pointer to the string
'\\   lMaxlength - the size of empty buffer to allow
'\\  DEJ 28/02/2001 Check pointer is a valid string pointer...
'\\ ----------------------------------------------------------------------------------------
'\\ (c) 2001 - Merrion Computing.  All rights  to use, reproduce or publish this code reserved
'\\ Please check for updates.
'\\ ----------------------------------------------------------------------------------------
Public Function StringFromPointer(lpString As Long, lMaxLength As Long) As String

Dim sRet As String
Dim lRet As Long

If lpString = 0 Then
    StringFromPointer = ""
    Exit Function
End If

lRet = lstrlen(lpString)
If lRet < lMaxLength Then
    lMaxLength = lRet
End If

If IsBadStringPtrByLong(lpString, lMaxLength) Then
    '\\ An error has occured - do not attempt to use this pointer
    Debug.Print "StringFromPointer - Attempt to read bad string pointer: " & lpString
    StringFromPointer = ""
    Exit Function
End If

'\\ Pre-initialise the return string...
sRet = Space$(lMaxLength)
Call lstrcpyToBuffer(sRet, lpString)
If Err.LastDllError = 0 Then
    If InStr(sRet, Chr$(0)) > 0 Then
        sRet = Left$(sRet, InStr(sRet, Chr$(0)) - 1)
    End If
End If

StringFromPointer = sRet

End Function

This is a cut-and-paste job so some declarations might be missing...let me know if there are...
Jeffrey CoachmanMIS LiasonCommented:
This information is covered in depth in the "Access Developers Handbook", Desktop Edition

(In the Access 2000 version it is chapter 10.)

Hope this helps
Y2KingswoodAuthor Commented:
Where would i find this "Access Developers Handbook"?
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Jeffrey CoachmanMIS LiasonCommented:

Yeah, the name makes you think it is some kind of proprietary publication. But you can actually find it in any large bookstore or on-line.

Here are two links:

Barnes and Noble:

There are actually two books one is "Desktop" and the other is "Enterprise".
I refered to the "Desktop" version of the book.

Try to buy the version of the book that is specific to your version of Access.
Y2KingswoodAuthor Commented:
you couldnt possible give me a snippet of code to save me 50 odd dollars?
Jeffrey CoachmanMIS LiasonCommented:
In Access 2000 (The version I have) it is not a "Snippet".

It's about a hundred lines of code! (Very confusing stuff)

In Access 2002, the printer Object is more "Exposed" to Visual Basic than it was Access 2000 or 97.

But again, I have the 2000 version of the book. I only read through the code once, just to see if I wanted to be bothered. But it turned out not to be worth it at that time. I never used it.

I hear, (but I don't kow for sure) that the code in 2002 and 2003 is a lot simpler (About 10 lines!)
(It probably involves looping through the "Printers" collection and loading the name of each Printer in the combobox)

With that in mind, You could probably do a search on Google and find it! Something like:
Access 2002 Printer Selection Dropdown ComboBox

Or even go down to your local book store and "Read" the code

I do that all the time. I'm cheap too!

(BTW: it is a GREAT book anyway, (well worth the $50) every Access developer has it on their shelf.)

Hope this helps.
Hi Y2Kingswood,

I'm not an expert here and didn't try it, but there is some code here:
I see PRINTER_INFO_2 has pLocation member.
Merrion who wrote that is also expert on EE, he's active in VB topic area, maybe you should ask there.

Jeffrey CoachmanMIS LiasonCommented:

Besides there is probably some "Nerdy" legal reason why we can't just Post Copyrighted, Published code here. (Need permission from the Author, Copyright infringement, Plagerism, Fair Use, .... bla ... bla ...bla

Y2KingswoodAuthor Commented:
hehe true true. ill try these sugestions out
All Courses

From novice to tech pro — start learning today.