Printing.Datagrid. problem stopping loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daniel aristidou
    Contributor
    • Aug 2007
    • 494

    Printing.Datagrid. problem stopping loop

    Hi i wrote code to print records off a datagrid.the code works on all but one of my data grids. The problem is that loop continues without stopping, Causing the program to crash.
    The only diff between all of the grids is the query and the widows form it is on.
    I basicly just copied the form changing the SQL for the datasource.
    any way so here is the code i typed:
    Code:
    Private Sub Print_Click()
    Dim msg As Variant
    Dim spaceaftercolumn1 As Variant
    Dim spaceaftercolumn2 As Variant
    Dim Spaceaftercolumn4 As Variant
    Dim Spaceaftercolumn5 As Variant
    Dim Spaceaftercolumn8 As Variant
    msg = "Are You Sure You Want to Print All Of The Records?"
     If MsgBox(msg, vbOKCancel, "Print All") = vbOK Then
     
        frmallrecords.grdDataGrid.Row = 0
        Printer.Orientation = 2
        Printer.Font = "Courier New"
        Printer.FontSize = 10
        Printer.FontUnderline = True
        Printer.FontBold = True
        Printer.Print Me.Caption & Space(1) & "-" & Space(1) & Date;
        Printer.Print
        Printer.Print
        Printer.Print "Company" & Space(43) & "First Name" & Space(4) & "Tel" & Space(7) & "Mob" & Space(7) & "Last Seen" & Space(3) & "Today?"
        Printer.Font = "Courier New"
        Printer.FontSize = 10
        Printer.FontUnderline = False
        Printer.FontBold = False
    
            Do While Not frmallrecords.grdDataGrid.Row = [B]datPrimaryRS.Recordset.RecordCount - 1[/B]
    
                spaceaftercolumn1 = 50 - Len(frmallrecords.grdDataGrid.Columns(1))
                spaceaftercolumn2 = 14 - Len(frmallrecords.grdDataGrid.Columns(2))
                Spaceaftercolumn4 = 10 - Len(frmallrecords.grdDataGrid.Columns(4))
                Spaceaftercolumn5 = 10 - Len(frmallrecords.grdDataGrid.Columns(5))
                Spaceaftercolumn8 = 12 - Len(frmallrecords.grdDataGrid.Columns(8))
    
                Printer.Print frmallrecords.grdDataGrid.Columns(1) & Space(spaceaftercolumn1) & frmallrecords.grdDataGrid.Columns(2) & Space(spaceaftercolumn2) & frmallrecords.grdDataGrid.Columns(4) & Space(Spaceaftercolumn4) & frmallrecords.grdDataGrid.Columns(5) & Space(Spaceaftercolumn5) & frmallrecords.grdDataGrid.Columns(8) & Space(Spaceaftercolumn8) & "______"
    
                frmallrecords.grdDataGrid.Row = frmallrecords.grdDataGrid.Row + 1
                Loop
    
        spaceaftercolumn1 = 50 - Len(frmallrecords.grdDataGrid.Columns(1))
        spaceaftercolumn2 = 14 - Len(frmallrecords.grdDataGrid.Columns(2))
        Spaceaftercolumn4 = 10 - Len(frmallrecords.grdDataGrid.Columns(4))
        Spaceaftercolumn5 = 10 - Len(frmallrecords.grdDataGrid.Columns(5))
        Spaceaftercolumn8 = 12 - Len(frmallrecords.grdDataGrid.Columns(8))
    
        Printer.Print frmallrecords.grdDataGrid.Columns(1) & Space(spaceaftercolumn1) & frmallrecords.grdDataGrid.Columns(2) & Space(spaceaftercolumn2) & frmallrecords.grdDataGrid.Columns(4) & Space(Spaceaftercolumn4) & frmallrecords.grdDataGrid.Columns(5) & Space(Spaceaftercolumn5) & frmallrecords.grdDataGrid.Columns(8) & Space(Spaceaftercolumn8) & "______"
        Printer.Print
        Printer.EndDoc
    End If
    I highlited (Bold) the line which seems to be making the problem.I have no solution as to why it is going wrong only on this datagrid when it works fine on all the others.
    Can anyone suggest alternative code for that bit?
    Another solution i thought of is since the error only occurs when there is no query ie Where statement in the datasource. Does any one know SQL that applies The where function *? i tried this
    Code:
    select * From Cutomers Where Customers.city = "*" Order by customers.Date_last_seen
    However this did not work so i removed the Where Customers.city = "*"
    Thank you for any help.!!!
  • daniel aristidou
    Contributor
    • Aug 2007
    • 494

    #2
    Anyone got any suggestions? or know a different way to print drawing the info straight from the datasource instead of from the datagrid?

    Comment

    • daniel aristidou
      Contributor
      • Aug 2007
      • 494

      #3
      Hi all who viewed this.
      After allot of trial and error and breakpoints.
      I realised the problem only occured when run all at once, if i broke the procedure after every loop the problem did not occur.
      Thus i realised that there was a problem with link between datagrid and the ado datasource.
      so i tought maybe the time link is available for is not long enough. or that it was not cacheing enough records at once.

      I then realized that the datagrid is conrolled directly by the ado.
      Thus i could change the "When do not" and go to next record code.
      Result 100% Success!!!
      Thanks anyone who tried to help me.
      The correct code is below if anyone experienced the same errors.

      Code:
      Private Sub Print_Click()
      Dim msg As Variant
      Dim spaceaftercolumn1 As Variant
      Dim spaceaftercolumn2 As Variant
      Dim Spaceaftercolumn4 As Variant
      Dim Spaceaftercolumn5 As Variant
      Dim Spaceaftercolumn8 As Variant
      msg = "Are You Sure You Want to Print All Of The Records?"
       If MsgBox(msg, vbOKCancel, "Print All") = vbOK Then
       
          [B]datprimaryrs.recordset.movefirst[/B]
          Printer.Orientation = 2
          Printer.Font = "Courier New"
          Printer.FontSize = 10
          Printer.FontUnderline = True
          Printer.FontBold = True
          Printer.Print Me.Caption & Space(1) & "-" & Space(1) & Date;
          Printer.Print
          Printer.Print
          Printer.Print "Company" & Space(43) & "First Name" & Space(4) & "Tel" & Space(7) & "Mob" & Space(7) & "Last Seen" & Space(3) & "Today?"
          Printer.Font = "Courier New"
          Printer.FontSize = 10
          Printer.FontUnderline = False
          Printer.FontBold = False
      
              Do While Not [B]datprimaryrs.recordset.eof[/B]
                  spaceaftercolumn1 = 50 - Len(frmallrecords.grdDataGrid.Columns(1))
                  spaceaftercolumn2 = 14 - Len(frmallrecords.grdDataGrid.Columns(2))
                  Spaceaftercolumn4 = 10 - Len(frmallrecords.grdDataGrid.Columns(4))
                  Spaceaftercolumn5 = 10 - Len(frmallrecords.grdDataGrid.Columns(5))
                  Spaceaftercolumn8 = 12 - Len(frmallrecords.grdDataGrid.Columns(8))
      
                  Printer.Print frmallrecords.grdDataGrid.Columns(1) & Space(spaceaftercolumn1) & frmallrecords.grdDataGrid.Columns(2) & Space(spaceaftercolumn2) & frmallrecords.grdDataGrid.Columns(4) & Space(Spaceaftercolumn4) & frmallrecords.grdDataGrid.Columns(5) & Space(Spaceaftercolumn5) & frmallrecords.grdDataGrid.Columns(8) & Space(Spaceaftercolumn8) & "______"
      
                  [B]datprimaryrs.recordset.movenext[/B]
                  Loop
      
          Printer.EndDoc
      End If

      Comment

      Working...