You are on page 1of 6

DbGrid Printing Tutorial by Gino Degiorgio

angdeg@orbit.net.mt Printing in visual basic can be a hassle and confusing to the beginner. Printing topics on books are common but usually they are too generic and superficial. I thought that a detailed tutorial regarding printing a dbgrid (or a grid) might be useful to someone. I suggest that one should make some reading on printing before attempting to read this page especially if the reader has no experience in printing. Some programming experience such as creating loops is also assumed. A good chapter on printing can be obtained from this site: Printing with Visual Basic (from Teach Yourself Visual Basic 5 in 24 Hours by Greg Perry). Figure 1 Build a Form with a dbgrid control, a data control, and a command button. Connect the data control to a database (use property Datasource: Authors and property Databasename: Biblio.mdb). Name command button caption Print. Set the form to your desire. Make only about 10 rows only visible for future demonstrations. As the third column Year Born are nearly all empty, enter some dates (1990,1991,1992, and so on to have some data in the third column Year Born One can remove them later.). Dont forget to retrieve the fields in the dbgrid by right click on the the dbgrid and choose Retrieve Fields . The form should look something like figure one. Ok, then lets go. Consider the grid as a number of text boxes joined together. When one prints a text box, the following command works: Private Sub Command1_Click() Printer.Print Text1.Text End Sub However one have to declare a position to start with, for example: Private Sub Command1_Click() Printer.CurrentX = 10 Printer.CurrentY = 10 Printer.Print Text1.Text End Sub Lets go to our dbgrid then: Private Sub Command1_Click() Printer.Scalemode = 6 declare units of measurement, i.e. millimeters Printer.CurrentX = 10 declare position of X Printer.CurrentY = 10 declare position of Y DBGrid1.Col = 0 Select first row to be printed DBGrid1.Row = 0 Select first column to be printed

Printer.Print DBGrid1.Text Printer.EndDoc End Sub Try to print out this. You will find that 1 is printed at the position you requested. Note the Scalemode property now is millimeters and has been declared. Now we have to print the next word and the next. Again we have to declare new positions and new cells. This process might be confusing but dont give up yet. Look closely and try to understand this code: Private Sub Command1_Click() Printer.ScaleMode = 6 'declare measurement units ' Printer.CurrentX = 10 'declare position of X Printer.CurrentY = 10 'declare position of Y DBGrid1.Col = 0 'identify column number to print DBGrid1.Row = 0 'identify row number to print Printer.Print DBGrid1.Text ' DBGrid1.Col = 1 'identify column number to print, no need to repeat row if it is the same. Printer.CurrentX = 15 'declare new position of Y Printer.CurrentY = 10 'declare same position of Y. Note that Y position is repeated. Printer.Print DBGrid1.Text DBGrid1.Col = 2 'identify again next column number to print, no need to repeat row Printer.CurrentX = 50 'declare new position of Y Printer.CurrentY = 10 'declare same position of Y. Position of Y is to be repeated. Printer.Print DBGrid1.Text Printer.EndDoc End Sub OK, try to print out this now and see the result. Well we have the first line. Note, that if one does not repeat the Y axis position, the Printer.Print command creates a new line. Therefore if we want to print near the next previous word and remain on the same line, we have to specify the Y axis again. Try and remove the Y axis as experiment for yourself. Now we go for more lines to print. The For....Next Loop will be used. Look closely at the following code. Note how the row number, the Y value number are increased every time the loop repeats itself. Private Sub Command1_Click() Dim i As Integer 'to be used for the for...next loop Dim R As Integer 'to be used to increment the row number Dim IncreaseY As Integer 'to be used to increase Y co-ordinate Printer.ScaleMode = 6 'declare measurement units Print First Row Printer.CurrentX = 10 'declare first position of X Printer.CurrentY = 10 'declare first position of Y DBGrid1.Col = 0 'identify column number to print DBGrid1.Row = 0 'identify row number to print Printer.Print DBGrid1.Text '

DBGrid1.Col = 1 Printer.CurrentX = 15 Printer.CurrentY = 10 Printer.Print DBGrid1.Text

'identify column number to print 'declare new position of Y 'declare position of Y

DBGrid1.Col = 2 'identify column number to print Printer.CurrentX = 50 'declare new position of Y Printer.CurrentY = 10 'declare position of Y Printer.Print DBGrid1.Text ' 'Print NINE more Rows, leave first row alone For i = 1 To 9 R=R+1 'row number by increase by one with every loop IncreaseY = IncreaseY + 5 'increaseY by 5 with every loop DBGrid1.Row = R 'identify row number to print Printer.CurrentX = 10 'declare same position of X Printer.CurrentY = 10 + IncreaseY 'declare new position of Y DBGrid1.Col = 0 'identify column number to print Printer.Print DBGrid1.Text ' DBGrid1.Col = 1 Printer.CurrentX = 15 Printer.CurrentY = 10 + IncreaseY Printer.Print DBGrid1.Text DBGrid1.Col = 2 Printer.CurrentX = 50 Printer.CurrentY = 10 + IncreaseY Printer.Print DBGrid1.Text Next i Printer.EndDoc End Sub Hope that the code above is self explanatory and you are not lost. What about if I want to print the visible rows on the screen. Easy, amend the above code as follows: For i = 1 To DBGrid1.VisibleRows Now, try to print more than 12 rows, that is rows that are not visible on the screen. Try it and note the error number of 6148 indicating an invalid row number is displayed. To over come this, amend the code in the ForNext loop as follows; when the error displays that there is an invalid row number CONT. 'Print NINE more Rows For i = 1 To 20 R=R+1 IncreaseY = IncreaseY + 5 ' 'or use DBGrid1.VisibleRows to print all the visible rows 'row number by increase one with every loop 'increaseY by 5 with every loop

