my code is printing directly to the printer but i want to see the preview

Dim vlineCount As Integer
    Dim vpageCount As Integer
    Dim SN As Integer
    Dim sqlp As String
    vlineCount = 0
    vpageCount = 1
    SN = 0
    
    '//NOTE : Report file name should never contain blank space.
    Open App.Path & "\Report.txt" For Output As #1 '//Report file Creation
    
    '//Create Report Heading
    '//chr(27) & chr(71) & chr(14) - for Enlarge letter and bold
    '//chr(27) & chr(45) & chr(1) - for Enlarge letter and bold
   
    Print #1, Space(20) & Chr(27) & Chr(71) & Chr(14) & _
              Chr(27) & Chr(45) & Chr(1) & _
              "LABOUR DATA" & _
              Chr(27) & Chr(72) & Chr(20) & _
              Chr(27) & Chr(45) & Chr(0)        '//Bold Off

             
               
              
    '//Printing the header for Page
    Print #1, Space(3) & RepeatString("=", 123)
    Print #1, Chr(27) & Chr(27) & _
              Space(3) & AlignLeft("SNo.", 5) & Space(1) & _
              Space(1) & AlignLeft("NAME", 20) & Space(1) & _
              Space(1) & AlignRight("AGE", 5) & Space(1) & _
              Space(1) & AlignLeft("ADDRESS", 20) & Space(1) & _
              Space(1) & AlignLeft("Martial Status", 15) & Space(1) & _
              Space(1) & AlignLeft("Reg. No.", 12) & Space(1) & _
              Space(1) & AlignLeft("Reg. Officer's Sign.", 25) & _
              Chr(27) & Chr(72)  '//Bold Ends
    Print #1, Space(3) & RepeatString("=", 123)
    Set cn_pall = New ADODB.Connection
    With cn_pall
        .ConnectionString = cmd
        .Open
    End With
    sqlp = "select * from labour_main where reg_no=" & Val(Text13.Text)
    
    Set rs_pall = New ADODB.Recordset
    If rs_pall.State = 1 Then rs_pall.Close
    rs_pall.CursorLocation = adUseClient
    rs_pall.Open sqlp, cn_pall, adOpenDynamic, adLockOptimistic
    Do While Not rs_pall.EOF
        'SN = SN + 1
        If vlineCount >= 50 Then '//Set number of lines per page according to yours
            vlineCount = 0
            
            '//ending this page
            Print #1, Space(3) & RepeatString("=", 123)
            Print #1, Space(3) & AlignRight("Page: " & vpageCount, 123)
            Print #1, Chr(13) '//Line break
            
            vpageCount = vpageCount + 1

            '//Printing the header for next Page
            Print #1, Space(3) & RepeatString("=", 123)
            Print #1, Chr$(27) & Chr$(27) & _
                    Space(3) & AlignLeft("SNo.", 5) & Space(3) & _
                    Space(3) & AlignLeft("NAME", 20) & Space(3) & _
                    Space(3) & AlignRight("AGE", 5) & Space(3) & _
                    Space(3) & AlignLeft("ADDRESS", 20) & Space(3) & _
                    Space(3) & AlignLeft("Martial Status", 15) & Space(2) & _
                    Space(3) & AlignLeft("Reg. No.", 14) & Space(3) & _
                    Space(3) & AlignLeft("Reg. Officer's Sign.", 25) & _
                    Chr$(27) & Chr$(72)  '//Bold Ends
            Print #1, Space(3) & RepeatString("=", 123)
        End If
        
        Print #1, Space(3) & AlignRight(rs_pall.Fields(0).Value, 5) & Space(1) & _
                  Space(1) & AlignLeft(UCase(rs_pall.Fields(1).Value), 20) & Space(1) & _
                  Space(1) & AlignRight(rs_pall.Fields(2).Value, 5) & Space(1) & _
                  Space(1) & AlignLeft(UCase(rs_pall.Fields(3).Value), 20) & Space(1) & _
                  Space(1) & AlignLeft(UCase(rs_pall.Fields(7).Value), 15) & Space(1) & _
                  Space(1) & AlignLeft(rs_pall.Fields(8).Value, 14) & Space(1) & _
                  Space(1) & AlignLeft(rs_pall.Fields(10).Value, 25)
                  
                  
        
        Print #1, Space(39) & AlignLeft(UCase(rs_pall.Fields(4).Value), 20)
        Print #1, Space(39) & AlignLeft(UCase(rs_pall.Fields(5).Value), 20)
        Print #1, Space(39) & AlignLeft(UCase(rs_pall.Fields(6).Value), 20)
        Print #1,
        '// printing Family Detail
        '/////////////////////////
        Set cn_child = New ADODB.Connection
        With cn_child
            .ConnectionString = cmd
            .Open
        End With
        sql_child = "select * from labour_child1 where reg_no=" & Val(rs_pall.Fields(8))
        Set rs_child = New ADODB.Recordset
        If rs_child.State = 1 Then rs_child.Close
        rs_child.CursorLocation = adUseClient
        rs_child.Open sql_child, cn_child, adOpenDynamic, adLockOptimistic
        If rs_child.EOF = False Then
            Print #1, Space(3) & AlignLeft("FAMILY DETAILS", 15)
            Print #1, Space(3) & RepeatString("-", 135)
            Print #1, Space(3) & AlignLeft("NAME", 20) & Space(3) & _
                    Space(3) & AlignRight("AGE", 5) & Space(3) & _
                    Space(3) & AlignLeft("RELATION", 10)
            Print #1, Space(3) & RepeatString("-", 123)
        End If

        Do While Not rs_child.EOF
            Print #1, Space(3) & AlignLeft(UCase(rs_child.Fields(1).Value), 20) & Space(3) & _
                        Space(3) & AlignRight(rs_child.Fields(2).Value, 5) & Space(3) & _
                        Space(3) & AlignLeft(UCase(rs_child.Fields(3).Value), 10)
            
            rs_child.MoveNext
        Loop
        Print #1,
        rs_child.Close
        Set rs_child = Nothing
        cn_child.Close
        Set cn_child = Nothing
        '//end of Family Detail
        '//////////////////////
        
        '// Printing reg detai
        '/////////////////////
        Set cn_child = New ADODB.Connection
        With cn_child
            .ConnectionString = cmd
            .Open
        End With
        sql_child = "select * from labour_child2 where reg_no=" & Val(rs_pall.Fields(8))
        Set rs_child = New ADODB.Recordset
        If rs_child.State = 1 Then rs_child.Close
        rs_child.CursorLocation = adUseClient
        rs_child.Open sql_child, cn_child, adOpenDynamic, adLockOptimistic
        If rs_child.EOF = False Then
            Print #1, Space(3) & AlignLeft("REGISTRATION DETAILS", 22)
            Print #1, Space(3) & RepeatString("-", 123)
            Print #1, Space(3) & AlignLeft("REG./RENEW. DATE", 20) & Space(3) & _
                    Space(3) & AlignLeft("BANK/CASH", 20) & Space(3) & _
                    Space(3) & AlignRight("AMOUNT", 10) & Space(3) & _
                    Space(3) & AlignRight("BILL No.", 10)
            Print #1, Space(3) & RepeatString("-", 123)
        End If

        Do While Not rs_child.EOF
            Print #1, Space(3) & AlignLeft(Format(rs_child.Fields(1).Value, "dd/MM/yyyy"), 20) & Space(3) & _
                        Space(3) & AlignLeft(UCase(rs_child.Fields(2).Value), 20) & Space(3) & _
                        Space(3) & AlignRight(rs_child.Fields(3).Value, 10) & Space(3) & _
                        Space(3) & AlignRight(rs_child.Fields(4).Value, 10)
            rs_child.MoveNext
        Loop
        rs_child.Close
        Set rs_child = Nothing
        cn_child.Close
        Set cn_child = Nothing
        
        '//end of registration detail
        '////////////////////////////
        Print #1,
        Print #1, Space(3) & AlignLeft("Remark :", 9) & _
                  AlignLeft(rs_pall.Fields(9).Value, 20)
        Print #1, Space(3) & RepeatString("=", 123)
        Print #1,
        vlineCount = vlineCount + 1
        rs_pall.MoveNext
    Loop
    
    '//End of the report
    Print #1, Space(3) & RepeatString("=", 123)
    Print #1, AlignRight("Page: " & vpageCount, 70)
    
    Close #1 '//Closing the file
    MsgBox "Report file generated" & vbCrLf & "Click OK to Print Report on Paper"
    Open App.Path & "\repo.bat" For Output As #1 '//Creating Batch file
    
    Print #1, "TYPE " & App.Path & "\Report.txt > PRN"
    Print #1, "EXIT"
    Close #1
    rs_pall.Close
    Set rs_pall = Nothing
    cn_pall.Close
    Set cn_pall = Nothing
    '//HERE write the proper path where your command.com file exist
    Shell "C:\WINDOWS\system32\COMMAND.COM /C " & App.Path & "\REPO.BAT", vbHide

