Wednesday, 29 February 2012

If Constructs...Select Case

If constructs are designed to execute when the condition turns true.
If D>10 then
Msgbox d
End If
if d>10 then
Msgbox d
ElseIf d=0 then
Msgbox d
else
Msgbox d
End If

Select case statements are written to make choices from different case values:
Select case D
Case IS<0
Msgbox D
Case 1,2,3
Msgbox D
Case 5 to7
Msgbox D
Case 8 to 11
Case IS>30
Msgbox D
End Select

Monday, 27 February 2012

Loops in Visual Basic

Visual basic uses three types of loops. Namely:
1) For Loops
2) Do Loops
3) Enumeration Loops- For Each

For loops use an auto increment of the form:
For i=1 to 10
Avar=i & "I's increment Value is " & (i+1)
Form1.print Avar
Next i

Do While statements are loops executing when the condition is true:
i=0
Do while(i<10)
Form.print " "&i
i=i+1
Loop

Dim item as Variant
Do Until rsRecord.EOF
Form1.Print rs.Fields(0)
Loop
nArray=Array(3,6,4,2,6,7,9)
For Each item in nArray
if item>6 Then Form1.Print item
Next item

Monday, 6 February 2012

Using an Inet Control to Retreive a Website HTML

The following program uses a Inet control to retrieve the HTML in a website and stores it in a text file on the desktop.
Private Sub Form_Load()
Dim strURL As String      ' URL string
Dim intFile As Integer   ' FreeFile variable
intFile = FreeFile()
strURL = "http://www.microsoft.com"
Open "C:\Users\PC\Desktop\MSsource.txt" For Output _
As #intFile
Write #intFile, Inet1.OpenURL(strURL)
Close #intFile
End Sub

An Interest Calculator Using a TextBox, a ListBox and a ComboBox

The following example uses a TextBox, a ListBox and a ComboBox to build an interest calculator. The principal amount is loaded on form startup in a  textbox. The ListBox is populated with interest values. The ListBox displays the interest amount earned on the different interest percentages on a principal amount. The interest values are selected from a ComboBox.
Dim principal As Integer
Dim interest1 As String
Dim interest2 As String
Dim interest3 As String
Private Sub Combo1_Click()
'List1.AddItem (principal * interest1 / 100)
If (Combo1.ListIndex = 0) Then
List1.AddItem (principal * interest1 / 100)
ElseIf (Combo1.ListIndex = 1) Then
List1.AddItem (principal * interest2 / 100)
Else
List1.AddItem (principal * interest3 / 100)
End If
End Sub
Private Sub Form_Load()
Text1.Text = 2000
principal = Val(Text1.Text)
interest1 = 9.75
interest2 = 10.5
interest3 = 11
Combo1.AddItem (interest1)
Combo1.AddItem (interest2)
Combo1.AddItem (interest3)
End Sub
Private Sub Text1_LostFocus()
principal = Val(Text1.Text)
End Sub

Using a Combo Box and a List Control

The following program demonstrates a combo box and a list control. The combo box is populated with items and is toggled with the list control with its given items.
Option Explicit
Dim i As Integer
Dim j As Integer
Dim arr() As String
Dim jrr() As String
Dim krr() As String
Private Sub Command1_Click()
'Combo1.RemoveItem (1)
'Combo1.RemoveItem (2)
'Combo1.RemoveItem (3)
Combo1.Clear
Combo1.Refresh
Combo1.AddItem "Shoes"
Combo1.AddItem "Tie"
Combo1.AddItem "Shirt"
i = Combo1.ListCount
ReDim jrr(i)
For i = 0 To Combo1.ListCount
jrr(i) = Combo1.List(i)
Next
List1.Clear
For i = 0 To Combo1.ListCount
List1.AddItem Combo1.List(i)
Next
End Sub
Private Sub Command2_Click()
If j = 0 Then
krr = arr
j = 1
Else
krr = jrr
j = 0
End If
List1.Clear
For i = 0 To Combo1.ListCount
List1.AddItem krr(i)
Next
End Sub
Private Sub Form_Load()
j = 0
Combo1.Clear
Combo1.Refresh
Combo1.AddItem "Mango"
Combo1.AddItem "Orange"
Combo1.AddItem "Custard Apple"
i = Combo1.ListCount
ReDim arr(i)
For i = 0 To Combo1.ListCount
arr(i) = Combo1.List(i)
Next
For i = 0 To Combo1.ListCount
List1.AddItem Combo1.List(i)
Next
End Sub