By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,992 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,992 IT Pros & Developers. It's quick & easy.

Printing.Datagrid. problem stopping loop

daniel aristidou
100+
P: 491
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Print_Click()
  2. Dim msg As Variant
  3. Dim spaceaftercolumn1 As Variant
  4. Dim spaceaftercolumn2 As Variant
  5. Dim Spaceaftercolumn4 As Variant
  6. Dim Spaceaftercolumn5 As Variant
  7. Dim Spaceaftercolumn8 As Variant
  8. msg = "Are You Sure You Want to Print All Of The Records?"
  9.  If MsgBox(msg, vbOKCancel, "Print All") = vbOK Then
  10.  
  11.     frmallrecords.grdDataGrid.Row = 0
  12.     Printer.Orientation = 2
  13.     Printer.Font = "Courier New"
  14.     Printer.FontSize = 10
  15.     Printer.FontUnderline = True
  16.     Printer.FontBold = True
  17.     Printer.Print Me.Caption & Space(1) & "-" & Space(1) & Date;
  18.     Printer.Print
  19.     Printer.Print
  20.     Printer.Print "Company" & Space(43) & "First Name" & Space(4) & "Tel" & Space(7) & "Mob" & Space(7) & "Last Seen" & Space(3) & "Today?"
  21.     Printer.Font = "Courier New"
  22.     Printer.FontSize = 10
  23.     Printer.FontUnderline = False
  24.     Printer.FontBold = False
  25.  
  26.         Do While Not frmallrecords.grdDataGrid.Row = datPrimaryRS.Recordset.RecordCount - 1
  27.  
  28.             spaceaftercolumn1 = 50 - Len(frmallrecords.grdDataGrid.Columns(1))
  29.             spaceaftercolumn2 = 14 - Len(frmallrecords.grdDataGrid.Columns(2))
  30.             Spaceaftercolumn4 = 10 - Len(frmallrecords.grdDataGrid.Columns(4))
  31.             Spaceaftercolumn5 = 10 - Len(frmallrecords.grdDataGrid.Columns(5))
  32.             Spaceaftercolumn8 = 12 - Len(frmallrecords.grdDataGrid.Columns(8))
  33.  
  34.             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) & "______"
  35.  
  36.             frmallrecords.grdDataGrid.Row = frmallrecords.grdDataGrid.Row + 1
  37.             Loop
  38.  
  39.     spaceaftercolumn1 = 50 - Len(frmallrecords.grdDataGrid.Columns(1))
  40.     spaceaftercolumn2 = 14 - Len(frmallrecords.grdDataGrid.Columns(2))
  41.     Spaceaftercolumn4 = 10 - Len(frmallrecords.grdDataGrid.Columns(4))
  42.     Spaceaftercolumn5 = 10 - Len(frmallrecords.grdDataGrid.Columns(5))
  43.     Spaceaftercolumn8 = 12 - Len(frmallrecords.grdDataGrid.Columns(8))
  44.  
  45.     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) & "______"
  46.     Printer.Print
  47.     Printer.EndDoc
  48. 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
Expand|Select|Wrap|Line Numbers
  1. 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.!!!
Oct 29 '07 #1
Share this Question
Share on Google+
2 Replies


daniel aristidou
100+
P: 491
Anyone got any suggestions? or know a different way to print drawing the info straight from the datasource instead of from the datagrid?
Oct 29 '07 #2

daniel aristidou
100+
P: 491
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Print_Click()
  2. Dim msg As Variant
  3. Dim spaceaftercolumn1 As Variant
  4. Dim spaceaftercolumn2 As Variant
  5. Dim Spaceaftercolumn4 As Variant
  6. Dim Spaceaftercolumn5 As Variant
  7. Dim Spaceaftercolumn8 As Variant
  8. msg = "Are You Sure You Want to Print All Of The Records?"
  9.  If MsgBox(msg, vbOKCancel, "Print All") = vbOK Then
  10.  
  11.     datprimaryrs.recordset.movefirst
  12.     Printer.Orientation = 2
  13.     Printer.Font = "Courier New"
  14.     Printer.FontSize = 10
  15.     Printer.FontUnderline = True
  16.     Printer.FontBold = True
  17.     Printer.Print Me.Caption & Space(1) & "-" & Space(1) & Date;
  18.     Printer.Print
  19.     Printer.Print
  20.     Printer.Print "Company" & Space(43) & "First Name" & Space(4) & "Tel" & Space(7) & "Mob" & Space(7) & "Last Seen" & Space(3) & "Today?"
  21.     Printer.Font = "Courier New"
  22.     Printer.FontSize = 10
  23.     Printer.FontUnderline = False
  24.     Printer.FontBold = False
  25.  
  26.         Do While Not datprimaryrs.recordset.eof
  27.             spaceaftercolumn1 = 50 - Len(frmallrecords.grdDataGrid.Columns(1))
  28.             spaceaftercolumn2 = 14 - Len(frmallrecords.grdDataGrid.Columns(2))
  29.             Spaceaftercolumn4 = 10 - Len(frmallrecords.grdDataGrid.Columns(4))
  30.             Spaceaftercolumn5 = 10 - Len(frmallrecords.grdDataGrid.Columns(5))
  31.             Spaceaftercolumn8 = 12 - Len(frmallrecords.grdDataGrid.Columns(8))
  32.  
  33.             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) & "______"
  34.  
  35.             datprimaryrs.recordset.movenext
  36.             Loop
  37.  
  38.     Printer.EndDoc
  39. End If
Oct 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.