Solved

Printing Grid

Posted on 2009-04-28
300 Views
How can i print 2 or more columns in grid?
My code below will only print 1 column
Thanks
``````Private Sub Command11_Click()

Printer.Print ""

Printer.Print ""

Printer.Print ""

Printer.Print ""

Printer.Print GRID.TextMatrix(GRID.Row, 0); GRID.TextMatrix(GRID.Row, 1); GRID.TextMatrix(GRID.Row, 2)

Printer.EndDoc

End Sub
``````
0
Question by:Whing Dela Cruz

LVL 15

Assisted Solution

Try this...

Printer.Print GRID.TextMatrix(GRID.Row, 0)
Printer.Print GRID.TextMatrix(GRID.Row, 1)
Printer.Print GRID.TextMatrix(GRID.Row, 2)
0

LVL 13

Assisted Solution

TRY SUMTHING LIKE THIS...

Private Sub PrintGrid(ctlGrid As Control)
Dim I As Integer
Dim J As Integer
Dim PTab As Integer

With ctlGrid
PTab = 20 'Set the first tab value

For J = 0 To .Columns.Count - 1
.Col = J 'Set the current column

'Send the field to the print line and add the tab.
'Notice the semicolon at the end of the line.
Printer.Print Trim\$(.Columns(J).Caption); Tab(PTab);

PTab = PTab + 50 'Increment the tab value
Next
Printer.Print

For I = 0 To .ApproxCount - 1
PTab = 20 'Set the first tab value

'This checks to see if a page break is needed
If Printer.CurrentY + Printer.TextHeight(.Text) > Printer.ScaleHeight - 600 Then
Printer.NewPage
End If

For J = 0 To .Columns.Count - 1
.Col = J 'Set the current column

'Send the field to the print line and add the tab.
'Notice the semicolon at the end of the line.
Printer.Print Trim\$(.Text); Tab(PTab);

PTab = PTab + 50 'Increment the tab value
Next

'After each column has printed to the print line
'a Printer.Print statement without a semicolon will
'will send the line to the printer and start a new
'one.
Printer.Print

On Error Resume Next
.Row = .Row + 1 'Set the active row
On Error GoTo 0

Next I
End With
Printer.EndDoc
End Sub

Private Sub Command2_Click()
Call PrintGrid(DataGrid1)
End Sub
0

Author Comment

Hi! I tried it but a message say's "Variable not define"  DataGrid1
Thanks
0

LVL 13

Expert Comment

changed DataGrid1 with the name of your Grid...

0

Author Comment

Hi! another error message say's "Object doesn't  support this property or method
For J = 0 to .columns.count - 1
0

LVL 13

Expert Comment

check this link as well to find more example...

http://www.experts-exchange.com/simpleSearch.jsp

i hope i can give u idea...

game-master
0

LVL 15

Expert Comment

Did you try my comment?
0

Author Comment

yes sir, its working.. but its only print in one column.  How to print more than 2 columns?

0

LVL 15

Expert Comment

Do you mean somthing like this output?

ColumnVAlue1         ColumnVAlue2                   ColumnVAlue3
ColumnVAlue1         ColumnVAlue2                   ColumnVAlue3
ColumnVAlue1         ColumnVAlue2                   ColumnVAlue3

Then Try this one:

Printer.Print GRID.TextMatrix(GRID.Row, 0) & vbTab & GRID.TextMatrix(GRID.Row, 1) & vbTab & Printer.Print GRID.TextMatrix(GRID.Row, 2)

0

Author Comment

okey thanks! i will try it now...

0

LVL 15

Expert Comment

ooppss... it should be like this

Printer.Print GRID.TextMatrix(GRID.Row, 0) & vbTab & GRID.TextMatrix(GRID.Row, 1) & vbTab & GRID.TextMatrix(GRID.Row, 2)
0

Author Comment

Hi! When I try Only the last line has printed. There are 3 columns or line in my grid but it only print one line.
What shall i do now?

grid.bmp
0

LVL 13

Expert Comment

For i = 0 To GRID.Rows - 1
Printer.Print GRID.TextMatrix(i, 0) & vbTab & GRID.TextMatrix(i, 1) & vbTab & GRID.TextMatrix(i, 2)
Next

that should work now..
0

LVL 15

Expert Comment

It should be like this:

Dim i as integer

For i = 0 To GRID.Rows.count - 1
Printer.Print GRID.TextMatrix(i, 0) & vbTab & GRID.TextMatrix(i, 1) & vbTab & GRID.TextMatrix(i, 2)
Next

This will print all the rows in your grid...
0

LVL 13

Expert Comment

i think that is what i have posted Jack..
0

Author Comment

Hi!  I tried it but an error appear like "Invalid qualifier"

"Rows is being highlighted"

For i = 0 To GRID.Rows.count - 1

0

LVL 13

Expert Comment

TRY MY CODE...
REMOVE THE .COUNT...

game-master
0

Author Comment

Hi! Thank you so much, I remove the .count now its working...
Can i ask some more questions? or do i need to post this to another question?

The grid.textMatrix(i, 2) does not align its other example of the result

1 pck    Paracetamol 500mg cap      1,900.00
2 bx      Chloramphenicol  500 cap           865.00
6 pcd   deras ssm                                        362.00

The problem is the alignment of the row
thanks!

0

LVL 13

Expert Comment

it is ok...

the alignment is unorganized because of the different length of the text...
u can align the result have some sort "cheating"....:-)

do sumthng like this...but im not quiet sure it works fine...

Printer.Print GRID.TextMatrix(i, 0) & Space(5) & vbTab & trim(GRID.TextMatrix(i, 1) ) & Space(5)  & vbTab & trim(GRID.TextMatrix(i, 2))

it means that ur adding 5 spaces on each text before the tab..

have some try...

game-master
0

LVL 15

Assisted Solution

>>Printer.Print GRID.TextMatrix(i, 0) & Space(5) & vbTab & trim(GRID.TextMatrix(i, 1) ) & Space(5)  & vbTab & trim(GRID.TextMatrix(i, 2))

This will produce the same result, even you put 5 spaces before the tabs...
0

LVL 15

Expert Comment

Any progress here?
0

Author Comment

Yes sir, the alignment of  GRID.TextMatrix(i, 0) and GRID.TextMatrix(i, 1) are exactly aligned
but i don't know the GRID.TextMatrix(i, 2) did not align
here's the result

1 pc   Dn Sample            15.00
2 pc   kmj sample2lko         98.00
3 pc   nm Sapmle            89.00

Thanks!
0

LVL 13

Accepted Solution

we have to set specific length of string on the matrix's second column...
let say the maximum length is 30 characters...

we have to do sumthing like this...

Dim i as integer
dim x as integer

For i = 0 To GRID.Rows - 1
x  = 30 - len(trim(GRID.TextMatrix(i, 1) ))
Printer.Print GRID.TextMatrix(i, 0) & Space(5) & vbTab & trim(GRID.TextMatrix(i, 1) ) & Space(x)  & vbTab & trim(GRID.TextMatrix(i, 2))

Next

this codes assumes that the allowable length of the matrix column 2 is 30.

game-master
0

Author Comment

i"ll try it now thanks

0

Author Comment

hi! sorry an error messages say like " invalid procedure call or argument"
0

LVL 15

Expert Comment

0

LVL 13

Expert Comment

on what part of the code shows the error?
0

Author Comment

Private Sub Command11_Click()
Dim i As Integer
Printer.Print ""
Printer.Print ""
Printer.Print ""
Printer.Print ""
Dim x As Integer

For i = 0 To grid.Rows - 1
x = 30 - Len(Trim(grid.TextMatrix(i, 1)))
Printer.Print grid.TextMatrix(i, 0) & Space(5) & vbTab & Trim(grid.TextMatrix(i, 1)) & Space(x) & vbTab & Trim(grid.TextMatrix(i, 2))
Next
Printer.EndDoc

End Sub
0

LVL 13

Expert Comment

good morning!

im sorry for the late reply bcoz i got sicked...

on what line on the codes shows the error?

game-master
0

Author Comment

Hi! Thanks, I hope that you are  in good condition right now..
I actually proceed to another project cause my problem on printing doesn't yet been solve.
i will recall it right now and chat you ASAP.
Thank you so much!
0

LVL 15

Expert Comment

May sakit daw? Ows...
0

Author Comment

Hi! I'm using this code
For i = 0 To grid.Rows - 1
x = 30 - Len(Trim(grid.TextMatrix(i, 1)))
Printer.Print grid.TextMatrix(i, 0) & Space(5) & vbTab & Trim(grid.TextMatrix(i, 1)) & Space(x) & vbTab & Trim(grid.TextMatrix(i, 2))
Next

As i observed upon printing when grid.TextMatrix(i, 1) is composed of long character the alignment will become disorder. But if the character is almost the same length then the printing alignment will have a good alignment.

I tried it many times but still not working.. I hope you can give more ideas
Thanks!

0

Author Comment

Hello jackofPh!
saan ka sa pilinas? Poyde mo akong matulungan sa problemang ito!
0

LVL 15

Expert Comment

Dito sa munoz, nakatira ako sa cubao, ikaw?
0

Author Comment

Cebuano po ako! Sa mindanao
Gusto po akong mag pa tutor ng vb.net na maybayad. maari ba kitang ma kuha na magtutor sa skin kahit sa email lang. Mag online po ako sa iyo ng pera, bigay mo lang sa akin ang accnt no. mo. Poyde ma send mo sa akin ang cel no. mo.
Thanks!
0

LVL 15

Expert Comment

Let me think first...
0

Author Comment

Okey,  Thanks!
0

LVL 13

Expert Comment

any progress?
0

Author Comment

yes sir, same problem!
Can you help me now?
0

LVL 13

Expert Comment

unfortunately, that is somehow the limitation of printer.print...
specially with regards to alignment...

as what i have said before, in order to make the alignment correct,
you should set for the specific length of string on each each column..

otherwise, alignment may not look the way as u want....

game-master
0

Featured Post

Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations. Â Recently, I solved an EE question using the LIKE function. Â In order for thâ€¦
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate rowâ€¦
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). Uâ€¦
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This lâ€¦