I am using this method to take print of data peresent in textboxes but it prints on top left corner i want to take print in a sequence and distance as it apears on form also want to increase font size waht should i do please help me.

Printer.Print txtstudentid.Text
Printer.Print txtstudentname
Printer.Print txtfathername
Printer.Print txtdob
Printer.Print txtclass
Printer.Print txtsubjects
Printer.Print txtmedium
Printer.Print txtfee
Printer.Print txttotalsubjects
Printer.Print txtadmission
Printer.EndDoc

Recommended Answers

All 18 Replies

Use the following. You will have to modify slightly, but it gives you all the properties required to set your print form properly.

Option Explicit

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

This peace of code have some bugs please check it for errors

I have observed some like

1. CommonDialog1 is not defined
2. frmPrintout is not defined
3. Also these two peaces of code confusing me

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

You have to add the common dialog control to your form. Select "Project/Components/Microsoft Common Dialog Control" from the toolbar.

frmPrintout is obviously the forms name. Change this to whatever your form is currently called.

What about the code do you find confusing? Name the row line.

This line of code what is cmdprnform

Private Sub cmdPrnForm_Click()

It means cmd (command Button) Prn (Print) Form (the current form)

It is just a name given to the command button, which will print the current form. Rename it to whatever you like.:)

Now giving me error "User defiend variable not defiend"
YPos = PRN.CurrentY

You mean i should have two Command button one is for this peace of code

Private Sub cmdPrinter_Click()
Set PRN = PrinterPRN.ScaleMode = 6
CreateSimplePrintoutPrinter.EndDocEnd Sub

and second is for


Private Sub cmdPrnForm_Click()Set PRN = frmPrintoutPRN.ScaleMode = 6CreateSimplePrintoutEnd SubPrivate 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

i think the both have same function

Yes, Have a look at the 2 different actions taken under each command button -

Private Sub cmdPrinter_Click()
Set PRN = Printer 'Here...

Private Sub cmdPrnForm_Click()
Set PRN = frmPrintout 'And here...

It is giving me error on this line

Now giving me error "User defiend variable not defiend"
YPos = PRN.CurrentY

Declare the Y position -

Dim YPos As Long

Ok its not printing the contents of textboxes it just prints this text

"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.

What is the problem with it.

Remember that the code given was only a sample. You still need to convert the sentence "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" to whatever you want printed.

In this case, write some code to gather all the textbox's text and then put it into one bunch and then print.

Ok see this the text which i want to print

Printer.Print txtstudentid.Text
Printer.Print txtstudentname
Printer.Print txtfathername
Printer.Print txtdob
Printer.Print txtclass
Printer.Print txtsubjects
Printer.Print txtmedium
Printer.Print txtfee
Printer.Print txttotalsubjects
Printer.Print txtadmissiondate
Printer.Print Label1
Printer.Print Label2
Printer.Print Label3
Printer.Print Label4
Printer.Print Label5
Printer.Print Label6
Printer.Print Label7
Printer.Print Label8
Printer.Print Label9
Printer.Print Label10
Printer.Print Label11
Printer.Print Label12

Can you modify it for me please

Ok now i have made some changes can you guide me how much more changes should i made and where should i made changes

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
Dim YPos As Long
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 "Excellent Acadmy"
PRN.Print
' Use smaller size for text
fnt.Size = 12: fnt.Bold = False
Set PRN.Font = fnt
PRN.Print "txtstudentid.Text " & "txtstudentname " & "txtfathername" & _
"txtdob" & "txtclass" & "txtsubjects" & "txtmedium" & "txtfee" & _
"txttotalsubjects" & "txtadmissiondate"
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

I'll post a solution tomorrow morning. Shooting home now, cheers.:)

Ok thanks

Use the PRN.CurrentX, CurrentY and Line methods to align the text from your textboxes. You can delete all the other unnecessary print events -

"the text start at the far left edge of the form on the following line."'Delete This etc.......
'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 '#######USE THIS TO ALIGN TEXT..........
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)'#####OR THIS .....
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 '###### OR AS IN HERE.
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

You need to play around with this code so you know how it works and why. I can do this for you, but that will be breaking the rules of posting here. I gave you the guideline, all you need to do now is to fine tune it.:)

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.