?
Solved

Printing with List Boxes

Posted on 1998-12-15
9
Medium Priority
?
149 Views
Last Modified: 2010-05-03
I have a large List Box on a Form full of items added using the "Additem" method. How do I print:
 
i)everything (all the items) into a report?
ii) only the selected (checked) items into a report?

NB I'm using VB5 Enterprise Edition.
0
Comment
Question by:its_ajt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Author Comment

by:its_ajt
ID: 1450218
Edited text of question
0
 

Author Comment

by:its_ajt
ID: 1450219
Edited text of question
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450220
call PrintListbox(list1, true) to print all
call PrintListbox(list1, true) to print only selected

Private Sub PrintListBox(ByVal list As listbox, Optional ByVal fAll As Boolean = True)
    Dim i As Integer
    For i = 0 To list.ListCount - 1
        If (list.Selected(i) Or fAll) Then
            Printer.Print list.list(i)
        End If
    Next
    Printer.EndDoc
End Sub

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450221
call PrintListbox(list1, false) to print only selected
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1450222
I was wondering how two *identical* calls were producing different results! (grin) For a moment there I thought you'ld invented the "do what I meant" function!

M
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450223
It's magic..
0
 

Author Comment

by:its_ajt
ID: 1450224
Can't get the code to work properly - something about list.Selected(i) being an invalid array but the programs at home and I'm at work! - and also it doesn't cater for multiple pages...........else I need more help getting it to work with the Printer object/Common dialog Control.

Thanks - Merry Xmas
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450225
Good Luck
0
 
LVL 1

Accepted Solution

by:
twolff earned 400 total points
ID: 1450226
Mirkwoods code is not bad but I would handle it a different way. It will require 3 functions that can be reused in other applications.

I would write one function that would print the line on the page. This function would be passed the text to be printed. It would then compare the height of the page minus the currenty location to the amount of space that the text to be printed will require using the textheight method of printer object. If the text will fit on the page then print the line else start a new page and then print the line.

The next function will be used to loop through all of the items in the listbox and send the text to the first function to be printed.

The final function will be used to print only the selected items in the list. This is a little harder(or not depending on your api knowledge) than presented by mirkwood. You can use the SendMessage API to retreive an array of indexs of only the selected items in a listbox or filebox. You would then cycle through the array and only print the items that are chosen. This will eliminate the need to loop through the whole list when deciding what is selected. This technique will also work if the listbox's style is changed to checkbox in VB5 and VB6.

Here is a sample that I wrote:

> Add a filebox(File1) and a commandbutton(Command1) to a form and paste the following code.

Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_GETSELCOUNT = &H190
Const LB_GETSELITEMS = &H191

Private Sub Command1_Click()
   Dim lngNumSel As Long
   Dim lngSelItems() As Long
   Dim lngStatus As Long
   Dim strMsg As String
   Dim x As Long
   
   '--- get the number of selected items
   lngNumSel = SendMessage(File1.hwnd, LB_GETSELCOUNT, 0, 0)
   
   '--- redim the array to hold the selected items
   ReDim lngSelItems(lngNumSel)
   
   '--- get the selected items
   lngStatus = SendMessage(File1.hwnd, LB_GETSELITEMS, lngNumSel, lngSelItems(0))
   
   Select Case lngStatus
      Case -1
         strMsg = "Error Occurred"
      Case 0
         strMsg = "None Selected"
      Case Else
         For x = 0 To lngStatus - 1
            strMsg = strMsg & File1.List(lngSelItems(x)) & vbCrLf
         Next x
   End Select
   MsgBox strMsg
End Sub

I hope this helps. Enjoy.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

777 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