0% found this document useful (0 votes)
53 views24 pages

VB-UNIT-2-Notes

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views24 pages

VB-UNIT-2-Notes

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT II

DISPLAYING INFORMATION:
Display text on a Form using the print method.
SYNTAX:

FormName. Print expression

Auto Redraw property of the form is set to true, the


text will disappear.
Private Sub Form_Load( )
Show
Print “Hello”
End Sub
Auto ReDraw property is to simply move the
above code to both the Form_Resize event and Form_paint event
procedures as in the following code:
Private sub Form_Paint( )
Cls
Print “Hello”
End sub
Private sub Form_Resize()
Cls
Print “Hello”
End Sub
LOOP SRUCTURES:
Looping structures are used when a group a statements are to be
executed repeatedly based on a condition. VB supports two types of
looping structures. They are:
❖ Indeterminate Loops
❖ Determinate Loops
INTDETERMINATE LOOPS:
Indeterminate loop is continuing until you reach a
predetermined specific goal or until certain conditions. The
indeterminate loops:
❖ Do……Loop
❖ Do While…..Loop
❖ Do……Loop While
❖ Do until…….Loop
❖ Do…….Loop until
DO…..LOOP:
▪ A Do….Loop construct is used to repeat the
execution of a block statements based on a condition.
▪ This loop constructs, evaluates a condition to
determine whether or not to continue the execution.
▪ The condition must be value or an expression that
returns a False or a True Value.
DO WHILE…..LOOP:
First it will check the condition, if it is TRUE it executes
the statements inside the loop, otherwise it will come out of the loop.
SYNTAX:

Do While<Condition>
Statements
Loop

EXAMPLE:
Private Sub Form_Load( )
Form1.Show
Dim I As Integer
I=1
Do while I<=5
Print I
I=I+1
Loop
End Sub
DO…..LOOP WHILE:
If first executes the set of statements once and then tests the
condition. If the condition is true it enters into the loop. Otherwise it
will exit the loop.
SYNTAX:

Do
Statements
Loop while<condition>
EXAMPLE:
Private Sub Form_Load( )
Form1.Show
Dim I As Integer
I=1
Do
Print I
I=I+1
Loop While I<=5
End Sub
DO UNTIL…..LOOP:
It first checks the condition if it is false executes the
statements and it continues the loop till the condition is false.

SYNTAX:
Do Until<Condition>
Statements
Loop

EXAMPLE:
Private Sub Form_Load( )
Form1.Show
Dim I AsInterger
I=1
Do Until I>=5
Print I
I=I+1
Loop
DO…..LOOP UNTIL:
It first executes the statements at least once and then tests the
condition, if the condition is false it will again continue loop,
otherwise it will exit the loop.
SYNTAX:
Do
Statements
Loop Until<Conditio>
EXAMPLE:
Private Sub Form_Load( )
Form1.Show
Dim iAs Integer
i=1
Do
Print i
i= i+1
Loop Until i>=5
End Sub

DETERMINATE LOOPS:
Reapting an operation a fixed number of times is called a
determinate loop. The determinate loops are
❖ For…..Next
❖ For Each…..Next
FOR…..NEXT:-
The For….Next Loop uses a counter that increases or decreases
the value of a variable each time the loop is executed.

SYNTAX:
For<counter>= Start To End
Statements
Next<Counter>

Where counter→Integer Variable


Start→ Initial value of Counter
End→ Stop Value of Counter
Step Value→ Increment or Decrement value of counter

EXAMPLE:
Private Sub Form_Load( )
Form1.Show
Dim I As Integer
For i= 1 to 5
Print i
Next i
End Sub
FOR EACH…..NEXT:
The For Each….Next Loop is used to perform an
operation on each element of an array or collection.

SYNTAX:
For Each element In Collection
Statements
Next

EXAMPLE:
Private Sub Form_Load( )
Form1.show
Dim CtrAs Control
For Each CtrIn Controls
Print Ctr.Name
Next
End Sub

CONDITIONAL STRUCTURES:
• Conditional structures enable you to execute a
certain set of statements set of statements based
on a condition.
• The condition should be specified at the design
time.
• At runtime, the condition is evaluated and
depending on the result of the condition, the
block of code following the condition is
executed.

IF….THEN:
IF…Then statements evaluate whether a condition is True
or False and direct the Program’s flow accordingly.
SYNTAX:
IF<condition> Then
Statement
End IF
EXAMPLE:
If a<=5 Then
Print a
End if
If the value of “a” is less than 5 then the
value of “a” is printed. Otherwise it won’t print.

IF…THEN-ELSE:
If….then…else statements are an elaboration on the
if…then concept. An If….Then-Else block allows you to define two
block of code and have your program execute one based upon the
result of a condition.
SYNTAX:
IF<Condition> Then
Statement 1
Else
Statement 2
End If
EXAMPLE:
IF a<=5 Then
Print a
Else
MsgBox “Number 1 to 5”
End If
If the value of ‘a’ is less than 5 then the value
of ‘a’ is printed. Else it displays an MsgBox with Message Number 1
to 5.

