Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Help!! 2

Status
Not open for further replies.

ts2032

Technical User
Mar 26, 2002
115
US
Hello All,

I have a program that populates a listview control with four columns of data. The data comes from an access database and are Names, DOB, Address, and Room#. What I am trying to do is allow the user(my wife) to print a list of all of the children in the listview control. What I have done so far is.

for x=1 to listview1.listitem.count
cname=listview1.listitems(x)
dob=listview1.listitems(x).listsubitems(1)address=listview1.listitems(x).listsubitems(2)
room=listview1.listitems(x).listsubitems(3)

printer.print cname & vbtab & dob & vbtab & address & vbtab & room
next x

the problem I am having is that I want all of the columns to be lined up, but they are all "S" shaped.

Juan 04/13/2000 Wherever Street Green Room
Billy S 03/18/1999 Wherever Street Yellow Room
Sam 01/24/1999 Wherever Street Red Room

I know this is because the columns are filled with text of different lenghts. I have tried an If..Then.. statement dependent upon the len(cname)'length of string, but because some of the names contain a lot of iii's the string takes up fewer spaces than a shorter string made up of wider characters.

Any help is appreciated.

P.S. While not on the subject of databases. What is the diffence between using SQL queries and the VB methods to select from different catagories. I have seen some of the queries Select * From . . . something or other and I am still a bit confused. I just use the following type statements:

dim rs as recordset
dim db as database
set db=opendatabase("somedatabase")
set rs=db.openrecordset("Sometable")
do while not rs.eof
if rs.fields(0).value="What I am looking for" then "Do what I want"
rs.movenext
x=x+1
loop

works for me. Is SQL faster or is it just preference?

Thank you,

T. Smith
 
The quickest way I've found to align printed columns is to switch to a fixed-width font like Courier then use the Space function to pad each field to an equal length.

Printer.FontName = "Courier"
' Then within your For...Next loop
Printer.Print cname & Space(35 - Len(cname)) & dob & Space(10 - Len(dob) & address & Space(35 - Len(address)) & room & Space(15 - Len(room))


If you're not familiar with the Space function, it just generates the number of spaces you indicate as it's argument. As I've used it, it will add enough spaces to pad each field to the lengths 35, 10, 35 and 15 respectively. You've either got to pick a length you know in advance will work or first iterate through your recordset and find the longest string, add a couple of spaces and use that number.

The problem is, if the machine your program is running on doesn't have the same font, you've either got to have the user pick a fixed-width font or it will default to something else.. probably true type.

A more reliable but tedious method is to use the .CurrentX and .CurrentY properties of the Printer object to set the position for each field.

SQL and your method are not mutually exclusive. You should use them together. Your method opens the entire recordset and iterates through it looking for matches. That wastes alot of time. You should open your recordset with an SQL statement using 'WHERE' to select only relevant records. SQL is MUCH faster at picking good records than using DAO the way you currently do. You still may need to iterate through your resulting recordset with a Do Until .EOF loop in order to do what you need to do but at least let SQL thin out the set first.

Set rs = db.OpenRecordset("SELECT * FROM Sometable WHERE YourField = 'What I am looking for'")

Or better yet, SELECT only the fields you really need instead of "*". This saves memory and (probably) time.
 
As Schroeder suggest, the use of a fixed font will certainly work.

Another approach is to user the Printer Object, where thru the use of the .PrinterX, and .PrinterY properties you can position the print head exactly where you want it to be.

Or you can use the Printer.TextWidth (<string>) to determine how much space a particular string will take up, and then append spaces until you reach the desired length. This way you append the correct number of spaces to reach the desired length, even tho use are using a proportional font.
Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
I'm very new to VB, I thought it was a lot simpler - why wont using a tab work if you set the tabs to be far enough apart that they dont overrun one another?

 
As far as I know, you aren't able to specify the width of a tab stop. You can add vbTab characters to the end of a string and they will bring you to a specific point regardless of the font width. The problem is, one row of text may be long enough to get you past tab-stop number 3, you add three vbTabs to the end and your next column will start at stop number 6. The next row of text may be longer and ends up past tab-stop 4. Add three more tabs and your next column is now at stop 7.
If it is possible to set individual tab widths, I'd be interested to learn how.
 
I don't know of a way to explicitly set tab stops, but you can simulate this using the .CurrentX property and an array.

Something like the following:

Dim CurrentTab as Integer

Dim PrinterTabs(5) as Integer

PrinterTabs(1) = 1000
PrinterTabs(2) = 2000
PrinterTabs(3) = 3000
...

CurrentTab = 1
Printer.CurrentX = PrinterTabs(CurrentTab)
<print some stuff>
CurentTab = CurrentTab + 1
Printer.CurrentX = PrinterTabs(CurrentTab)
<print some stuff>


Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
msdn says that you can set the tab(n) as an option to the print method such as:

cury=printer.currenty
printer.print tab(10);mytext
printer.currenty=cury
printer.print tab(20);&quot;more text&quot;

so that the first text would be indented to begin text at the 10th column and &quot;more text&quot; would begin at the 20th tab stop.

I have not tried it yet.
t.smith
 
T.S.

Your last posting does what you were asking. Try it. A simpler way would be&quot;

Printer.print myName; tab(30); myPhone; tab(50); myDOB

You got to make sure the tab size is larger than the longest text, or it will come out strange looking.


Jason
 
hey ts2032. i have been trying to do the same thing as you are doing but I can't ever get mine to work. so could you do me a favor and let me know the print sub you end up using. I would much appreciate this
 
If you define your variables like this it will allow the specified number of spaces for each column

Dim Number As String * 5
Dim ID As String * 10
Dim FirstName As String * 25
Dim LastName As String * 25
 
I use this in a loop

Code:
        Printer.Print Tab(25); VN;
        Printer.Print Tab(43); VT;
        Printer.Print Tab(60); Format(VQ, "#,###");
        Printer.Print Tab(70); Format(VL, "#,###")

Notice the ";" everywhere except the end of line.

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top