Dani AI

Generated

A reliable preview comes from rendering the same report data you send to the printer — not from sending bytes straight to the device and hoping you can intercept them. Generate the report into a buffer or temp file, then render that buffer into a preview UI. That keeps formatting code in one place so the preview always matches the printed output.

For a quick proof-of-concept (as suggested), show the report in a fixed-width viewer (RichTextBox or multiline TextBox using Courier New) so columns line up. Before showing, scan the text for printer control sequences (escape/ASCII 27 and the following bytes) and either remove them or translate them into visual styles (bold, larger font, underline) so the preview is readable. Preserve spaces and explicit line breaks — those keep your column alignment.

For a robust solution (building on ’s point about direct device writes), refactor to a single "render" routine that accepts a device context: call it with a bitmap/graphics for preview and with the printer DC for printing. In VB6 you can draw to a PictureBox scaled to page size; in VB.NET use PrintDocument + PrintPreviewDialog. That way you parse/interpret escape codes once, build style runs, and draw the runs to whichever surface you need.

Example (VB.NET PrintPreview sketch):

Private WithEvents pd As New Printing.PrintDocument
Private previewDlg As New PrintPreviewDialog()

Sub ShowPreview()
    AddHandler pd.PrintPage, AddressOf pd_PrintPage
    previewDlg.Document = pd
    previewDlg.ShowDialog()
