Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Friday, May 27, 2011

VB-13 : Set Focus(If-Then-Elseif-Endif)


Elseif clause is a variation of IF..THEN.....ELSE statement.By using this , we can control program flow.

Here, let be a field in the table as "age". Let the value in age cannot be less than 10 and greater than 60.So the code do check the value in the text box control named text1 is can be placed in the lost focus event of the text1 control.

A)   Property :
Textbox


B)   Attaching Code to the Object :
Private Sub Text1_LostFocus()
If Val(Text1.Text) < 10 Then
MsgBox "age cannot be less than 10"
Text1.SetFocus
ElseIf Val(Text1.Text) > 60 Then
MsgBox "age cannot be greater than 60"
Text1.SetFocus
Else
MsgBox "age entered correctly"
End If
End Sub

You can add any number of ElseIF clauses as you need to provide alternative choices.A better way to choose between several alternatives is the Select Case statement as in example VB-14 : Making decision with Select Case


               VB-11 : Set Focus(If-Then-Endif)
               VB-12 : Set Focus(If-Then-Else-Endif)




=========================================================================
Back to Home   &  VB

VB-12 : Set Focus(If-Then-Else-Endif)

Sometimes you have to run certain statements IF A condition is true and run others IF A condition is false.
Then you can use an IF...THEN.....ELSE statement to define two blocks of executable ststements:
One block to run if the condition is TRUE,
and the other block to run if the condition is FALSE.


A)   Property :

Textbox

B)   Attaching Code to the Object :


Private Sub Text1_LostFocus()
If Text1.Text = " " Then
MsgBox "Sorry!name cannot be left blank"
chance = chance + 1
Text1.SetFocus
Else
MsgBox "Great!name was successfully entered"
End If
End Sub



Refer : VB-10 : Set Focus(If-Then)



=========================================================================
Back to Home   &  VB

VB-11 : Set Focus(If-Then-Endif)

To run more than one line of code,you must use multiple line(or block)syntax.
This syntax includes the ENDIF statements;


A)   Property :
Text Box (Same as VB-10)


B)   Attaching Code to the Object :


Private Sub Text1_LostFocus()
If Text1.Text = " " Then
MsgBox "Sorry!name cannot be left blank"
chance = chance + 1
Text1.SetFocus
End Sub


Refer : VB-10 : Set Focus(If-Then)



=========================================================================
Back to Home   &  VB

VB-10 : Set Focus(If-Then)

This program does not allow the user to move out of the text box till the user enters some value in it.
This example omits the ELSE and the END IF keywords.


A)   Property :
Text Box : NAME-Text1, TEXT-(Should be kept blank)

B)   Attaching Code to the Object :


Private Sub Text1_LostFocus()
If Text1.Text = " " Then MsgBox "Sorry!name cannot be left blank"
Text1.SetFocus
End Sub

Note : To run more than one line of code,we must includes the ENDIF statements.Refer VB-11 : Set Focus(If-Then-Endif) in which we are using ENDIF
==========================================

Refer : VB-11 : Set Focus(If-Then-Endif)

=========================================================================
Back to Home   &  VB

VB-9 : Print name 10 times(For-Next Loop)


Refer Print "Hello" 10 times(Do-While) also.

We need 2 command buttons.
The output will print names of first 10 students in the array batch on the click event(For-NextLoop)

A)   Property :
Command button : NAME- Command1 , CAPTION- Click
Command button : NAME- Command2 , CAPTION- Exit




B)   Attaching Code to the Object :
Private Sub Command2_Click()
End
End Sub

Private Sub Form_Click()
Dim i As Integer
For i = 0 To 9
Print Batch(i)
Next i
End Sub




=========================================================================
(Check the output)


Back to Home   &  VB

VB-8 : Print "Hello" 10 times(Do-While)


Refer Print name 10 times(For-Next Loop) also.

We need 2 command buttons.
The output will print "Hello" 10 times(Do-While Loop)

A)   Property :
Command button : NAME- Command1 , CAPTION- Click
Command button : NAME- Command2 , CAPTION- Exit




B)   Attaching Code to the Object :
Private Sub Command1_Click()
Dim num, count
num = 10
count = 0
Do While count < num
Print "Hello"
count = count + 1
Loop
End Sub

Private Sub Command2_Click()
End
End Sub


=============================================
Back to Home   &  VB