how to print all data from datagrid to printer??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hwkong1688
    New Member
    • Dec 2011
    • 7

    how to print all data from datagrid to printer??

    Hi,this is my code for print data to my default printer,but not for all data.

    1.How to print all data from datagrid to printer?

    Code:
    Private Sub Command2_Click()
    
    Dim RRow As Integer
    Dim Colomn1 As Integer
    
    DataGrid1.row = 0
    DataGrid1.Col = Colomn1
    For Colomn1 = 0 To DataGrid1.Columns.Count - 1
    DataGrid1.Col = Colomn1
    Debug.Print DataGrid1.Text
    
    Next
    
    Colomn1 = 0
    DataGrid1.Col = Colomn1
    For RRow = 0 To 5
    DataGrid1.row = RRow
    For Colomn1 = 0 To DataGrid1.Columns.Count - 1
    DataGrid1.Col = Colomn1
    Printer.Print DataGrid1.Text
    Next
    Next
    
    Printer.EndDoc
    
    End Sub
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Not sure what you mean. What does this code not print, that you want it to?

    Comment

    • hwkong1688
      New Member
      • Dec 2011
      • 7

      #3
      sorry make you confused, my question is how to print all data from datagrid to my default printer?

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by hwkong1688
        sorry make you confused, my question is how to print all data from datagrid to my default printer?
        All you've done is repeat the original question. It doesn't give us enough information to work with.

        We need more detail to understand exactly what you want from us. You've shown us code which prints data to your default printer. As far as I can tell, you already have exactly what you're asking for. What is it about this code that you're not happy with? What does this code not do, that you want it to? In what way does your existing code not "print all data from datagrid to my default printer"? Please explain.

        Comment

        • hwkong1688
          New Member
          • Dec 2011
          • 7

          #5
          Sorry my english not so well could be make you don't understand want i mean. you can check back my first post #1 line 16 [For RRow = 0 to 5] this is can only print 6 data from my datagrid to my default printer. How to print all data to my printer. This is my question?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Can't you apply the same technique used in line 18, but using Rows.Count instead of Columns.Count?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32668

              #7
              Thread moved from the Insights forum to the Answers forum. Please be more careful in future where you post your questions to (This is certainly not an Insight).

              Comment

              • hwkong1688
                New Member
                • Dec 2011
                • 7

                #8
                Sorry noted NeoPa,When i changed Columns.Count to Rows.Count i got this error msg: Compile error:invalid qualifer

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Damn, how do you determine the number of rows (or should that be "records") in a datagrid? I haven't worked with them in years.

                  Anyone?

                  Comment

                  • Guido Geurs
                    Recognized Expert Contributor
                    • Oct 2009
                    • 767

                    #10
                    this is how to collect data from a datagrid

                    Code:
                    Option Explicit
                    '§ add reference= Microsoft ActiveX Data Object 2.8 Library
                    '§ C:\Program Files\Common Files\system\ado\msado15.dll
                    
                    Dim ADO_RECSET As ADODB.Recordset '§ Create a Recordset
                    
                    Private Sub Form_Load()
                       Set ADO_RECSET = New ADODB.Recordset
                       ADO_RECSET.CursorLocation = adUseClient
                       ' Add columns to the Recordset
                       ADO_RECSET.Fields.Append "Key", adInteger
                       ADO_RECSET.Fields.Append "Field1", adVarChar, 40, adFldIsNullable
                       ADO_RECSET.Fields.Append "Field2", adDate
                       ' Open the Recordset
                       ADO_RECSET.Open , , adOpenStatic, adLockBatchOptimistic
                       ' Add data to the Recordset
                       ADO_RECSET.AddNew Array("Key", "Field1", "Field2"), _
                          Array(1, "string1", Date)
                       ADO_RECSET.AddNew Array("Key", "Field1", "Field2"), _
                          Array(2, "string2", #1/1/2000#)
                       ' Populate the Data in the DataGrid
                       Set DataGrid1.DataSource = ADO_RECSET
                    End Sub
                    
                    Private Sub ComPrint_Click()
                    Dim FIELDSidx As Integer
                    Dim ITEMidx As Integer
                    Dim DATA As String
                       ADO_RECSET.MoveFirst
                       Do Until ADO_RECSET.EOF = True
                          DATA = DATA & vbCrLf
                          For ITEMidx = 0 To ADO_RECSET.Fields.Count - 1
                             DATA = DATA & ADO_RECSET.Fields.Item(ITEMidx).Value & " - "
                          Next
                          ADO_RECSET.MoveNext
                       Loop
                       Text1.Text = DATA
                    End Sub
                    Instead of putting the data in the textbox, send it to the printer.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Seems as though the whole recordset creation thing is rather clouding the issue here.

                      If I'm not mistaken, the point you're making is that there is no record count, and one must simply loop through all the records. Correct?

                      Comment

                      • Guido Geurs
                        Recognized Expert Contributor
                        • Oct 2009
                        • 767

                        #12
                        Yes, there are different types of grids in VB but for the datagrid like in your call, the way to read the records is to go to the first one, read it, go to the next one, ... until EOF. (see demo attached)
                        Attached Files

                        Comment

                        Working...