End Sub

Private Sub pd_PrintPage(sender As Object, e As Printing.PrintPageEventArgs)
    Dim txt = IO.File.ReadAllText(IO.Path.Combine(Application.StartupPath, "Report.txt"))
    e.Graphics.DrawString(txt, New Font("Courier New", 9), Brushes.Black, e.MarginBounds)
    e.HasMorePages = False
End Sub

Troubleshooting: test with small files, confirm monospaced font preserves alignment, and incrementally add escape-code handling so the preview progressively matches your printer output.

Recommended Answers

All 2 Replies

Depends on how you want to see it. Simplest would be to just take the report.txt file, open it, line input it, then dump it into a textbox. Of course, there's the whole size limit thing to worry about.

Next possibility is to have a "Preview" button that creates the text file, then instead of your REPO.BAT file using "TYPE report.txt > PRN", you could just use "TYPE report.txt | MORE". This will display the output to the console and pause when the screen fills. If they don't want to go through the entire file, they can use the <ctrl>-<c> keystroke combination to abort.

Hope this helps! Good luck!

I don't really think that shelling out to a batch file is particularly good. It might be better to write to the printer directly yourself. you can open it as follows

open PRN for output as #2

As far as previewing, why don't you load the lines from the file into a list box then your user could scroll up and down through the report.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.