.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

print codings

Posted By: bas     Posted Date: March 28, 2012    Points:5   Category :VB.Net
i want the vb coding for printing the datas in the database.by click the print button the datas in the database have to be print.Please provide necessary coding for printing.

Author: Mahesh             
Posted Date: March 29, 2012     Points: 20   

Use Following code,

Private Sub cmdPrint_Click()
Dim intLineCtr As Integer
Dim intPageCtr As Integer
Dim intX As Integer
Dim strCustFileName As String
Dim strBackSlash As String
Dim intCustFileNbr As Integer

Dim strFirstName As String
Dim strLastName As String
Dim strAddr As String
Dim strCity As String
Dim strState As String
Dim strZip As String

Const intLINE_START_POS As Integer = 6
Const intLINES_PER_PAGE As Integer = 60

' Have the user make sure his/her printer is ready ...
If MsgBox("Make sure your printer is on-line and " _
& "loaded with paper.", vbOKCancel, "Check Printer") = vbCancel _
Exit Sub
End If

' Set the printer font to Courier, if available (otherwise, we would be
' relying on the default font for the Windows printer, which may or
' may not be set to an appropriate font) ...

For intX = 0 To Printer.FontCount - 1
If Printer.Fonts(intX) Like "Courier*" Then
Printer.FontName = Printer.Fonts(intX)
Exit For
End If

Printer.FontSize = 10

' initialize report variables ...
intPageCtr = 0
intLineCtr = 99 ' initialize line counter to an arbitrarily high number

' to force the first page break
' prepare file name & number
strBackSlash = IIf(Right$(App.Path, 1) = "\", "", "\")
strCustFileName = App.Path & strBackSlash & "customer.txt"
intCustFileNbr = FreeFile

' open the input file
Open strCustFileName For Input As #intCustFileNbr

' read and print all the records in the input file
Do Until EOF(intCustFileNbr)

' read a record from the input file and store the fields there into VB variables
Input #intCustFileNbr, strLastName, strFirstName, strAddr, strCity, strState, strZip

' if the number of lines printed so far exceeds the maximum number of lines
' allowed on a page, invoke the PrintHeadings subroutine to do a page break
If intLineCtr > intLINES_PER_PAGE Then
GoSub PrintHeadings
End If

' print a line of data
Printer.Print Tab(intLINE_START_POS); _
strFirstName & " " & strLastName; _
Tab(21 + intLINE_START_POS); _
strAddr; _
Tab(48 + intLINE_START_POS); _
strCity; _
Tab(72 + intLINE_START_POS); _
strState; _
Tab(76 + intLINE_START_POS); _

' increment the line count
intLineCtr = intLineCtr + 1


' close the input file
Close #intCustFileNbr

' Important! When done, the EndDoc method of the Printer object must be invoked.
' The EndDoc method terminates a print operation sent to the Printer object,
' releasing the document to the print device or spooler.


Exit Sub

' internal subroutine to print report headings
' If we are about to print any page other than the first, invoke the NewPage
' method to perform a page break. The NewPage method advances to the next
' printer page and resets the print position to the upper-left corner of the
' new page.

If intPageCtr > 0 Then
End If

' increment the page counter
intPageCtr = intPageCtr + 1

' Print 4 blank lines, which provides a for top margin. These four lines do NOT
' count toward the limit of 60 lines.


' Print the main headings
Printer.Print Tab(intLINE_START_POS); _
"Print Date: "; _
Format$(Date, "mm/dd/yy"); _
Tab(intLINE_START_POS + 31); _
Tab(intLINE_START_POS + 73); _
"Page:"; _
Format$(intPageCtr, "@@@")
Printer.Print Tab(intLINE_START_POS); _
"Print Time: "; _
Format$(Time, "hh:nn:ss"); _
Tab(intLINE_START_POS + 33); _


' Print the column headings
Printer.Print Tab(intLINE_START_POS); _
Tab(21 + intLINE_START_POS); _
Tab(48 + intLINE_START_POS); _
"CITY"; _
Tab(72 + intLINE_START_POS); _
"ST"; _
Tab(76 + intLINE_START_POS); _

Printer.Print Tab(intLINE_START_POS); _
"-------------"; _
Tab(21 + intLINE_START_POS); _
"-------"; _
Tab(48 + intLINE_START_POS); _
"----"; _
Tab(72 + intLINE_START_POS); _
"--"; _
Tab(76 + intLINE_START_POS); _


' reset the line counter to reflect the number of lines that have now
' been printed on the new page.
intLineCtr = 6


End Sub

Hope this helps...

Thanks & Regards,

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend