Solved

Printing in columns

Posted on 2000-02-21
8
273 Views
Last Modified: 2008-02-26
I'm trying to print a series of columns of data out, only I'm having a few problems.

I first tried using something like:
dummy$=field1$+field2$+..
printer.print Dummy$

The problem was that the non-uniformity in the physical print size of the various fields (even though they were all the same character width) meant that things didn't line up correctly.

I then tried loading the various columns into list boxes on a new form, and then used:
formname.printform

I got an error message telling me that my printer "didn't support rastering" (I'm using an Epsom Stylus 740, under Win98 with VB6)

The documentation that came with VB6 is blummin' terrible (I'm sure it's OK if you already know what you're doing), so any help would be greatly appreciated...

PoRL
0
Comment
Question by:PoRL
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2544404
Have you tried the tab() function??

Something like:

dummy$=field1$+field2$+..
printer.print field1$; Tab(10); field2$; Tab(40); field3$



Cheers!



0
 
LVL 14

Expert Comment

by:mcrider
ID: 2544410
That's bizzare... I never typed "dummy$=field1$+field2$+.." in my response above, yet it showed up... Expert's exchange acting up again??? Anyhow, my answer should have looked like this:


Have you tried the tab() function??

Something like:

printer.print field1$; Tab(10); field2$; Tab(40); field3$



Cheers!
0
 
LVL 12

Accepted Solution

by:
mark2150 earned 50 total points
ID: 2544665
Tabbing is fairly unreliable.

Although somewhat complex, Printer.CurrentX/Y is best way to handle. Use the printer.Scale command to set the dimensions of the logical printer to match your physical paper printable area. Then use the printer.CurrentX and Y commands to move the print cursor to exactly where you want it. You can then print in a proportional font and even do nice things like right align or center. Here is a sample print routine (off the top of my head:

Const Col1 as Double = 1.0 ' Left aligned col location
Const Col2 as Double = 4.0 ' Center aligned col location
Const Col3 as Double = 8.0 ' Right aligned col location
Dim OldY as Double ' Position down from top
Dim Work as String ' Working String
'
Printer.Scale (0,0)-(8,10.5) ' 8-1/2x11 less 1/4" all round for printable region
'
' Assuming you have gotten database recordset open at this point
'
do while not rs.eof
  oldy = printer.currenty
  printer.currentx = col1
  printer.print rs!field1 ' Left aligned
'
  Printer.CurrentY = oldy
  work = trim("" & RS!field2) ' Get 2nd fields data into a var
  printer.CurrentX = col2 - (printer.TextWidth(work)/2) 'Position to center
  Printer.Print work
'
  Printer.CurrentY = oldY
  work = trim("" & RS!Field3) ' 3rd field
  Printer.CurrentX = col3 - printer.textwidth(work)
  Printer.Print Work ' Right aligned
'
  RS.MoveNext  ' Advance to next record
Loop
'
Printer.EndDoc

This will give you a nice three column layout. Is the "right" way to print under VB. Using SPACE() or TAB() is hangover from DOS and will produce erratic results as users change fonts/printers. By using absolute positioning, no matter what printer or font is selected print positions will come out *exactly* the same.

M
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2545172
There is ABSOLUTELY NOTHING wrong with using TAB function.  It is NOT unreliable.  It is a standard function that you can EASILY use to print columns.

Here is the help page from Microsoft on the Tab Function:





TAB Function
------------

Used with the Print # statement or the Print method to position output.

Syntax

Tab[(n)]

The optional n argument is the column number moved to before displaying or printing the next expression in a list. If omitted, Tab moves the insertion point to the beginning of the next print zone. This allows Tab to be used instead of a comma in locales where the comma is used as a decimal separator.

Remarks

If the current print position on the current line is greater than n, Tab skips to the nth column on the next output line. If n is less than 1, Tab moves the print position to column 1. If n is greater than the output line width, Tab calculates the next print position using the formula:

n Mod width

For example, if width is 80 and you specify Tab(90), the next print will start at column 10 (the remainder of 90/80). If n is less than the current print position, printing begins on the next line at the calculated print position. If the calculated print position is greater than the current print position, printing begins at the calculated print position on the same line.

The leftmost print position on an output line is always 1. When you use the Print # statement to print to files, the rightmost print position is the current width of the output file, which you can set using the Width # statement.

Note   Make sure your tabular columns are wide enough to accommodate wide letters.

When you use the Tab function with the Print method, the print surface is divided into uniform, fixed-width columns. The width of each column is an average of the width of all characters in the point size for the chosen font. However, there is no correlation between the number of characters printed and the number of fixed-width columns those characters occupy. For example, the uppercase letter W occupies more than one fixed-width column and the lowercase letter i occupies less than one fixed-width column.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:PoRL
ID: 2545803
The final paragraph seems to be the problem. I don't know (obviously) exactly what data will be put into the thing. It might be "Wu" and "Popadopolous"..

mark2150 suggests that the printer.textwidth might be useful in determining the *maximum* with of text in a column (if I loop through all of the data entries), but is there anyway to use that information to set the TAB(n) function correctly??

Finally, does anybody have any ideas why PrintForm doesn't work?? It's not as if my Epsom is an old printer..

PoRL
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2545845
The final paragraph is indeed why TAB is "unreliable". This is especially true if you're trying to right align text. The statement:

Printer.Print Tab(80 - len(MyString); Mystring

Which will produce nice right aligned stuff under DOS with a monospaced printer font, will completely mess up under windows using proportional fonts.

If you want nicely aligned columns as the text width wanders around, my method is much superior to TAB().

PoRL, the TextWidth property will determine the width of *any* item. You don't have to search for the maximum. When you implement my loop as shown, it will properly right align all values in 'Field3'

As for printform, this is not a really good way to generate printouts. It's basically the equivalent of doing an <Alt/Prt Sc> where you'ld screen dump to the printer under DOS. This will produce an image of the screen, but the size will not be consistent on systems with different screen or printer resolutions. Say your printer prints at both 300 DPI and 600 DPI, you get it looking nice at 300 DPI and then your user switches to 600 and the printout shrinks to 1/4 the area! Not good.

M
0
 

Author Comment

by:PoRL
ID: 2546213
I couldn't get the "Tab()" to do what I wanted at all, so used mark2150's method of "positioning" the printhead, so I'm afraid (mcrider) he gets the points.

Incidentally, the only problem I had was setting the scale of the thing correctly using printer.ScaleMode. I tried setting this to 6 (i.e. millimeters) but what should have been a 10mm increase in width turned out to about about 13mm! Although I couldn't suss out why this was, I was able to adjust my column widths so everything came as I wanted.

Thanks for your help!


PoRL
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2546253
Don't use the .ScaleMode. If you just set the .Scale to whatever units you like then .ScaleMode will automatically change to "user".

I'm not sure the exact size of A4 paper, but it's around 212mm wide by 275mm high. So you could set the .Scale to (0,0)-(200,267) and then instead of Col1 = 1, Col2=4 and Col3 = 8 you could have Col1=25, Col2 = 100 and Col3=200 and all would work same-same.

M
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now