Anda di halaman 1dari 11

Week-1 Introduction 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.

Visual Basic appeared in 1991, developing Microsoft Windows-based applications


was a difficult and cumbersome process. Although Visual Basic is derived from the
BASIC programming language.

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

Using the Intrinsic Controls

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.

Week-2 Sub Routine and Functions


Looping and Decision Control Structures
If / then / else Structure
Select Structure

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:

Select Case expression


Case value 1
statementblock-1
Case value 2
statementblock-2
.
.
.
Case Else
Statementblock
End Select

Example:
Option Explicit
Dim weekday As String
Dim dayname, message As String

Private Sub Form_Load()


weekday = InputBox$("Enter day")
Select Case (weekday)
Case 1
dayname = "Monday"
message = "Have a nice week"
Case 6
dayname = "Saturday"
message = "Have a nice weekend"
Case 7
dayname = "Sunday"
message = "Did you have a nice weekend?"
Case Else
message = "Welcome back"
End Select
MsgBox message
Unload Form1
End Sub

If / then / else Structure

If structure can have a single-line or a multiple-line syntax.


Use single-line syntax:

If condition Then statement

Use multiple-line syntax:

If condition Then statement: statement: statement

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:

Private Sub Ok_Click()


If Text2.Text = "administrator" And Text1.Text = "letmein" Then
MsgBox "Great-Username & Password accepted"
Unload Form1
End
Else
MsgBox "Sorry, that's wrong try again, check username or password"
Text1.Text = ""
Text2.SetFocus
End If
End Sub

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

Looping and Decision Control Structures

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:

Private Sub Form_Load()


Dim i
Form1.Show
Form1.Print "Integer Numbers from 1 to 10"
For i = 1 To 10 Step 2
Form1.Print i
i=i-1
Next i
MsgBox "All done"
End
End Sub

While-Wend loop

The While..Wend loop executes a block of statements while conditions is True.


If condition is True, all statements are executed and when the Wend statements is
reached, control is returned to the While statement which executes condition again.
If condition is still True, the process is repeated. If condition is False, the program
resumes with the statement following the Wend statement.

Syntax:

While condition
statement-block
Wend

Example:
Option Explicit
Dim number As Integer
Dim counter As Integer

Private Sub Form_Load()


number = 0
While number >= 0
counter = counter + number
number = InputBox("Please Enter another value, less than 0 value to quit")
Wend
MsgBox counter
Unload Form1
End Sub

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

Private Sub Form_Load()


n=3
Do
sPassword = InputBox$("Enter the password and click OK")
n=n-1
Loop While sPassword <> "letmein" And n > 0
If sPassword <> "letmein" Then
MsgBox "You got the password wrong-Access denied"
Unload Form1
Else
MsgBox "Welcome to the system - password accepted"
Unload Form1
End If
End Sub
Sub Routine and Functions

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:

Function NextDay ( ) As Date


NextDay= Date( ) +1
End Function

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.

……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
………

Week-3 Developing Database Application

Connecting to SQL Server


Steps for setting up connection to SQL Server and showing in DataGrid object using
ADODC object

1. Select a New Data Project


2. Select a Form
3. Drag and drop ADODC onto Form
4. Drag and drop Data Grid
5. Go to the Properties Window of Data Grid and set Data Source Property to ADODC
6. Go to the Properties Window of ADODC and browse ConnectionString Property
and select Use Connection String option and click on Build button on the right side.
7. A Data Link Properties Window will be appeared
8. Select Microsoft OLE DB Provider for SQL Server and click on Next button.
9. Enter or Select a Server Name. i.e. Database Server Name
10. Enter the Windows integrated security username and password or Use a specific
username and password of a Server. i.e. Database Server
11. Select a Database on the Server. i.e. the one you want to connect to
12. Click on Test Connection button. i.e. to test the Database connectivity.
13. Click Ok
14. Click Apply
15. Click Ok
16. Go to the Properties Window of ADODC once again and this time set RecordSource
Property to “2-adCmdTable” and select a Table of a Database. i.e. from the
Database you have connect to.
17. Click Apply
18. Click Ok

When you run your project, you will see the Table of a Database within the DataGrid.

Developing Front End

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.

Like: DataGrid, DataList, DataCombo, DataRepeater, MSHFlexGrid etc

Designing Database Tables in SQL

Create a Table
Example:

CREATE TABLE sales_order_details (detorder_no varchar2(6),


product_no_varchar2(6), qty_ordered number(8), qty_disp number(8), product_rate
number(8,2), PRIMARY KEY (detorder_no, product-no));
Example:
CREATE TABLE sales_order_details (detorder_no varchar2(6),
product_no_varchar2(6), qty_ordered number(8), qty_disp number(8), product_rate
number(8,2), PRIMARY KEY (detorder_no, product-no), FOREIGN KEY
(detorder_no) REFERENCES sales_order);

Insert data into a Table

Example:
INSERT INTO client_master (client_no, name, address1, address2, city, state, pincode)
VALUES (‘C02000’, ‘Wasim’, ‘A-5, Sial Flats’, ‘Peshawar’,’Pakistan’,25000);

Update a Table Column


Example:
UPDATE client_master SET name=’Waqar’, address1=’B-6 Muslim Apartements’,
WHERE client_no=’C02001’;

Alter/Modify a Table Column Size or Add new column


Example:
ALTER TABLE client_masters MODIFY (client_fax varchar2(25));

Example:
ALTER TABLE client_masters ADD (client_tel number(8));

Drop a Table

DROP TABLE client_masters;

Anda mungkin juga menyukai