Solved

Selected property and printing

Posted on 2004-10-08
10
177 Views
Last Modified: 2010-05-02
Hi:
    Can someone give examples of how to use the selected property to print multiple selection from a List box?
0
Comment
Question by:LearningJava
10 Comments
 
LVL 7

Expert Comment

by:HoweverComma
ID: 12264547
Dim Inum as Integer
Inum=0

While Inum< ListBox.ItemCount-1      ' -1 because it is Zero based
If ListBox.Selected(Inum)=True Then
    'It is selected we need to print
    ' If it is not numeric data the use Str(ListBox.ItemData(Inum))
    Printer.Print ListBox.ItemData(Inum)
End If
Printer.EndDoc
Wend
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264554
Private Sub Command1_Click()
Dim c As Integer
For c = 0 To List1.ListCount - 1
    If List1.Selected(c) = True Then
        Debug.Print List1.List(c)
    End If
Next c
End Sub

S
0
 
LVL 10

Expert Comment

by:anv
ID: 12265387
Set List1 MultiSelect to true in design mode..

and then do following

dim ar() as string

Private Sub Command1_Click()
      Redim ar(List1.ListCount-1)
      dim i!,j!
      j=0

      for i = 0 to list1.ListCount - 1
         if List1.Selected(i) = True then
           printer.print List1.List(i)
           ar(j) = List1.List(i)''for storing into an array for later use.
           j=j+1
         end if
      next

instead of storing it into an array u can store the list into a text file...

which u can print later after the loop..
reasn being..

above code will print each line into a separate page (i suppose)

to avoid that ..u can append the list to a file...which u can print later..


0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:LearningJava
ID: 12266236
Where does ListCount come from? I don't see it in the ListBox properties?

Also, can someone explain to me what ItemData means?
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12266262
Are you with VB or VBA?
ListCount is a property for listbox in VB.  ItemData is another property that allows you to set a specific NUMBER to each item in the list, so later on you can identity this item using the ItemData, like if you have a job number, or employee number, or part number, so you can display the item's name and store in ItemData the corresponding number for later use.

S
0
 

Author Comment

by:LearningJava
ID: 12266406
I am using Visual basic 6.0.

Ithought that the Listbox entries could be accessed by index number??
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12266429
Yes, they are.

Dim c As Integer
For c = 0 To List1.ListCount - 1    'listcount is the number of items in the list
    If List1.Selected(c) = True Then    'c is looping among the index numbers.
        Debug.Print List1.List(c)    'doing your stuff with a specific item in the list. In this case - a selected item
        'do your stuff here
    End If
Next c

S

0
 

Author Comment

by:LearningJava
ID: 12266598
What is the meaning of Debug.Print?

Why not Printer.Print List1.List(c)?

Also, could you elaborate on ListCount? I don't see it in the  properties for Listbox.

0
 
LVL 19

Expert Comment

by:Shauli
ID: 12266662
Debug.Print is a convenient way to see the result in the immediate window (the bottom window on the VB) to check that your code does what you really expect it to do. If you want to print to a printer then Printer.Print List1.List(c) is just fine.

S
0
 
LVL 19

Accepted Solution

by:
Shauli earned 500 total points
ID: 12266691
ListCount must be in the property list. Type List1. (dot) and a list of properties will open. Scroll down (it is sorted alphabetically) and you'l find it there.
ListCount returns the total of items in a listbox. Lets say your listbox list is:

John Doe
Jane Doe
GW Bush
JF Kerry

Then ListCount will return 4. However, as the first index number for the list in a listbox is 0, then when you loop among the index of the list, you loop from 0 to ListCount -1, which means from 0 to 3 (in this case, where you have 4 items in the list)

S
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 115
MS Acccess - Update Field in Table when report is printed 6 44
Added a column screws up code 5 57
Help me. 3 54
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

776 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