Third
Third
To Create an MDI child form , Select Form1 ( Or add a new form) and set its MDI child
Property to True. It is a standard form with following attributes.
1. A Child form cannot be moved outside the parent form’s boundaries
2. Activating a Child form will result in the activation of the parent form
3. When the child form is minimized , its icon will appear at the bottom of the
workspace of the parent form
4. When a child form is maximized , it fills the entire are of the parent form , and
its menu will become the menu of the parent form
Unit - III 53
Visual Basic
5. There can be more than one child form in an application
6. The icons for MDI parent, child and a standard form are different.
To place other control on an MDI form , we can draw a picture box on the form,
and then draw other controls inside a picture box.
An MDI form object can’t be modal
The Menu for the Child form will be displayed as the menu on the MDI form
when the Child form has the focus.
Unit - III 54
Visual Basic
The default AutoShowChildren property displays the child forms automatically
when they are loaded
The default scrollbars will display the scroll bars when the child forms extend
beyond the boundaries of the parent form.
DEBUGGING TIPS
What is Debugging?
The Process of finding out and removing the error is called debugging.
Errors of Syntax
A syntax error is generated when Visual Basic Editor detects an invalid statement
that violates the Visual Basic syntax rules. For example, misspelled keywords generate
syntax errors. An error dialog box appears that indicates the error message, and
highlights the invalid statement in red.
VBA environment is by default configured to automatically check for syntax
errors. Click on the Tool and Choose options from the Menu. Under the ‘Editor’ tab we
will find the following items checked.
They are code setting and window setting
Code Setting
1. Auto Syntax check
2. Require Variable declaration
3. Auto List members
4. Auto quick info
5. Auto data tips
Window Setting
1. Drag – and – Drop text Editing
2. Default to Full Module view
3. Procedure Separator
Unit - III 55
Visual Basic
Auto Syntax Check
This option used to will check for syntax errors as we enter the commands and
statements.
Runtime Errors
Runtime error is the execution generated by Visual basic when it ascertains that
the code is about to perform something illegal.
Unit - III 56
Visual Basic
An illegal function could be something as simple as trying to determine the size
of file that does not exist or attempting to multiply two numbers, the result of which
exceeds the storage that could be contained by the data type.
Example
Private Sub Form_Load()
Dim x As Integer
Dim y As Integer
Dim z As Integer
x=9
y=0
z=x/y
End Sub
Logical Errors
logical errors are the most difficult to find, with logic errors , the program will
usually run , but will produce incorrect or unexpected results.
A logic error is a human error - a programming mistake that makes the program code
produce the wrong results
Unit - III 57
Visual Basic
Example
Private Sub Command1_Click()
Dim I as Integer
For I = 1 To 20 step 4
Msgbox I
Next I
End Sub
Debug.Print
The other method is to use the Debug object. The debug object will send the
output to the immediate window. The code above can be modifies to look like this.
Example
Private Sub Command1_ Click()
Dim I as Integer
For I = 1 To 20 step 4
Debug. Print I
Next I
End Sub
The Value assigns to the variable ‘I’ will be displayed in the “Immediate Window”.
Unit - III 58
Visual Basic
Group 2
Toggle Breakpoints
Step Into
Step Over
Step Out
Group 3
Locals Window
Immediate Window
Watch Window
Quick Watch
Call stack
Start Button
We start the program by clicking on this button. It is like pressing F5 button and
choosing the Run option
Break Button
It Pauses the program. It is like pressing the ctrl + Break key
End Button
Halts the program. The next sets of buttons come into play only if we set the
Breakpoint
Toggle Break Points
We can have more than one breakpoint, click on this button to shift the
breakpoints
Step Into
Executes code one statement at a time.
Step Over
Click on the Step Over icon , The Called procedure will e executed , and control
will be returned to the next line in the current procedure
Step Out
Click on Step Out icon, control will be transferred to the line after the current
procedure or function.
Local Window
Click on the Locals Window icon. This Window will display all the variables that
are declared in the current procedure. As we continue to step through code , we can
watch the variables that are used in the current procedure.
Unit - III 59
Visual Basic
Immediate Window
This is the window to which the Debug. print sends the output. In this window
we can directly executes a statement as if we are directly communicating with the
interpreter.
Watch Window
In this window all the variables used in the application till the breakpoint are
displayed. The Watch window also displays the name of the procedure where the
variables is used and the value that variables assumes.
Call stack
The Call Stack window allows we to see the list of procedures that were called
in order to get to the current statement. The window may either be displayed by
selecting "Call Stack" from the "View" menu or by clicking the "Locals" window's
"Call Stack" button.
Unit - III 60
Visual Basic
ERROR HANDLING
Error handling is an essential procedure in Visual Basic programming because it
can help make the program error-free. An error-free program can run smoothly and
efficiently, and the user does not have to face all sorts of problems such as program
crash or system hang.
Errors often occur due to incorrect input from the user. For example, the user
might make the mistake of attempting to ask the computer to divide a number by zero
which will definitely cause system error. Another example is the user might enter a
text (string) to a box that is designed to handle only numeric values such as the weight
of a person, the computer will not be able to perform arithmetic calculation for text
therefore will create an error. These errors are known as synchronous errors.
Start
Unit - III 61
Visual Basic
Is Error
Erro Handler
r
Next Statement
End
Error Trapper
An Error trapper should be introduced in the procedure where we anticipate the
error. Once an error occur , the program flow must be directed to another part of the
procedure will be resolved.
On Error Statement
The On Error statement causes Visual Basic for Applications to start or stop error
trapping. The On Error statement also specifies a set of statements to execute if an error
is encountered.
On Error go to label
On Error go to 0
On Error go to Label
This statement tells the VBA to transfer the program control to the line followed
by the label, in case any runtime errors are encountered. In such cases all the statements
between the exception line and the label will not be executed.
Syntax
Sub MySub()
On Error GoTo label
Statements that do something useful
Exit Sub
label:
Error-handling statements
End Sub
Example
Unit - III 62
Visual Basic
Sub GetErr()
On Error GoTo Error_handler:
N = 1 / 0 ' cause an error
MsgBox "This line will not be executed"
Exit Sub
Error_handler:
MsgBox "exception handler"
End Sub
On Error go to 0
This is also called VBA default exception handling. When On Error Goto 0 is in
effect, it is same as having no error handler in the code. Here we are instructing the
program to display the standard runtime message box with ‘Continue’, ‘End’, ‘Debug’
and ‘Help’ buttons.
Unit - III 63
Visual Basic
13 - Type mismatch
14 - Out of string space
52 - Bad file name or number
53 - File not found
55 - File already open
58 - File already exists
68 - Device unavailable
76 - Path not found
94 - Invalid use of Null
Example
Private Sub Command1_Click()
On Error GoTo OpenFileError
Open "A:\JUNK.TXT" For Input As #1
MsgBox "File was opened successfully"
Close #1
Exit Sub
OpenFileError:
MsgBox "The following error occured: " & vbNewLine _
& "Error # " & Err.Number & vbNewLine _
& Err.Description, _
vbCritical, _
"Open Error"
End Sub
Unit - III 64
Visual Basic
The Common Dialog Control Methods
The Common Dialog control can display the following dialogs, using the specified
methods.
Unit - III 65
Visual Basic
File Open Dialog Box
Write some simple code to allow the user to open a file and displays its contents
in the textbox.
Add the following line in the code window of the open command button
CommonDialog1.ShowOpen
There are two ways we can give a title to the dialog box
1. By setting the title at design time
2. By setting the title at runtime through code
Add the following line to the code the Open Command button .
Private Sub Command1_Click()
CommonDialog1.DialogTitle = “Select the File to Open”
CommonDialog1.Showopen
End Sub
Saving a file
The ShowSave method to allow the user to enter the name of the file in which
the text entered in the textbox has to be saved.
Add the following line to the code the Save Command button .
Private Sub Command2_Click()
CommonDialog1.DialogTitle = “Select the File to Open”
CommonDialog1.Showopen
End Sub
Changing the Color
The Common Dialog Control also provides to change the color of the text or other
controls on the form. To display the color Dialog Box we must use the ShowColor
methods.
Add the following line to the code the Color Command button .
Private Sub Command3_Click()
CommonDialog1.ShowColor
Text1.Forecolor = CommonDialog1.Color
End Sub
Printing a document
The Print dialog box allows the user to specify how output should be printed.
The user can specify a range of pages to be printed, print quality, number of copies to
be printed, and so on.
Unit - III 66
Visual Basic
The following example uses the ShowPrinter method with the Printer object to
set the number of copies and page orientation.
Private Sub Command1_Click()
CommonDialog1.ShowPrinter
Printer.Copies = CommonDialog1.Copies
Printer.Orientation = CommonDialog1.Orientation
Printer.EndDoc
End Sub
Unit - III 67
Visual Basic
Changing the font of the selected text
In this case of the Rich Textbox , formatting changes have to be applied to the
text after selecting it. In order to change the font of a portion or all the text in the rich
textbox.
Select the text and call the font dialog box. Select the font type click Ok. The
selected text will be displayed in the New format.
Add the following line to the code the Font Command button .
Private Sub Command3_Click()
CommonDialog1.ShowFont
RichTextBox1.SelFontName = CommonDialog1.FontName
End Sub
richTextBox1.SelRightIndent = 12
Unit - III 68
Visual Basic