Sunday, June 17, 2012

Contoh CommonDialog - Print Dengan Range Tertentu

Option Explicit

Private Sub Command1_Click()
Dim myDatabase As Database
Dim rsMyTable As Recordset
Dim i As Integer
Dim j As Integer
Dim startpage As Integer

CommonDialog1.Max = 3
CommonDialog1.FromPage = 1
CommonDialog1.ToPage = 3
CommonDialog1.flags = 0
CommonDialog1.ShowPrinter
startpage = CommonDialog1.FromPage

Printer.FontSize = 18

Set myDatabase = OpenDatabase("nwind.mdb")
Set rsMyTable = myDatabase.OpenRecordset("Customers")

rsMyTable.MoveFirst

If (CommonDialog1.flags And cdlPDPageNums) <> 0 Then
MsgBox " Printing pages " & CommonDialog1.FromPage & " to " & CommonDialog1.ToPage
Select Case startpage
Case 1

Case 2
For i = 1 To 42
rsMyTable.MoveNext
Next

Case 3
For i = 1 To 84
rsMyTable.MoveNext
Next
End Select

If startpage <> 0 Then
For j = startpage To CommonDialog1.ToPage
For i = 1 To 42
If rsMyTable.EOF Then Exit For
Text1.Text = Text1.Text & rsMyTable!CompanyName & vbCrLf
Printer.Print rsMyTable!CompanyName
rsMyTable.MoveNext
Next
Printer.NewPage
Next
Printer.EndDoc
End If

ElseIf (CommonDialog1.flags And cdlPDSelection) <> 0 Then

rsMyTable.MoveLast
rsMyTable.MoveFirst
For i = 1 To rsMyTable.RecordCount
Text1.Text = Text1.Text & rsMyTable!CompanyName & vbCrLf
rsMyTable.MoveNext
Next
MsgBox "Select text to be printed"
Else
For i = 1 To rsMyTable.RecordCount
Text1.Text = Text1.Text & rsMyTable!CompanyName & vbCrLf
rsMyTable.MoveNext
Next
Printer.Print Text1.Text
Printer.EndDoc
MsgBox "Printing all pages"
End If
End Sub

Private Sub Command2_Click()
Printer.Print Text1.SelText
Printer.EndDoc
End Sub

Private Sub Form_Load()
Command1.Caption = "Select Printing Option"
Command2.Caption = "Print selected text"
End Sub