'identify column number to print 'declare position of Y 'declare new position of Y 'identify column number to print 'declare new position of Y 'declare position of Y

On Error Resume Next DBGrid1.Row = R 'identify row number to print If Err.Number = 6148 Then Data1.Recordset.MoveNext End If Printer.CurrentX = 10 Printer.CurrentY = 10 + IncreaseY DBGrid1.Col = 0 CONT . This code causes the grid to scroll down. Ok, suppose we want to print the grid now. Well, let us start with the vertical lines. Vertical lines can be coded when the first row that was coded. Therefore to print the first vertical line give the code following code: Printer.Line (9, 10)-(9, 10 + 20 * 5) 'vertical line 1 just after the code the print the first word, in our case 1 is printed first word. Let give some explanation. The Line command takes these parameters: . (1,1)(22) In our case, X1 is 10 minus 1, to print one millimeter before the word, and that goes for X2. The value of X does not change in a vertical line, no. Now Y1 is 10, same as the CurrentY (initial value of Y) and Y2 is 10 +20*5 (10, the initial value of Y, plus 20, the number of rows to be printed times and 5, the width of the printed row, the incremental of Y remember.) Put and amend vertical lines in the second and third word. Finally lets us go to the horizontal line. We have to create another loop here. Look at this code carefully. CONT............. 'Print Horizontal Lines Dim j As Integer Dim Counter As Integer 'to be used for the ForNext Loop to draw horizontal line 'to increase the value of Y 'declare same position of X 'declare position of Y 'identify column number to print

For j = 1 To 20 Counter = Counter + 5 Printer.Line (9, 5 + Counter)-(85, 5 + Counter) Next j CONT........... One has to be patient here and calculate positions as desired The Printer.Line follows the same rules for the vertical lines. Ok the tutorial is finished. Now use your brains and adjust and amend to your desire. The concept behind this tutorial is to give an idea to the confused beginner how to print a dbgrid. I hope that I did not confused further After all I just a newbie in VB programming Here is the final code: Private Sub Command1_Click() Printer.PrintQuality = -4

Dim i As Integer 'to be used for the for...next loop Dim R As Integer 'to be used to increment the row number Dim IncreaseY As Integer 'to be used to increase Y co-ordinate ' Printer.ScaleMode = 6 'declare measurement units ' Printer.CurrentX = 10 'declare position of X Printer.CurrentY = 10 'declare position of Y DBGrid1.Col = 0 'identify column number to print DBGrid1.Row = 0 'identify row number to print Printer.Print DBGrid1.Text Printer.Line (9, 10)-(9, 10 + 20 * 5) 'vertical line 1 ' DBGrid1.Col = 1 'identify column number to print Printer.CurrentX = 20 'declare new position of Y Printer.CurrentY = 10 'declare position of Y Printer.Print DBGrid1.Text Printer.Line (19, 10)-(19, 10 + 20 * 5) 'vertical line 2 ' DBGrid1.Col = 2 'identify column number to print Printer.CurrentX = 70 'declare new position of Y Printer.CurrentY = 10 'declare position of Y Printer.Print DBGrid1.Text Printer.Line (69, 10)-(69, 10 + 20 * 5) 'vertical line 3 Printer.Line (85, 10)-(85, 10 + 20 * 5) 'vertical line 4 ' 'Print NINE more Rows For i = 1 To 20 'or DBGrid1.VisibleRows R=R+1 'row number by increase one with every loop IncreaseY = IncreaseY + 5 'increaseY by 5 with every loop ' On Error Resume Next DBGrid1.Row = R 'identify row number to print If Err.Number = 6148 Then Data1.Recordset.MoveNext End If Printer.CurrentX = 10 'declare same position of X Printer.CurrentY = 10 + IncreaseY 'declare position of Y DBGrid1.Col = 0 'identify column number to print Printer.Print DBGrid1.Text ' DBGrid1.Col = 1 'identify column number to print Printer.CurrentX = 20 'declare new position of Y Printer.CurrentY = 10 + IncreaseY 'declare position of Y Printer.Print DBGrid1.Text ' DBGrid1.Col = 2 'identify column number to print Printer.CurrentX = 70 'declare new position of Y Printer.CurrentY = 10 + IncreaseY 'declare position of Y Printer.Print DBGrid1.Text Next 'Print Horizontal Lines Dim j As Integer 'to be used to draw horizontal line

Dim Counter As Integer

'to increase the value of Y

For j = 1 To 20 Counter = Counter + 5 Printer.Line (9, 5 + Counter)-(85, 5 + Counter) Next j Printer.EndDoc End Sub The sample project prints 9 rows.

NB: if anyone knows a better or a more efficient way how to print such a grid, please send me an e-mail. Thank you

The End

You might also like