Showing posts with label FAQ. Show all posts
Showing posts with label FAQ. Show all posts

Saturday, July 5, 2008

Print via DOS mode using a DotMatrix printer in VB 6.0.

Q:
Hi Iam preparing a report on VB6. I wish to have my report printed in the DOS mode using a DotMatrix printer. How will I do it, since Iam not working on the command prompt. Is there any way to solve this issue? Are there any general rules irrespective of the tools used to undertake such printing ? Any help/suggestion in this regard would be highly appreciated.
Answer:
I wrote a driver module using windows API's for DOS print through VB 6.0. If you want the Module please send me your mail ID via comments in the bottom of this Post. Or simply copy and paste the solution below . if you have any doubt in this feel free to ask me...
Program:


Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As Long, ByVal hPrinter As Long) As Long
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Public Declare Function DefineDosDevice Lib "kernel32" Alias "DefineDosDeviceA" (ByVal dwFlags As Long, ByVal lpDeviceName As String, ByVal lpTargetPath As String) As Long
Public Const DDD_RAW_TARGET_PATH = &H1
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
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
Dim lhPrinter As Long
Dim lsp As Integer
Dim hsp As Integer
'------------------------------------------------------------
Function load_pr() As Boolean
Dim lReturn As Long
Dim lDoc As Long
Dim MyDocInfo As DOCINFO
lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
If lReturn = 0 Then
MsgBox "The Printer Name you typed wasn't recognized."
Exit Function
End If
MyDocInfo.pDocName = "AAAAAA"
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
Call StartPagePrinter(lhPrinter)
End Function
'---------------------------------------------------
Function send_pr(sss As String) As Boolean
Dim lReturn As Long
Dim lpcWritten As Long
'Dim sWrittenData As String
'sWrittenData = "How's that for Magic !!!!" & vbCrLf
sss = sss & vbCrLf
lReturn = WritePrinter(lhPrinter, ByVal sss, Len(sss), lpcWritten)

End Function
'--------------------------------------------------------------------
Function unload_pr() As Boolean
Dim lReturn As Long

lReturn = EndPagePrinter(lhPrinter)
lReturn = EndDocPrinter(lhPrinter)
lReturn = ClosePrinter(lhPrinter)

End Function

The workgroup Information file is missing or opened exclusively by another user in VB 6.0

The below code will be helpful if you have the error like
The workgroup Information file is missing or opened exclusively by another user in VB 6.0

Public cn As ADODB.Connection

Public Function DBConnect() As Boolean

On Error GoTo OpenErr

Dim MSDatabase

Dim str As String Set cn = New ADODB.Connection

cn.CursorLocation = adUseClient

str = "DBQ=" & App.Path & "\db.mdb;" & "DefaultDir=" & App.Path & ";" ' database name and directory

str = str + "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"

str = str + "PWD=dbb;UID=admin;" ' password & user id

str = str + "DriverId%;FIL=MS Access;MaxBufferSize 48;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;T hreads=3;UserCommitSync=Yes;"

cn.ConnectionString = str

cn.Open DBConnect = True

Exit Function

OpenErr:

MsgBox "Error Opening " & MSDatabase & vbNewLine & Err.Description, vbCritical, "Open Database Error"

DBConnect = False

End Function

'You just use the above code it will work nicely instead of Jet OLEDB 4.0