You are on page 1of 22

Internal Functions in VB 6.

Lecture 5
Introduction to Internal Functions
• Functions are a kind of a procedure. The
difference between a function and a procedure
is that a function sends a value to another part
of the program after it has finished executing its
code.
• An internal function operates like a magic box
that takes values you send to it and sends back,
or returns, a single value to your program.
• An internal function, sometimes called an
intrinsic function, is a function supplied with
the VB language that does a job such as a
calculation or I/O.

Introd. Cont’d
• In terms of internal functions and functions in general, you must
know the following:
– you typically pass one or more values to a function; rarely will a
function require nothing passed to it. These values that you pass
are called arguments.
– the function name always has parenthesis following it (with the
rare exception of those functions that require no arguments)
– you place the function's arguments, separated by commas if you
pass multiple arguments, inside the function's parenthesis.
• Because the code for internal functions is shipped with the VB
language, you simply pass the arguments to these functions in order
to make them work in your applications.
• Internal functions are time-savers and add power to your
applications
The MsgBox() Function
• This function is used to display a message box with at least one command
button like as shown below.

• The message box helps the user to communicate with the program while it
is executing. It can be moved but can’t be resized.
•The arguments that you supply the message box function will determine
which icon the message box displays, the message and the number of
command buttons. Hence the programmer controls exactly how the message
box appears to the user.
•When the message box completes, its return value specifies which
command button the user clicked. The programmer can then use an if
statement to determine the best course of action based on the user's
command selection.
MsgBox Function Contd
• The format for the message box function is:
• intVariable = MsgBox (strPrompt[, intstyle][, strTitle])
• intVariable holds the function's integer return data. The first argument is a string
that displays as the message box's message. The second argument determines the
style of the buttons that appear. The last argument determines the title that
appears in the message box's title bar.
• The program's execution temporarily halts on reaching the message box function
and resumes only if the user clicks one of the message box's command buttons.
As soon as the user clicks the command button, the program continues executing
at the statement that follows the command button.
• Here is an example of using the MsgBox function.
MsgBox Function Contd
• Here is the code at work. When the user clicks the print button, the
resultant message box will be as shown below:

•By clicking ok, the program uses the resultant message box:

•If the user clicked cancel in the Printer Dialog message box, the
program would have used the following message box:
MsgBox Function Contd
• If you want more control of your buttons, you can use an integer value
for the second argument to specify the style of buttons.
• With only one button, the return value makes little difference. With
more than one command button, the return value holds a value that
corresponds to the command button clicked. You can use this
information in an if statement or a select case to execute one of two sets
of code that handles either button.
• Below is a list of integer button style values you can use for the
message box's button style argument.
Using Named Constants
• A named constant is a name that VB applies to an internal list of values.
• VB's named constants usually begin with the VB prefix. You cannot change the
value of the named constants as you can change the value of the variables you
declare, but you can use the named constants in function argument lists and
anywhere else that a literal can go.
• The figure on the previous slide provides the VB named constants and their integer
values.
• The earlier sample code can be written using the named constant vbOkCancel as
instead of the integer value 1 as shown below.
• Using named constants makes the code more readable.
Specifying the Icon in the MsgBox

• Adding an additional value to the second argument specifies the icon used
to the left of the message inside the message box. Until now, the message
box code has not specified this part of the argument.
• Below is the list of named constant values and the icons they produce.

The print dialog example can be modified with the second argument to place
a question mark inside the message box as shown in the next slide.
Specifying the Icon in the MsgBox Contd
• Private Sub cmdClick_Click()
• Dim intResponse As Integer
• intResponse = MsgBox("Are you ready to print?", vbOKCancel
+ vbQuestion + vbDefaultButton2, "Printer Dialog")
• If (intResponse = 1) Then
• MsgBox ("Printing will now begin")
• Else
• MsgBox ("Printing has been cancelled")
• End If
• End Sub
• The out put would be as shown below.
Getting Input with InputBox()
• The MsgBox() function exists to display messages to your users but give
users a way to respond with the command buttons that appear in the
message boxes. The command button they click guides the next course of
action.
• If you need to ask the user a question, you can use the InputBox() function.
This function displays a message box and allows the user to enter a value
that your program can respond to.
• An input box is simply a message box with a field in which the user can
type a value, such as a word or phrase that might answer a question you ask.
Unlike message boxes, you cannot control which command buttons appear
in an input box and you cannot place an icon in it as well.
• The syntax of the InputBox() function is:
• strVariable = InputBox (strPrompt[, strTitle][, strDefault])

