Anda di halaman 1dari 7

Tutorial de impresin de DbGrid de Gino Degiorgio angdeg@orbit.net.mt impresin en visual basic puede ser complicado y confuso para el principiante.

Temas de impresin de libros son comunes, pero generalmente son demasiado genricos y superficiales. Pens que un detallado tutorial sobre impresin un dbgrid (o una cuadrcula) podra ser til a alguien. Sugiero que uno debe hacer alguna lectura sobre la impresin antes de intentar leer esta pgina, especialmente si el lector no tiene ninguna experiencia en la impresin. Tambin se supone cierta experiencia de programacin tales como la creacin de bucles. Un buen captulo sobre la impresin puede ser obtenido de este sitio: impresin con Visual Basic (de Teach Yourself Visual Basic 5 en 24 horas por Greg Perry).

Crear un formulario con un control dbgrid, un control de datos y un botn de comando. Conectar el control de datos a una base de datos (utilice la propiedad DataSource: autores y propiedad DatabaseName: Biblio.mdb). Ttulo del botn de nombre de comando "Imprimir". Establezca el formulario a su voluntad. Hacer slo unas 10 filas slo visibles para las futuras de las manifestaciones. Como la tercera columna "Ao Born" estn casi todos vacos, introduzca algunas fechas (1990, 1991,1992, y as sucesivamente a tener algunos datos en la tercera columna "Ao De nacimiento" One puede eliminarlos ms adelante.). No se olvide de recuperar los campos en el dbgrid haciendo clic derecho sobre el el dbgrid y elija los campos de recuperacin. El formulario debe parecerse figura uno.

Bien, entonces vamos . Considere la posibilidad de la cuadrcula como un nmero de cuadros de texto que se unieron. Cuando uno imprime un cuadro de texto, funciona el siguiente comando

Private Sub Command1_Click() Printer.Print Text1.Text End Sub Sin embargo uno tiene que declarar una posicin comenzar con, por ejemplo: 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 Printer.CurrentY = 10 DBGrid1.Col = 0 DBGrid1.Row = 0 Printer.Print DBGrid1.Text Printer.EndDoc End Sub declare position of X declare position of Y Select first row to be printed Select first column to be printed

Intente imprimir esto. Usted encontrar que el "1" se imprime en la posicin que ha solicitado. Tenga en cuenta la propiedad de modo de escala ahora es milmetros y ha sido declarada. Ahora tenemos que imprimir la siguiente palabra y la siguiente. Una vez ms tenemos que declarar nuevas posiciones y nuevas clulas. Este proceso puede ser confuso, pero no renunciar a an. Mira de cerca y tratar de comprender este cdigo:

Private Sub Command1_Click() Printer.ScaleMode = 6 Printer.CurrentX = 10 Printer.CurrentY = 10 DBGrid1.Col = 0 DBGrid1.Row = 0 'declare measurement units 'declare position of X 'declare position of Y 'identify column number to print 'identify row number to print

Printer.Print DBGrid1.Text DBGrid1.Col = 1 if it is the same. Printer.CurrentX = 15 Printer.CurrentY = 10 repeated. Printer.Print DBGrid1.Text DBGrid1.Col = 2 repeat row Printer.CurrentX = 50 Printer.CurrentY = 10 repeated. Printer.Print DBGrid1.Text Printer.EndDoc End Sub 'identify again next column number to print, no need to 'identify column number to print, no need to repeat row

'declare new position of Y 'declare same position of Y. Note that Y position is

'declare new position of Y 'declare same position of Y. Position of Y is to be

De acuerdo, intente imprimir esta ahora y ver el resultado. Pues bien, tenemos la primera lnea. Tenga en cuenta que si uno no se repita la posicin del eje Y, el comando de Printer.Print crea una nueva lnea. Por lo tanto, si queremos imprimir cerca de la siguiente palabra anterior y permanecer en la misma lnea, tenemos que especificar el eje Y otra vez. Pruebe y quitar el eje Y como experimentar por s mismo. Ahora vamos para que ms lneas imprimir. Se utilizar el siguiente bucle de... de for. Examine atentamente el siguiente cdigo. Nota cmo el nmero de fila, el nmero de valor Y se incrementan cada vez que el bucle repite.

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 DBGrid1.Col = 0 DBGrid1.Row = 0 Printer.Print DBGrid1.Text ' DBGrid1.Col = 1 Printer.CurrentX = 15 Printer.CurrentY = 10 Printer.Print DBGrid1.Text

'declare first position of Y 'identify column number to print 'identify row number to print

'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 'identify column number to print Printer.CurrentX = 15 'declare position of Y Printer.CurrentY = 10 + IncreaseY 'declare new position of Y Printer.Print DBGrid1.Text DBGrid1.Col = 2 Printer.CurrentX = 50 Printer.CurrentY = 10 + IncreaseY Printer.Print DBGrid1.Text Next i Printer.EndDoc End Sub Espero que el cdigo anterior se explica por s mismo y no ests perdido. Qu pasa si quiero imprimir las filas visibles en la pantalla. Fcil, modificar el cdigo anterior como sigue: 'identify column number to print 'declare new position of Y 'declare position of Y

For i = 1 To DBGrid1.VisibleRows Ahora, intente imprimir filas de ms de 12, que es de filas que no son visibles en la pantalla. Prubalo y nota se muestra el nmero de error de 6148 indicando un nmero de fila no vlido. A travs de esto, modificar el cdigo en el bucle For Next como sigue; cuando el error se muestra que hay un nmero de fila no vlido

'Print NINE more Rows For i = 1 To 20 'or use DBGrid1.VisibleRows to print all the visible rows 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

En nuestro caso, X 1 es 10 menos 1, para imprimir un milmetro antes de la palabra, y eso va para X 2. El valor de X no se cambia en una lnea vertical, no. Ahora Y1 es 10, lo mismo que el CurrentY (valor inicial de Y) y Y2 es 10 + 20 * 5 (10, el valor inicial de Y, ms de 20, el nmero de filas para imprimirse veces y 5, recuerde que la anchura de la fila impresa, el incremental de Y.) Poner y modificar las lneas verticales en la palabra de segunda y tercera. Por ltimo nos permite ir a la lnea horizontal. Tenemos que crear otro bucle aqu. Mirar cuidadosamente este cdigo.

'Print Horizontal Lines Dim j As Integer draw horizontal line Dim Counter As Integer 'to be used for the ForNext Loop to 'to increase the value of Y

For j = 1 To 20 Counter = Counter + 5 Printer.Line (9, 5 + Counter)-(85, 5 + Counter)

Next j Uno tiene que ser paciente aqu y calcular posiciones como Printer. Line el deseado sigue las mismas reglas para las lneas verticales. Bien el tutorial est terminado. Ahora utilizar su cerebro y ajustar y modificar a su voluntad. El concepto detrs de este tutorial es dar una idea para el principiante confundido cmo imprimir un dbgrid. Espero que lo hice no confundir an ms despus de todos que me un novato en VB de programacin aqu es el cdigo final:

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 If Err.Number = 6148 Then Data1.Recordset.MoveNext End If

'identify row number to print

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

El proyecto de ejemplo imprime 9 filas. NB: si alguien sabe de un mejor o una forma ms eficiente para imprimir una cuadrcula de tal, por favor enviarme un correo electrnico. Gracias

Anda mungkin juga menyukai