Is it possible to print a report in VB using only Data Environment.

I mean setting the textboxes only to datasource and datafields in the record
I really dont want to use codes (RS, things like that) cause you see, I'm kind of a rushing so i don't have much time to code now.

The report to be printed is selected in listview control or the records to be printed is selected only.

Just want to print directly.

Thanks, really appreciate it.

It seems I have missed your post Abe, sorry.:)

You can not print a report with just text box values. You can however take a picture of the screen and then print that...

Uhm now i know, but now that i try to create a data report that does not have any database connection (At All), it says no Datasource or something like that :lol:

Its a plain report only, a slip. no textbox, only labels and lines.

If it is going to be a normal cash slip etc, use the print function to add the text to the slip and then print.

Printer.Newpage

Printer.ScaleMode = 6
Printer.FontSize = 14
Printer.ForeColor = vbBlack

You can also play with the following -

Dim PRN As Object

Private Sub cmdPrinter_Click()
    Set PRN = Printer
    PRN.ScaleMode = 6
    CreateSimplePrintout
    Printer.EndDoc
End Sub

Private Sub cmdPrnForm_Click()
    Set PRN = frmPrintout
    PRN.ScaleMode = 6
    CreateSimplePrintout
End Sub

Private Sub CreateSimplePrintout()
    CommonDialog1.CancelError = True
    On Error Resume Next
    CommonDialog1.ShowPrinter
    If Err.Number = 32755 Then
        Exit Sub
    End If
    On Error GoTo 0
    If CommonDialog1.Orientation = cdlLandscape Then
        Printer.Orientation = cdlLandscape
    End If
    ClearPrintoutArea
    ' Position title on the page
    PRN.CurrentX = 30: PRN.CurrentY = 15
    ' Set title font
    Dim fnt As New StdFont
    fnt.Name = "Verdana": fnt.Size = 14: fnt.Bold = True
    Set PRN.Font = fnt
    PRN.Print "Simple Printout Demo"
    PRN.Print
    ' Use smaller size for text
    fnt.Size = 12: fnt.Bold = False
    Set PRN.Font = fnt
    PRN.Print "Print and preview formatted text. " & _
                "If the text printed with a single call to the Print method exceeds " & _
                "the width of the form, the text isn't wrapped automatically."
    PRN.Print
    PRN.Print "However, every time you call the Print method, " & _
                "the text start at the far left edge of the form on the following line."
    PRN.Print
    PRN.Print
    ' Another font font for formatted text
    fnt.Size = 10: fnt.Italic = True
    Set PRN.Font = fnt
    ' Set the left margin.
    ' No need to set the vertical coordinate,
    ' because each call to the Print method
    ' advances to the following line
    PRN.CurrentX = 20
    PRN.Print "Print and preview formatted text. "
    PRN.CurrentX = 20
    PRN.Print "If the text printed with a single call to the Print method exceeds "
    PRN.CurrentX = 20
    PRN.Print "the width of the form, the text isn't wrapped automatically."
    ' Call Print method to advance vertically
    PRN.Print
    PRN.CurrentX = 20
    PRN.Print "However, every time you call the Print method, "
    PRN.CurrentX = 20
    PRN.Print "the text starts at the far left edge of the form "
    PRN.CurrentX = 20
    PRN.Print "on the following line."
    PRN.Line (10, PRN.CurrentY)-(PRN.ScaleWidth - 10, PRN.CurrentY)
    PRN.Font.Italic = False
    PRN.Font.Bold = True
    ' Print left aligned text (no special action required)
    Dim str As String
    str = "Left aligned Text"
    PRN.CurrentX = 0
    PRN.CurrentY = PRN.CurrentY + 5
    YPos = PRN.CurrentY
    PRN.Print str
    ' Print centered text
    ' Use the TextWidth method to retrieve the width
    ' of a string when rendered on a specific device
    ' (screen/printer) and then leave same amount of
    ' space on either side of the string
    str = "Centered Text"
    PRN.CurrentX = (PRN.ScaleWidth - PRN.TextWidth(str)) / 2
    PRN.CurrentY = YPos
    PRN.Print str
    ' Print right aligned text
    ' As with the centered text, calculate the width
    ' of the string and then leave the extra space to
    ' the left of the string
    str = "Right Aligned Text"
    PRN.CurrentX = PRN.ScaleWidth - PRN.TextWidth(str)
    PRN.CurrentY = YPos
    PRN.Print str
    
    ' Print the same string in three different sizes
    ' with their enclosing rectangles
    PRN.Font.Italic = False
    PRN.Font.Bold = False
    PRN.CurrentX = 20
    PRN.CurrentY = 120
    PRN.Font.Size = 8
    str = "(abcxyz)[ABCXYZ]{123}"
    PRN.Print str
    PRN.Line (20, 120)-(20 + PRN.TextWidth(str), 120 + PRN.TextHeight(str)), , B

    PRN.CurrentX = 60
    PRN.CurrentY = 120
    PRN.Font.Size = 12
    PRN.Print str
    PRN.Line (60, 120)-(60 + PRN.TextWidth(str), 120 + PRN.TextHeight(str)), , B
    
    PRN.CurrentX = 115
    PRN.CurrentY = 120
    PRN.Font.Size = 20
    PRN.Print str
    PRN.Line (115, 120)-(115 + PRN.TextWidth(str), 120 + PRN.TextHeight(str)), , B
    
    str = ".     ."
    PRN.CurrentX = 20
    PRN.CurrentY = 140
    PRN.Font.Size = 8
    PRN.Print str
    PRN.Line (20, 140)-(20 + PRN.TextWidth(str), 140 + PRN.TextHeight(str)), , B

    PRN.CurrentX = 30
    PRN.CurrentY = 140
    PRN.Font.Size = 12
    PRN.Print str
    PRN.Line (30, 140)-(30 + PRN.TextWidth(str), 140 + PRN.TextHeight(str)), , B
    
    PRN.CurrentX = 45
    PRN.CurrentY = 140
    PRN.Font.Size = 20
    PRN.Print str
    PRN.Line (45, 140)-(45 + PRN.TextWidth(str), 140 + PRN.TextHeight(str)), , B
    
End Sub


Private Sub ClearPrintoutArea()
    If PRN Is Printer Then
        PRN.NewPage
    Else
        PRN.Cls
    End If
End Sub

I hope this helps in getting a solution to your problem...:)

If you want to go low level and have a printer direct connected to the a printer port (no idea how it handles USB) you can write to it directly as follows

Open "PRN" For Output As #1
Print #1, "Here is line one"
Print #1, "Here is a text box " & Text1.Text
Close #1

Thanks. Got it right.

Preferably the 1st code from sir Andre.

The 2nd one's a bit too long. ;)

It was a pleasure abe. Happy coding...:)

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.