• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Printing a dbgrid

I would like to know the easiest way to print the contents of a dbgrid.
1 Solution


Set your Crystal Report Control Datasource property to your data control. Then add the following code to your print button.

    CR.ReportSource = crptDataControl
    CR.Action = 1

If you are using a bound DBGrid this will print what is in the grid


Public Sub GridExport(GridToExport _
   As Object, FileName As String, Optional _
   Delimiter As Variant, Optional _
   EncloseStrings As Variant)
   Dim iNumRows As Integer
   Dim iNumCols As Integer
   Dim iFileNumber As Integer
   If IsMissing(Delimiter) Then
      Delimiter = vbTab
   End If
   If IsMissing(EncloseStrings) Then
      EncloseStirngs = "" 
   End If
   iFileNumber = FreeFile
   Open FileName For Output As #iFileNumber
   For iNumRows = 0 To _
      GridToExport.rows - 1
      GridToExport.Row = iNumRows
      For iNumCols = 0 To _
         GridToExport.Cols - 1
         GridToExport.col = iNumCols
         'if it isn't the first column,
         'put a delimiter before the value
         If iNumCols > 0 Then
            Print #iFileNumber, Delimiter;
         End If
         Print #iFileNumber, EncloseStrings & _
            GridToExport.Text & EncloseStrings;
      Next iNumCols
      Print #iFileNumber, "" 
   Next iNumRows
   Close #iFileNmuber
End Sub

Private Sub cmdExport_Click()
   Call GridExport(MSFlexGrid1, "c:/test.csv", ",", Chr$(34))
End Sub

Private Sub Form_Load()
   MSFlexGrid1.AddItem "Sam Huggill"
   MSFlexGrid1.AddItem "VB Square"
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now