VB Programs

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

DO LOOPS

It repeats the enclosed block of statements while a Boolean condition is True or until the
condition becomes True. It could be terminated at any time with the Exit Do statement.

The syntax for this loop construct is −

Example:

When the above code is compiled and executed, it produces the following result −
DO-WHILE LOOP

It executes a block of statements as long as a condition is True. The loop has the following
syntax:

If condition is True, all statements in the bock are executed. When the End While statement is
reached, control is returned to the While statement, which evaluates condition again. If
condition is still True, the process is repeated. If condition is False, the program resumes with
the statement following End While.

If you do not know the number of times you


need to execute a block of code, then you will
be using Do While loops. For example, you
want to output the message "Welcome" while
the value of the variable x is less than 5. In
such case, Do While loop will be used.

If you try executing the code, you will get the


output like this:

DO-UNTIL LOOP

‘Do Until’ loop is also used when you do not know the number of time you need to execute
a block of code. The first block of code in Do Until loop (Do While x<5) is equivalent to the given
below block of code.
This will give you the same output as the first block of code in Do While loop. You will see the
Welcome message four times. Similar to
Do .....Loop While, we have to Do...Loop
Until as well.

If you want to exit a Do While or Do Until


loop in between, then you can use Exit Do
statement. Suppose you want to exit the
block when the value of x becomes 3 in the
above program, then you need to code like
this:

If you execute the code, your output will look


like this:

VBScript For-Next Loop

The For-Next loop can be used to execute a block


of code for a specific number of times. The
“VBScript For loop" specifies the counter variable
and its start and end values. The Next statement increases the counter variable by one.

If you execute the code, you will get the output like this:
LISTBOX AND ARRAYS

ListBox - This control allows the user to select from a list of possible choices.

ListBox vs ComboBox

Adding to single column

You can use the "AddItem" method when you have a single column listbox.

If you try to add items to a listbox that has a non empty RowSource property you will get a
"permission denied" error.

Currently Selected Item

Obtaining the currently selected item in a single selection list box


Multiple Columns

A listbox can contain multiple columns by using the ColumnCount property.

You can use the "AddItem" combined with the List property when you have multiple columns.

All list entries start with a row number of 0, and a column number of 0, ie List(0,0) = "text"

If you want to add items to a multi column listbox, you need to use "AddItem" to add a new

row and then either "List" or "Column" to add the specific items past the first column.

Both column and row numbers in a listbox start at 0 by default and not 1.

The only way to obtain the selected items in a multiple selection list box is to cycle through the
whole list.

Adding using an Array


If you data is stored in a one-dimensional array you can assign the array directly using the List
property.

If you data is
stored in a
two-

dimensional array you can assign the array directly using the List property.

No items selected

It is possible to display a listbox with no items selected (when the listindex = -1).

Although once an item is selected it is not possible to unselect all the items.

Multiple selections

By default only a single item can be selected although this can be changed by changing the
MultiSelect property.

You can only make multiple selections with a listbox - not a combo box.

You might also like