VB Examples and Exercises

Revision Test


[Top of Page] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [About this Page]

1. A form has a text box named txtIn and a label named lblvOut. Use the _Change procedure to make the label "echo" what is typed into the text box (i.e. at all times the contents of the label and the text box should be the same).

ANSWER

2. A form contains a text box named txtIn. Write code which, when the Enter key is pressed, checks the data in the text box and give an error message if it is blank. Include a procedure heading so we know which event you are using.

Hint 1: the VB constant for the Enter key is vbKeyReturn

Hint 2: Trim(variable) = "" will be true if variable is Null or all spaces.

ANSWER

3. What is wrong with the following code?

Private Sub txtIn_Change()

    txtIn.Text = txtIn.Text - 1

End Sub

ANSWER

4. A form contains a command button named cmdExit. The following line of Visual Basic code:

      cmdExit.Text = "RETURN"

produces the error message:

      Compile error: Method or data member not found

a) explain what is wrong with this line

b) rewrite the line correctly

ANSWER


[Top of Page] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [About this Page]

5. A form contains two list boxes, named lstProduct and lstPrice. The following line of Visual Basic code:

      List1.AddItem "ToothPaste"

produces the error message:

      Compile error: Variable not defined

a) explain what is wrong with this line

b) rewrite the line correctly

ANSWER

6. Consider the following code:

Dim now as Date

Dim number as Single

    now = Time

    number = Time

    Label1.Caption = now

    Label2.Caption = number

When this code is run, what will appear in Label1 and Label2?

ANSWER

7. A form has a text box called txtProdCode. The KeyPress procedure for this text box has the code:

Dim prodcode as Integer

    If KeyAscii = vbKeyReturn then

       prodcode = txtProdCode.Text

    End If

When this code is run and the user types 13-4310 into the text box the program stops with the error:

    Run-time error '13': Type mismatch

a) explain what is wrong with this code

b) rewrite the code correctly

ANSWER


[Top of Page] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [About this Page]

8. We wish to keep details of subjects on a university timetable on a file. The required details are subject number (5 digits), subject name (up to 20 characters), day of week (abbreviated as Mo, Tu, ... ), start hour, end hour and room number (up to 8 characters). Write the declaration for a public user defined type (record) to contain the subject details.

ANSWER

9. A user-defined type (record) is declared as follows:

Public Type product

    pcode As Integer

    pname As String * 15

    pcost As Currency

End Type

A variable is then declared as   Dim thisProduct As product

Write code which displays an error message if the product cost is greater than $999.99.

ANSWER

10. A form contains a list box named lstCities and a text box named txtCountry. A function Country(city As String) As String has been written and tested. This function finds the country in which the city is located. Write code which, when a city in the list is clicked, places the corresponding country in the text box. Include a procedure heading so we know which event you are using.

Note: Do not attempt to code the function.

ANSWER

11. A list box named lstNums contains numbers. Write code which totals the numbers in the list:

a) using a For ... Next statement

Hint: the ListCount property gives the length of a list;

b) using a Do ... Loop statement

Hint: assume the list item following the last number is Null (= "").

General Hint: The first list item is indexed 0.

ANSWER


[Top of Page] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [About this Page]

12. Consider the following code:

Dim i as Integer

Dim result as Integer

    result = 1

    for i = 1 to 5

       result = result * i

    Next i

    lblResult.Caption = result

    lblTimes.Caption = i

What appears in lblResult and lblTimes after the code is executed?

ANSWER

13. A list box named lstNums contains numbers. Write code which prints the numbers to a file called "a:\list.txt", one number to a line.

ANSWER


[Top of Page] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [About this Page]

14. A sequential file with identifier pfile contains an unknown number of numbers (e.g. 1,5,2,7,3,...) A form has a command button named cmdNext and a label named lblvNum. Write code which, when the button is clicked, will place the next number into the label, and will stop the program when there are no more numbers.

Note: Assume the file has already been opened.

Hint 1: The syntax of the input statement is

Input # filenumber, variable, variable, ...

Hint 2: End of file is tested with EOF(filenumber )

ANSWER

15. Student records contain sId (5 digits - students are numbered sequentially from 1), sName (up to 20 characters), sPhone (up to 20 characters). A user defined type named student and a variable of this type named thisStudent are declared. A random file containing student records has been opened as sfile. A form has text boxes txtId, txtName and txtPhone, and command buttons cmdFind and cmdStore. Write code which:

when cmdFind is clicked: reads the record with record number = txtID;

when cmdStore is clicked: changes the phone number in the record to txtPhone and writes the record back to the file.

ANSWER

16. A form has a text box txtIn, a label lblvTotal, and a list lstAmount. When an amount is typed into the text box and Enter is pressed the amount is added to the list. There are also buttons TOTAL (which puts the list total into the label), CLEAR (which clears the list) and EXIT.

Write an event/response list for this system. (Note: Do not write code.)

ANSWER


[Top of Page] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [About this Page]

About this Page

This page is maintained by Jim Underwood who can be reached at jim@socs.uts.edu.au.
This page was last updated on July 23rd 1999.

   http://www-staff.socs.uts.edu.au/~jim/avb/revision.html