Hi,

im doing a program in VB and i want to print my clients information.

But i dont want to print a printscreen of the form, i want to print a list of all my clients on a page design by me, i know that is possible to design the print page in acess but i dont know how to do it on VB and how to select by code, the number of clients that i want printed on the list.

Hi,

im doing a program in VB and i want to print my clients information.

But i dont want to print a printscreen of the form, i want to print a list of all my clients on a page design by me, i know that is possible to design the print page in acess but i dont know how to do it on VB and how to select by code, the number of clients that i want printed on the list.

well, there are different ways to use the printer; i don't really remember them off hand. the standard way isn't as easy as one would like, but it'll work...

to test this, and save on paper; i suggest that you create a picture box and print to it using the same method shown here; only difference is the EndDoc; don't use that with a picturebox

anyway, on to the code...

now, i'm assuming you have some kind of client list; so i thought i'd make one up just for a visual...

Private Type myCLIENTS
 stName as String * 32
 stHomePhone as string * 10
 stWhatever as string * 32
End Type

Private Clients() as myCLIENTS
Private iPage as Integer

I happen to like Types, you might notice this if you look at any of my other posts...
anyway, in my sample that i wrote and tested with, i hard coded the variables in a sub i called 'SET_CLIENTS'

Private Sub SET_CLIENTS()
 ReDim Clients(1 To 10)
 Clients(1).stName = "Joe Bob"
 Clients(1).stHomephone = "1111111111"
 Clients(1).stWhatever = "nice number"
 Clients(2).stName = "Jill Bob"
 Clients(2).stHomephone = "2222222222"
 Clients(2).stWhatever = "nice number"
 Clients(3).stName = "Betty Bob"
 Clients(3).stHomephone = "3333333333"
 Clients(3).stWhatever = "nice number"
 Clients(4).stName = "Billy Bob"
 Clients(4).stHomephone = "4444444444"
 Clients(4).stWhatever = "nice number"
 Clients(5).stName = "Brandy Bob"
 Clients(5).stHomephone = "5555555555"
 Clients(5).stWhatever = "nice number"
 Clients(6).stName = "Robby Bob"
 Clients(6).stHomephone = "6666666666"
 Clients(6).stWhatever = "nice number"
 Clients(7).stName = "Ricky Bob"
 Clients(7).stHomephone = "7777777777"
 Clients(7).stWhatever = "nice number"
 Clients(8).stName = "Joe Bob Sr"
 Clients(8).stHomephone = "8888888888"
 Clients(8).stWhatever = "nice number"
 Clients(9).stName = "Joe Bob Jr"
 Clients(9).stHomephone = "9999999999"
 Clients(9).stWhatever = "nice number"
 Clients(10).stName = "Bobby Sue"
 Clients(10).stHomephone = "1234567890"
 Clients(10).stWhatever = "nice number"
End Sub

yes, very creative i know... but I'm just trying to show how this works...
now, to print the page header... only need to do this once if you only have 1 page, but... you might have more...

Private Sub PRINT_HEADER()
 iPage = iPage + 1
 Dim sPrinterY As Single
 Dim sPrinterY2 As Single
 Printer.ScaleMode = vbInches
 Printer.CurrentX = 0.25 'set printer to 0.25 inches in on the page
 Printer.FontSize = 18
 Printer.FontName = "Arial"
 sPrinterY = Printer.CurrentY
 Printer.Print "Client List"
 sPrinterY2 = Printer.CurrentY 'yeah, this is really sloppy; 
                               'but it's just to get the different 
                               'height of font size 18
 Printer.CurrentY = sPrinterY
 Printer.FontSize = 10
 Printer.CurrentX = 7
 Printer.Print "Page " & Trim$(Str$(iPage))
 Printer.CurrentY = sPrinterY2
 Printer.FontSize = 10
 Printer.CurrentX = 0.25
 Printer.FontUnderline = True
 stestY = Printer.CurrentY
 Printer.Print "Client Name"
 Printer.CurrentY = stestY
 Printer.CurrentX = 1.75
 Printer.Print "Phone"
 Printer.CurrentY = stestY
 Printer.CurrentX = 2.75
 Printer.Print "whatever"
 Printer.FontUnderline = False
End Sub

this is where iPage comes into play, just allows you to write what page it is on the header. unfortunately since i'm not sure how many pages there will be off hand, this example will not print page 1 of 10 or whatever page count there is... (you could by printing it twice, killing the document and saving the current value of iPage in iPageCount, then printing "Page " & trim$(str$(iPage)) & " of " & $trim(str$(iPageCount))

anyway, that's the header.. kind of annoying to print this way, and yes i'm aware that this isn't the most opitimized or even close to it (neccessarily) way to do this.. but i haven't used the printer object in a good 10 years...

now for printing the list...

Private Sub PRINT_LIST()
 Dim sPrinterY As Single
 For x = 1 To 10
  sPrinterY = Printer.CurrentY
  If sPrinterY >= Printer.Height Then
   Printer.NewPage
   PRINT_HEADER
  End If
  Printer.CurrentX = 0.25
  Printer.Print Trim$(Clients(x).stName)
  Printer.CurrentY = sPrinterY
  Printer.CurrentX = 1.75
  Printer.Print Trim$(Format(Val(Clients(x).stHomephone), "(###)###-####"))
  Printer.CurrentY = sPrinterY
  Printer.CurrentX = 2.75
  Printer.Print Trim$(Clients(x).stWhatever)
 Next x
 Printer.EndDoc
 iPage = 0
End Sub

and finally how i called everything for testing (this was all done in a single form by the way)

Private Sub Form_Load()
 Printer.ScaleMode = vbInches
 SET_CLIENTS
 PRINT_HEADER
 PRINT_LIST
End Sub

if you want to test your document out before printing it, use a picture box. replace 'Printer.' with the name of your picturebox. (ie: picPage.print). endDoc won't apply to print page and new page won't do anthing either, but it's a good way to get a feel for your document without printing anything / wasting ink / paper...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.