VB 6 Collections

Advertisements
Advertisements

In the previous article, you learned about different types of loops and know about arrays. In VB 6, there is also a container called Collection.

The collections are interesting because they have following properties:

  • Can contain different data types
  • Have their own index
  • Have their own methods

The collection have different types of methods to manage the collection and its items. They are listed below.

Add – to add items to collection

Remove – to remove an item from collection

Advertisements

Count – to count number of items in the collection.

Item – access any item in the collection using its index.

Example Program: Collection

In this program we will accept the items through a textbox and use Add method of collection. We can display and show the collection items respectively.

Dim myCollection As New Collection
Dim studentName As String, i As Integer

Private Sub Command1_Click()

myCollection.Add (Text1.Text)
Text1.Text = ""

End Sub

Private Sub Command2_Click()

For i = 1 To myCollection.Count

studentName = myCollection.Item(i)
MsgBox (studentName)

Next i

End Sub

Private Sub Command3_Click()

MsgBox (myCollection.Count)

End Sub

Output – Collection

Add item to the collection from the text box.

Figure 1 - Add Item to the Collection
Figure 1 – Add Item to the Collection

Show the item from the collection by clicking on the Show button. All the items will be displayed one at a time.

Figure 2 - Show items from the collection one at a time.
Figure 2 – Show items from the collection one at a time.

Display the number of items in the collection.

Figure 3 - Number of items in the collection
Figure 3 – Number of items in the collection

Visual Basic 6.0 Programming by Examples Kindle Edition

Visual Basic is one of the easiest to learn computer programming language.
Yes, it is obsolete, but all MS Office products include VBA (Visual Basic for Application) and if you learn VB, you will know VBA!
In my tutorial, I used VB 6 to explain step by step how to create a simple Visual Basic Application and a relatively complex one (a Patient Management system) that is using a database. A patient Management application source code is explained in detail.
You will learn how to design and create a database in MS Access and how to create tables and queries.
The book includes a sample application that shows how to use Windows API function.
You will learn how to convert VB program that can be run only in Visual Basic development environment to a distributable application that can be installed on any client computer.
For illustration, I included 128 screenshot images and links to a video.
You will be able to download from my website complete source code for 7 Visual Basic projects including a Password Keeper, a Patient Management and a Billing Management application.
Get Your Copy Today!

Advertisements
Advertisements
Advertisements