IF….THEN-ELSE IF:
It evaluate more than one condition and also define several
blocks of statement.(Nested If).
SYNTAX:
IF<Condition1> Then
Statement 1
ElseIf<Condition2> Then
Statement 2
Else
Statement 3
End If
EXAMPLE:
b=10
If a=5 Then
Print a
ElseIf a<=10 Then
Print b
Else
MsbBox”Number 1 to 10”
End IF
If the value of ‘a’ is less than or equal to 5 then
print the value of ‘a’. Else check whether the value of ‘a’ is less or
equal to 10 value of ‘b’ is printed. Else aMsg number 1 to 10 is
displayed the msgbox.

SELECT CASE:
In Select Case, the result of the expression is then
compared against multiple values to determine which code block is
invoked. It is also called multi-way branching statement.
SYNTAX:
Select case <expression>
Case <expression 1>
Statement 1
………..
Case <expression n>
Statement n
End Select.
EXAMPLE:
Select Case Avg
Case Is>90
Grade = “A”
Case Is>80
Grade = “B”
Case Is <40
Grade =”F”
End Select

BUILT IN FUNCTION:
A built-In function is simply a pre-packaged piece of code
that accomplishes a single task.
Some of the built-in functions are:
1. LEN:
The function counts the characters in a string and returns
the length as an Integer.
SYNTAX:
Len(String)

EXAMPLE: RESULT:
PrintLen(“Mother”) 6
2. MID:
The MID Function returns characters from any part of a
string.
SYNTAX:
Mid(String Expression, Start Index, Length)

EXAMPLE: RESULT:
Print Mid(“Hello World”,5) o World

3. REPLACE:
This function is used to replace the string.
EXAMPLE: RESULT:
Replace(TXTY, “X”,”Y”) TYTY
4. STRCOMP:
▪ In Case-sensitive comparisons, capital letters have
different value than lowercase letters. Remember that
the ASCII value of a lower case letter is greater than
the capital letter by 32.

▪ ASC(“A”)=65
ASC(“a”)=97.
▪ In case-sensitive comparisons, “A”<”a”, and
comparisons that are not case sensitive “A”=”a”.

▪ If you are doing case-sensitive comparisons “A”=”a”


return False.
The StrComp function returns a value indicating the result of a string
comparison.

SYNTAX:
Strcomp(String1, String 2, VB Compare)
▪ Where String 1 and string 2 are any valid string
expressions.
• Compare specifies the type of string
comparison →0 for case sensitive comparison
→1 for not case-sensitive comparisons.

The Return Values for StrComp:


RETURN COMPARISON
VALUE
-1 String 1 < String 2
0 String 1 = String 2
1 String 1 > String 2
NULL String 1 or String

EXAMPLE:
Dim Str1 as String, Str2 as String
Str1= “Abcdef”
Str2= “Abcdef”
Print strcomp(str1, str2, VbBinaryCompare)
RESULT:
1
5. ASC:
The ASC Function returns the character code
corresponding to the first letter in a string.
SYNTAX:
ASC(Str)
EXAMPLE: RESULT:
Dim Str1 As String 65
Str1=”A”
Print ASC(Str1)
6. LCASE:
The LCase function returns a string with all letters
converted to lowercase.
SYNTAX:
LCASE(String)
EXAMPLE: RESULT:
Dim Str1 As String visual basic
Str1= “VISUAL BASIC”
Print LCase(Str1)

7. UCASE:
The UCase function returns a string with all letters
converted to uppercase.
SYNTAX:
UCASE(String)

EXAMPLE: RESULT:
Dim Str1 As String VISUAL BASIC
Str1= “visual basic”
Print UCase(Str1)

8. SPACE:
The Space Function returns a string consisting of the
specified number of spaces.

SYNTAX:
SPC(Integer Value)
EXAMPLE:
Print “Welcome” & Spc(4); & “to VB”

RESULT:
Welcome to VB.

9. TAB:
The TAB Function returns a string consisting of the
specified number of spaces.
SYNTAX:
Tab(Integer Value)
EXAMPLE:
Print “Welcome” & TAB(4); & “to VB”
RESULT:
Welcome to VB.

10. LEFT :
The Left Function returns characters from the left-hand side of
string.
SYNTAX:
Left(String, Length)
o Where string is the string from which you
are returning characters.
o Length is the number of characters to
return.
EXAMPLE: RESULT:
Print Left(“Visual Basic”, 6) Visual
11. RIGHT :
The Right Function returns characters from the right-
hand side of string.
SYNTAX:
Right(String, Length)
o Where string is the string from which you
are returning characters.
o Length is the number of characters to
return.

EXAMPLE: RESULT:
Print Right(“Visual Basic”, 6) Basic

12. TRIM:
The Trim functions strips leading and trailing spaces from
a string.
SYNTAX:
Trim(String)
EXAMPLE:
Print Trim(“Visual Basic “) & Len (Trim(“Visual Basic”))
RESULT:
Visual Basic 12.

