Solved

Printing from a Datagrid

Posted on 2000-04-17
3
206 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

19 Experts available now in Live!

Get 1:1 Help Now