Week-1-2-3 Introd To Visual Basic
Week-1-2-3 Introd To Visual Basic
Exploring VB Environment
Visual Basic .NET evolved from BASIC (Beginner’s All-Purpose Symbolic Instruction
Code), developed in the mid-1960s by Professors John Kemeny and Thomas Kurtz of
Dartmouth College as a language for writing simple programs.
VB offers
• VB is the newest addition to the family of Visual Basic products. It allows you to
quickly and easily create Windows applications for your PC without being an
expert in C++ or other programming languages.
• Visual Basic supports many useful tools that will help you to be more productive.
These include projects, forms, object templates, custom controls, add-ins and
database manager.
• Powerful graphical user interfaces
• Event handling
• Object-oriented programming and Exception handling
• Visual Basic .NET is an event-driven, visual programming language
• VB Programs are created using an Integrated Development Environment (IDE)
• With the IDE, a programmer can write, run, test and debug Visual Basic programs
conveniently, thereby reducing the time it takes to produce a working program to
a fraction of the time it would have taken without using the IDE
• The process of rapidly creating an application is typically referred to as Rapid
Application Development (RAD)
• Visual Basic is the world’s most widely used RAD language
Label
The control displays text on a Form that the user can’t edit. You set the label’s text with
the Caption property.
Frame
This control is used to draw boxes on the Form and to group other elements.
CheckBox
The CheckBox controls presents one or more choices that the user can select. The
CheckBox main property is Value, and it is 0 if the CheckBox is cleared, and 1 if the
CheckBox is checked. The CheckBox is toggle. Every time it is clicked, it changes status
(from checked to cleared and back).
ComboBox
The control is similar to the ListBox control, but it contains a Text-Edit field. The user
can either choose an item from the list or enter a new string in the Edit field.
Command Button
This is the most common element of the Windows interface. A Command Button
represents an action that is carried out when the user clicks the button.
ListBox
This control contains a list of options from which the user can choose one or more. The
selected item in a ListBox control is given by the Text property. Another property of
ListBox is the Sorted property, which determines whether the items in the list will be sorted.
Select Structure
The Select Case structure compares one expression to different values. The advantage of
the Select Case statement over multiple If..Then..Else statement is that it makes the code
easier to read and maintain.
The Select Case structure tests a single expression, which is evaluated once at the top of
the structure. The result of the test is then compared with several values, and if it matches
one of them, the corresponding block of statements is executed.
Syntax:
Example:
Option Explicit
Dim weekday As String
Dim dayname, message As String
If condition is True, then statement is executed otherwise, the statement following the If
structure is executed.
If-Else Structure
If condition is True, the first block of statement is executed, if condition is False, then
Visual basic ignores first block of statements and executes the following the block
following the Else keyword.
Syntax:
If condition Then
statementblock-1
Else
statementblock-2
End If
Example:
If-End If Structure
Use Multiple If Structures, once a True condition is found, Visual Basic executes the
associated statements are skips the remaining clauses it continues executing the program
with the statement immediately after End If.
Example:
Option Explicit
Dim score As Variant
Dim result As String
Private Sub Form_Load()
score = InputBox$("Enter your score")
If score < 50 Then
result = "Failed"
End If
If score < 75 And score >= 50 Then
result = "Pass"
End If
If score < 90 And score >= 75 Then
result = "Very Good"
End If
If score >= 90 Then
result = "Excellent"
End If
MsgBox result
Unload Form1
End Sub
Nested-If Structure
If outer If-structure condition is True, then inner If-structure condition is checked, if
condition is True, then first block of the statement is executed, otherwise then Visual
basic ignores first block of statements and executes the following the block following the
Else keyword within inner If-structure, otherwise Else clause will be executed within
upper If-structure.
Syntax:
If condition Then
If condition Then
statementblock-1
Else
statementblock-2
End if
Else
statementblock-3
End if
Example:
Private Sub Form_Load()
income = InputBox("Enter your income")
If income > 0 Then
If income > 20000 Then
MsgBox "You will pay taxes this year"
Else
MsgBox "You won't pay any taxes this year"
End If
Else
MsgBox "Bummer"
End If
Unload Form1
End Sub
If-ElseIf Structure
The conditions are evaluated from the top, and if one of them is True, the corresponding
block of statements is executed. The Else clause will be executed if none of the previous
expressions are True.
Syntax:
If condition Then
statementblock-1
ElseIf condition Then
statementblock-2
ElseIf condition Then
statementblock-3
Else
statementblock-4
End If
Example:
Option Explicit
Dim score As Variant
Dim result As String
Private Sub Form_Load()
score = InputBox$("Enter your score")
If score < 50 Then
result = "Failed"
ElseIf score < 75 And score >= 50 Then
result = "Pass"
ElseIf score < 90 And score >= 75 Then
result = "Very Good"
ElseIf score >= 90 Then
result = "Excellent"
End If
MsgBox result
Unload Form1
End Sub
For-Next Loop
The For-Next loop uses a variable (it’s called the loop’s counter) that increases or
decreases in value during each repetition of the loop.
Example1:
Private Sub Form_Load()
Dim nindex
Form1.Show
For nindex = 1 To 1000
Form1.Print "I must not play too many games"
Next
MsgBox "All done"
End
End Sub
Example2:
Private Sub Form_Load()
Dim nindex
Form1.Show
Form1.Print "Odd Numbers from 1 to 10"
For nindex = 1 To 10 Step 2
Form1.Print nindex
Next
MsgBox "All done"
End
End Sub
Example3:
While-Wend loop
Syntax:
While condition
statement-block
Wend
Example:
Option Explicit
Dim number As Integer
Dim counter As Integer
Do Loop While
Execute block of statements before while condition is True, otherwise only once if False.
Syntax:
Do while condition
statement-block
Loop
Example:
Option Explicit
Dim sPassword As String
Dim n As Integer
Subroutines
A subroutine is a block of statements that carries out a well-defined task. The block of
statements is placed with a pair of Sub/End statements and can be invoked by name. The
following subroutine displays the current date in a Message Box and can be called by its
name, ShowDate:
Example:
Sub ShowDate( )
MsgBox Date( )
End Sub
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
………
Functions
A function is similar to a subroutine, but a function returns a result. Subroutines perform
a task and don’t report anything to the calling program. Functions commonly carry out
calculations and report the result. The statements that make up a function are placed in a
pair of Function/End Function statements, because a function reports a result it must have
a type, for example:
The NextDay( ) function returns tomorrow’s date by adding one day to the current date.
Because it must report the result to the calling program, the NextDay( ) function has a
type, as do variable, and the result is assigned to its name.
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
………
When you run your project, you will see the Table of a Database within the DataGrid.
You can develop the Front end of your Database software project in Visual Basic very
easily. Visual Basic offers many build-in objects and components. You can easily drag and
drop onto your project and can connect with Database at Back end.
Create a Table
Example:
Example:
INSERT INTO client_master (client_no, name, address1, address2, city, state, pincode)
VALUES (‘C02000’, ‘Wasim’, ‘A-5, Sial Flats’, ‘Peshawar’,’Pakistan’,25000);
Example:
ALTER TABLE client_masters ADD (client_tel number(8));
Drop a Table