• The input box function returns a Variant data typed value that you can
always interpret as a string; so you can assign the input box function to a
string or use it anywhere you can use a string value.
• strPrompt - the message, such as the question you ask, that appears in the
input box.
• strTitle - the text that appears in the input box's title bar
• strDefault - you can specify default text that automatically appears in the input box's field.
InputBox() function contd
• Example of using the InputBox (without default argument)
• Private Sub cmdInput_Click()
Dim strInput As String
strInput = InputBox("Enter your first name.", "Wanna be famous?")
If (strInput <> "") Then
lblOutput.Caption = strInput
Else
lblOutput.Caption = "Please tell me your name.“
End If
End Sub
• The above code would produce the following output.
InputBox() function contd
• Below is the code that uses the default argument
• Private Sub cmdInput_Click()
Dim strInput As String
strInput = InputBox("Enter your first name.", "Wanna be famous?", "John Henry")
If (strInput <> "") Then
lblOutput.Caption = strInput
Else
lblOutput.Caption = "Please tell me your name."
End If
End Sub

The input box function returns a zero-length string, equal to "" if the user clicks
cancel instead of entering a value.
If a valid entry is entered, then that entry is placed on the form. If the user does
not enter a value, then alternate text is placed on the form.
Using the CheckBox
• A check box is an option on a form that is
checked when selected and unchecked when
not selected. Use check box controls when you
want to offer the user two or more value choices.
• The check box property value that determines
the current state of the check box resides in the
value property. If the value property is 1, the
check box is selected. If the value property is 0,
the check box is not selected.
CheckBox Contd
• Below is the sample code for a check box
• Private Sub Check1_Click()
If (Check1.Value = 1) Then
Label1.FontBold = True
Else
Label1.FontBold = False
End If
End Sub
• Private Sub Check2_Click()
If (Check2.Value = 1) Then
Label1.FontItalic = True
Else
Label1.FontItalic = False
End If
End Sub
• Private Sub Check3_Click()
If (Check3.Value = 1) Then
Label1.FontUnderline = True
Else
Label1.FontUnderline = False
End If
CheckBox contd
• The output of the previous code would be as
shown below.
Option (or Radio) Buttons
• Option buttons let the user select from one of several choices.
Unlike check boxes, VB lets the user select one and only one option
button at a time.
• Here is an example

Option Buttons Contd
• Below is the code to produce the functionality of the radio buttons
on the previous slide
• Private Sub Option1_Click()
• If (Option1.Value = True) Then
• Label1.Font = "Aerial"
• End If
• End Sub
• Private Sub Option2_Click()
• If (Option2.Value = True) Then
• Label1.Font = "Comic Sans Ms"
• End If
• End Sub
• Private Sub Option3_Click()
• If (Option3.Value = True) Then
• Label1.Font = "Garamond"
• End If
• End Sub
The Frame Control
• A frame, sometimes referred to as a container control, holds
controls on a plane that differs from the form itself .
• Although you can only provide one set of option buttons on a form,
you can provide multiple option button sets on the screen as long
as one or more sets reside on one or more frames.
• Frames can hold more than option buttons.
• A frame can hold any kind of control that you want to visually group
with other controls.
• Below is a sample of using a frame control
Frame Control Contd
• Below is the additional code for the radio buttons in the frame
• Private Sub Option4_Click()
If (Option4.Value = True) Then
Label1.FontSize = 10
End If
End Sub
• Private Sub Option5_Click()
If (Option5.Value = True) Then
Label1.FontSize = 12
End If
End Sub
• Private Sub Option6_Click()
If (Option6.Value = True) Then
Label1.FontSize = 14
End If
End Sub
The Form Load Procedure
• The Form Load procedure allows the programmer to initialize controls on
the form.
• When the form loads into memory, Windows studies the form load
procedure to determine which controls to set before the form appears on
the screen.
• In our example, we can use the form load procedure to initialize the bold
check box option, the Arial font option and the 12-point font size option.
Form Load Procedure contd
• The additional code to put in the form load
procedure is shown below.
• Private Sub Form_Load()
Option1.Value = True
Check1.Value = 1
Option5.Value = True
End Sub

You might also like