Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePrint data from a database
Keywordsdatabase, print
CategoriesDatabase, Graphics
 
Select the data into a recordset and loop through the records. For each, set the Printer's CurrentX position to the left margin and print the record's values.

This automatically moves the Printer to the next line. See if CurrentY has reached the bottom margin and, if it has, start a new page.

 
Private Sub Command1_Click()
' Use 1 inch margins.
Const TOP_MARGIN = 1440
Const LEFT_MARGIN = 1440

Dim bottom_margin As Single
Dim db As Database
Dim qdef As QueryDef
Dim rs As Recordset
Dim dbname As String
Dim the_year As Integer
Dim the_value As Integer

    MousePointer = vbHourglass
    DoEvents

    ' Open the database.
    dbname = App.Path
    If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
    dbname = dbname & "data.mdb"
    Set db = OpenDatabase(dbname)

    ' Get the records.
    Set qdef = db.CreateQueryDef("", _
        "SELECT Year, Value FROM YearlyValues")
    Set rs = qdef.OpenRecordset(dbOpenSnapshot)

    ' Read the data and print it.
    bottom_margin = Printer.ScaleTop + _
        Printer.ScaleHeight - 1440
    rs.MoveFirst
    
    Printer.CurrentY = TOP_MARGIN
    Do While Not rs.EOF
        ' Use rs!FieldName to get the data for
        ' the field named FieldName.
        Printer.CurrentX = LEFT_MARGIN
        Printer.Print _
            Format$(rs!Year, "yyyy") & _
            vbTab & _
            Format$(rs!Value, "0.0000")

        ' See if we have filled the page.
        If Printer.CurrentY >= bottom_margin Then
            ' Start a new page.
            Printer.NewPage
            Printer.CurrentY = TOP_MARGIN
        End If
        
        rs.MoveNext
    Loop

    rs.Close
    db.Close

    ' Finish printing.
    Printer.EndDoc

    MousePointer = vbDefault
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated