VB Functions
VB Functions
Functions are similar to normal procedures but the main purpose of the functions is to accept certain inputs
and pass them on to the main program to finish the execution. They are two types of function, the built-in
functions(or internal functions) and the functions created by the programmers.
functionName(arguments)
In this lesson, we are going to learn two very basic but useful internal functions, the MsgBox( ) and InputBox
( ) functions.
MsgBox ( ) Function
The objective of MsgBox is to produce a pop-up message box and prompt the user to click on a command
button before he /she can continues. This message box format is as follows:
The first argument, Prompt, will display the message in the message box. The Style Value will determine
what type of command buttons appear on the message box, please refer Table 10.1 for types of command
button displayed. The Title argument will display the title of the message board.
We can use named constant in place of integers for the second argument to make the programs more readable.
Infact, VB6 will automatically shows up a list of names constant where you can select one of them.
yourMsg is a variable that holds values that are returned by the MsgBox ( ) function. The values are
determined by the type of buttons being clicked by the users. It has to be declared as Integer data type in the
procedure or in the general declaration section. Table 10.2 shows the values, the corresponding named
constant and buttons.
Table 10.2 : Return Values and Command Buttons
Value Named Constant Button Clicked
1 VbOk Ok button
2 VbCancel Cancel button
3 VbAbort Abort button
4 VbRetry Retry button
5 VbIgnore Ignore button
6 VbYes Yes button
7 VbNo No button
The Interface:
Draw three command buttons and a label
End Sub
When a user clicks on the test button, the message box will appear. As the user click on the OK button, the
message "Testing successful" will be displayed and when he/she clicks on the Cancel button, the message
"Testing fail" will be displayed.
To make the message box looks more sophisticated, you can add an icon besides the message. The are four
types of icons available in VB as shown in Table 10.3
End Sub
The InputBox( ) Function
An InputBox( ) function will display a message box where the user can enter a value or a message in the form
of text. The format is
End Sub
When a user click the OK button, the input box will appear. After user entering the message and click OK,
the message will be displayed on the caption, if he click Cancel, "No message" will be displayed.