13. CINT:
It converts the string into Integer.
SYNTAX:

CINT(Text Box) CINT(INPUTBOX(“ “))


EXAMPLE:
CInt(Text1.Text)
CInt(IntegerBox(“Enter the Value”))
14. CDBL:
It converts the string value to double.
SYNTAX:
Cdbl(TextBox)
Cdbl(InputBox(“ “))
EXAMPLE:
Cdbl(Text2.Text)
Cdbl(InputBox(“Enter your Salary”))

15. CSNG:
It converts the string value to single.

SYNTAX:
Csng(TextBox)
Csng(InputBox(“ “))

EXAMPLE:
Csng(Text1.Text)
Csng(InputBox(“Enter your age”))

16. CLNG:
It converts the string value to long.

SYNTAX:
Clng(TextBox)
Clng(InputBox(“ “))
EXAMPLE:
Csng(Text1.Text)
Csng(InputBox(“Enter your ID”))

17.STRING :
The string function returns a repeating character string of
the length specified.

SYNTAX:
String (Number, String Expression $)

➢ Where number is the length of the returned string.


➢ Character is the character code specifying the
character or string expression whose first character is
used to build the return string

EXAMPLE:
Dim Str As String
Str= String(20,”*”)
Print Str
RESULT:
********************

18.RND:
Returns a random number less than 1 but greater than or
equal to 0.

SYNTAX:

Rnd
EXAMPLE:

Dim I As Integer
For I=1 to 5
Print Rnd
Next I
End Sub

RESULT:

1
2
3
4
5

19.LIKE:
The Like function lets you to compare the strings using
wildcards.

SYNTAX:
If(Condition) Like

EXAMPLE:
If(“0-9”) Like A

20.INSTR:
The Instr function returns the first position of a string
inside another string. If the substring isn’t found Instr
returns 0.
SYNTAX:
Instr(<Start Position>, String 1, String 2, Compare)
▪ Where start position is a numeric
expression that sets the start position for
the search of substring in string. If Start
position is omitted, Instr begins searching
at the first position 1.
▪ String is the string being searched
▪ Substring is the string you are looking for
in string.
EXAMPLE:
Instr(1, “Visual Basic”, “Basic”, VbBinaryCompare)
RESULT:
6
21.INSTRREV:
This function returns the position of a string after a
substring.
SYNTAX:
Instrrev(Original String, sub String, Start Index, Compare)
EXAMPLE:
Instrrev(“Sis.Vbp”,”.”, Len(“sis.vbp”), Vbcompare)

22.VAL:
This function will convert string into integer. It is also
called blessing. It is equal to variant datatype.

SYNTAX:

Val(TextBox)

EXAMPLE:
Val(Text6.Text)
23.ROUND:
The round function that used to round of the numeric
values in visual basic statements.

SYNTAX:
Round(Expression[,number of decimal places])

Here the optional second parameter allows the user to


round the decimal point .If it is left become an Integer.

EXAMPLE:
Round(3.7) → 4
Round(3.75, 1) → 3.76
Round(3.786, 2) → 3.789= 3.79

PROCEDURES:
➢ A Procedure is a set of one or more program
statements that can be executed by referring to the
procedure name.
➢ Calling refers to a program statement that instructs
the compiler to execute a set of statements stored as a
procedure.
➢ The data values that the calling application supplies
to procedures are known as arguments.

ADVANTAGES OF USING PROCEDURES:


• The application is easier to debug because you
can easily trace the source of an error to a
single procedure.
• The code in a procedure I reusable because
once the procedure has been tested and stored.
It can be called from any module.
TYPES OF PROCEDURES:
1. Sub Procedure
2. Function procedure

1. SUB PROCEDURE:
Sub procedures can be further divided into two types.
➢ General
➢ Event

GENERAL PROCEDURE:
These are special purpose blocks of code that perform a
single task. You can create general procedure in all types of modules.
They can perform tasks common to all the application such as
connecting to a database.
SYNTAX:

(Public/Private) Static Sub <Procedure Name>[arguments]


Statements
End sub
EXAMPLE:
Public Sub fnsqrt (num as Integer)
Print sqr(num)
End Sub
EVENT PROCEDURE:
Event procedure is a block of code that are executed when
a specific event occurs, such as a command button is clicked or a
form is loaded into the memory. Events are associated with both
controls and forms.

SYNTAX:
Private sub <object name_event name> ([arguments])
Statements
End Sub

EXAMPLE:
Private Sub CmdAdd_Click( )
Txtno.Font.Italic= True
End Sub

FUNCTION PROCEDURES:
A Function procedure, like a sub procedure is used to
execute a set a statements. It can take arguments and also change the
value of the arguments, but unlike sub procedures. It can return values
to the calling procedures.

SYNTAX:
[Private/Public] Static Function [as<Function_Name> As <Data …..
Type>]
Statements
End Function
EXAMPLE:
Public Function fnsqrt(num As Integer) As Double
FnSqrt= sqr(num)
End Function

You might also like