Free Guides

Language Tutorials


           

 

Visual Basic

          Index         

Your Ad Here

Introduction to VB Functions Part I-Built-in Functions

A function is similar to a normal procedure but the main purpose of the functios is to accept a  certain input and return a value which is passed on to the main program to finish the execution. There are two types of functions, the built-in functions (or internal functions) and the functions created by the programmers.

The general format of a function is
              FunctionName (arguments)

 The arguments are values that are passed on to the function.

In this lesson, we are going to learn two very basic but useful internal functions of Visual basic , i.e.  the MsgBox( ) and InputBox ( ) functions. You can also learn about mathematical functions, formatting functions and  string manipulation functions by clicking the links at the end of this page.

10.1 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  format is as follows:

          yourMsg=MsgBox(Prompt, Style Value, Title)
 

 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.

Table 10.1: Style Values

Style Value

Named Constant Buttons Displayed

0

vbOkOnly Ok button

1

vbOkCancel Ok and Cancel buttons

2

vbAbortRetryIgnore Abort, Retry and Ignore buttons.

3

vbYesNoCancel Yes, No and Cancel buttons

4

vbYesNo Yes and No buttons

5

vbRetryCancel Retry and Cancel buttons

We can use named constant in place of integers for the second argument to make the programs more readable. In fact, VB6 will automatically shows up a list of names constant  where you can select one of them.

example: yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu")

             and yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu")

are the same.

 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

 

 

 

 

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  

myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)

myMessage is a variant data type but typically it is declared as string, which accept the message input by the users. The arguments are explained as follows:
 

  • Prompt       - The message displayed normally as a question asked.

  • Title            - The title of the Input Box.

  • default-text  - The default text that appears in the input field where users can use it as his intended input or he may change to the message he wish to key in.

  • x-position and y-position - the position or the coordinate of the input box.

Example 10.3

i.  The Interface

 

 

ii. The procedure for the OK button

Private Sub OK_Click()
Dim userMsg As String
userMsg = InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700)
If userMsg <> "" Then
message.Caption = userMsg
Else
message.Caption = "No Message"
End If

End Sub

When a user click the OK button, the input box as shown in Figure 10.5 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.