Solved

Printing from a Datagrid

Posted on 2000-04-17
3
207 Views
Last Modified: 2010-05-02
I am trying to allow a user to print out information from a database query that is appearing to him in a datagrid.  What is the easiest way to print out this information since a datagrid doesn't have a print method?
0
Comment
Question by:sladej
3 Comments
 
LVL 1

Expert Comment

by:tkuppinen
ID: 2723595
Add a data report to your project and bind the datasource to the smae datasource to which the grid is bound.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2723605
I have this code that I never tried.

Public Sub MSFlexGridPrint(gridQuery As Control)
   ' *** Print MSFlexGrid

   Dim I          As Integer
   Dim J          As Integer
   Dim PTab       As Integer

   For I = 1 To gridQuery.Rows - 1
      PTab = 20 'Set the first tab value
      'This checks to see if a page break is needed

      If Printer.CurrentY + Printer.TextHeight(gridQuery.Text) > Printer.ScaleHeight - 600 Then
         Printer.NewPage
      End If

      gridQuery.Row = I 'Set the active row

      For J = 0 To gridQuery.Cols - 1
         gridQuery.Col = J 'Set the current column
           
         'Send the field to the print line and add the tab.
         'Notice the semicolon at the end of the line.

         Printer.Print Trim$(gridQuery.Text); Tab(PTab);

         PTab = PTab + 20 'Increment the tab value
      Next

      'After each column has printed to the print line

      'a Printer.Print statement without a semicolon will
      'will send the line to the printer and start a new
      'one.
      Printer.Print
   Next

End Sub    
 
Please tell me your result!
0
 

Accepted Solution

by:
brandont earned 50 total points
ID: 2727296
The easiest way is definitely NOT to create a data report.  If you don't need a lot of formatting options, just use the printer object and send all the info from the grid (similar to above comment).  I will generally declare a variable for each column that I want printed, and set them to the horizontal location on the page where I want it to print (i.e. COL1 = 1.5: COL2 = COL1 + 1, etc).  Below is an example of how I generally handle it.

Sub PrintHline(lmar As Double, rmar As Double)
    Printer.Line (lmar, Printer.CurrentY)-(rmar, Printer.CurrentY)
    Printer.CurrentX = lmar
End Sub
Sub LineFeed(xPosition As Double)
    Dim LF As String: LF = Chr(13)
    Printer.Print LF
    Printer.CurrentX = xPosition
End Sub
Sub PrintText(dblXpos As Double, dblYpos As Double, strText As String)
    Printer.CurrentX = dblXpos: Printer.CurrentY = dblYpos
    Printer.Print strText
End Sub
Sub PrintDoc(lmar As Double, rmar As Double, tmar As Double, bmar As Double, Optional SelectOnly As Boolean)
    Dim Y As Double, lngRow As Integer, lngCol As Integer ', lngNumItems As Integer
    Dim COL1 As Double, COL2 As Double, COL3 As Double, COL4 As Double, COL5 As Double
    Dim intStart As Integer, intEnd As Integer
    Printer.FontName = "Arial"
    Printer.FontSize = 8
   
    COL1 = lmar: COL2 = COL1 + 2.25: COL3 = COL2 + 0.75: COL4 = COL3 + 0.5: COL5 = COL4 + 0.5
    Printer.FontBold = True: Printer.FontSize = 10
    PrintText lmar, tmar, "Laser Library Tracker - Printed on: " & Date & " at " & Time
    Printer.FontSize = 8
    PrintHline lmar, rmar
    LineFeed lmar
    Y = Printer.CurrentY
    PrintText COL1, Y, "Form": PrintText COL2, Y, "Status": PrintText COL3, Y, "Date"
    PrintText COL4, Y, "Tstr": PrintText COL5, Y, "Problem"
    PrintHline lmar, rmar
    Printer.FontBold = False
    Y = Printer.CurrentY + 0.05
    'Print items in grid
    With frmMain.MSFlexGrid1
        If SelectOnly Then
            intStart = .Row
            intEnd = .RowSel
        Else
            intStart = 1
            intEnd = .Rows - 1
        End If
        'lngNumItems = .Rows
        'For lngRow = 1 To lngNumItems - 1
        For lngRow = intStart To intEnd
            .Row = lngRow
            .Col = GRD_FORM: PrintText COL1, Y, .Text
            .Col = GRD_STATUS: PrintText COL2, Y, .Text
            .Col = GRD_DATE: PrintText COL3, Y, .Text
            .Col = GRD_INITIALS: PrintText COL4, Y, .Text
            .Col = GRD_PROBLEM: PrintText COL5, Y, .Text
            If lngRow Mod 10 = 0 Then
                Y = Printer.CurrentY + 0.12
                Else
                Y = Printer.CurrentY + 0.04
            End If
            If Y >= bmar Then Printer.NewPage: Y = tmar
        Next
    End With
    Printer.EndDoc
    Exit Sub
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
consolidate or create a new vba code for performing tasks 5 62
VBA error replacing data 6 36
Microsoft Access combo box help 2 37
Spell Check in VB6 13 95
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now