Wednesday, 7 March 2012

Using the Treeview Control

Private Sub Form_Load()
Dim tempnode As Node
Set tempnode = TreeView1.Nodes.Add(, , "mykey", "Health")
tempnode.Image = 1
tempnode.SelectedImage = 1
tempnode.ExpandedImage = 1
Set tempnode = TreeView1.Nodes.Add("mykey", tvwChild, "E", "East Division")
Set tempnode = TreeView1.Nodes.Add("E", tvwChild, "B", "Big")
Set tempnode = TreeView1.Nodes.Add("E", tvwChild, "s", "Small")
Set tempnode = TreeView1.Nodes.Add("E", tvwChild, "St", "Strong")
Set tempnode = TreeView1.Nodes.Add("E", tvwChild, "Ta", "Tall")
Set tempnode = TreeView1.Nodes.Add("E", tvwChild, "ft", "Fit")
Set tempnode = TreeView1.Nodes.Add("mykey", tvwChild, "C", "Central Division")
Set tempnode = TreeView1.Nodes.Add("mykey", tvwChild, "W", "West Division")
tempnode.EnsureVisible
End Sub

Building a Listview Control

A Listview uses an imagelist to create groups and assign numbers to them as per list criterias. Columnheaders are created and listitems and list subitems are added to them.
ListView1.Icons = ImageList1
ListView1.SmallIcons = ImageList2
ListView1.ColumnHeaderIcons = ImageList3
Dim clmx As ColumnHeader
Set clmx = ListView1.ColumnHeaders.Add(, , " EL Magazine Member Groups", , , 1)
Set clmx = ListView1.ColumnHeaders.Add(, , "Free", , , 2)
Set clmx = ListView1.ColumnHeaders.Add(, , "Subscription", , , 3)
Dim itmx1 As ListItem
Dim itmx2 As ListItem
Dim itmx3 As ListItem
Set itmx1 = ListView1.ListItems.Add(, "NR", "North", 1, 1)
itmx1.Selected = False
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , "14", 0
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , "25", 0
Set itmx2 = ListView1.ListItems.Add(, , "East", 1, 1)
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , "04", 0
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , "28", 0
Set itmx3 = ListView1.ListItems.Add(, , "West", 1, 1)
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , "12", 0
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , "15", 0
     
ListView1.View = lvwReport
End Sub

The File List Box User Control

The File List Box user control combines a file list, a drive list and a directory list to display the file name in a text box.
Private Sub File1_Click()
Dim FileName As String
FileName = File1.Path
If Right$(FileName, 1) <> "\" Then FileName = FileName & "\"
FileName = FileName & File1.FileName
Text1.Text = FileName
End Sub
Private Sub Drive1_Change()
 ' The Drive property also returns the volume label, so trim it.
 Dir1.Path = Left$(Drive1.Drive, 1) & ":\"
 End Sub

 Private Sub Dir1_Change()
 File1.Path = Dir1.Path
 End Sub

A User Control to Check Textbox data

A user control is used to check the data being entered in a text control for alphabet characters and throws an error when other characters are entered in it.

Private Sub Text1_LostFocus()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim str As String
str = Trim(Text1.Text)
k = 0
l = 0
For i = 1 To Len(str)
For j = 65 To 122
If Mid(str, i, 1) = Chr(j) Then
k = k + 1
End If
Next j
If Mid(str, i, 1) = Chr(32) Then
l = l + 1
End If
Next i
'MsgBox "K: " & k
'MsgBox Len(str)
'MsgBox str
k = k + l
If k < Len(str) Then
MsgBox "Please Enter Valid Characters"
End If
End Sub

A Form with a Status Bar, Progress Bar and an Error Object

A status bar can be used to display several form and user key stats such as caps lock, scroll lock, INS key and to display the system time. A timer object is used to create an auto increment for the time object to be placed in the status bar.
time1 = Time + Timer1.Interval
time1 = Format(time1, "HH:MM:SS")
StatusBar1.Panels(6).Text = time1
A progress bar uses an increment value in a timer object to display form load progress.
Private Sub Timer2_Timer()
If ProgressBar1.Value < 10 Then
ProgressBar1.Value = ProgressBar1.Value + 1
ElseIf ProgressBar1.Value = 10 Then
ProgressBar1.Value = 0
Timer2.Enabled = False
ProgressBar1.Visible = False
End If
The error object traps errors in the form for an animation object:
'On Error GoTo FileOpenError
On Error Resume Next
Animation1.Open ("c:\Users\PC\Desktop\photos\MVI_1230.avi")
Animation1.AutoPlay = True
If Err Then
MsgBox Err.Description & vbCrLf & Err.Number
End If
Timer1.Enabled = True
MsgBox "Total Message"
FileOpenError:
MsgBox "The Avi file is not ok. Please Try Another File"
Option Explicit
Dim time1 As Date
'Dim sbr1 As sbrTime
Private Sub Form_Load()
time1 = Format(Time, "HH:MM:SS")
'Text1.Text = sbr1
With StatusBar1
.Panels.Remove 1
.Panels.Add 1, "my key", "Form 6", sbrText
.Panels(1).AutoSize = sbrSpring
.Panels(1).Text = Form4.Caption
'sbrTime = time1
.Panels.Add , , , sbrCaps
.Panels.Add , , , sbrNum
.Panels.Add , , , sbrIns
.Panels.Add , , , sbrScrl
.Panels.Add 6
End With
'On Error GoTo FileOpenError
On Error Resume Next
Animation1.Open ("c:\Users\PC\Desktop\photos\MVI_1230.avi")
Animation1.AutoPlay = True
If Err Then
MsgBox Err.Description & vbCrLf & Err.Number
End If
Timer1.Enabled = True
MsgBox "Total Message"
FileOpenError:
MsgBox "The Avi file is not ok. Please Try Another File"
End Sub
Private Sub Timer1_Timer()
time1 = Time + Timer1.Interval
time1 = Format(time1, "HH:MM:SS")
StatusBar1.Panels(6).Text = time1
Text1.Text = Time + Timer1.Interval
'Text1.Text = Format(Text1.Text, "HH:MM:SS")
'Text1.Text = FormatDateTime(Text1.Text, vbGeneralDate)
'Text1.Text = Format(time, "HH:MM:SS")
End Sub
Private Sub Timer2_Timer()
If ProgressBar1.Value < 10 Then
ProgressBar1.Value = ProgressBar1.Value + 1
ElseIf ProgressBar1.Value = 10 Then
ProgressBar1.Value = 0
Timer2.Enabled = False
ProgressBar1.Visible = False
End If
End Sub

A Module Object for Automated Customer ID Generation

The Customer.bas module has a procedure named customer_ID_auto to generate automated customer ID numbers to be loaded into the customer form on form load. The call Customer_ID_auto() statement is used to invoke this method: Private Sub Form_Load()
Call customer_ID_auto
Text1.Text = str
End Sub
in the Form_Load sub procedure. The str string is public and hence the value is populated in the form text field on form load.
The Customer.bas module:
Option Explicit
Public str As String
Sub customer_ID_auto()
Dim rvalue As Integer
Dim upperbound As Integer
Dim lowerbound As Integer
Dim i As Integer
' Initialize the random-number generator.
Randomize
' Generate random value between 1 and 6.
upperbound = 65
lowerbound = 90
'rvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
str = "CMP"
For i = 1 To 8
rvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
str = str + Chr(rvalue)
Next i
'Form6.List1.AddItem str
End Sub

Creating a Customer Form using the Customer Class

The customer form shown on the left uses the customer class object given below to retrieve and update data in tables stored in a database.Option Explicit
Private Sub Command1_Click()
Dim clsCust As New clsCustomerOne
clsCust.CustomerID = Text1.Text
clsCust.CustomerName = Text2.Text
clsCust.CustomerAddress = Text3.Text
clsCust.CompanyName = Text4.Text
clsCust.CompanyAddress = Text5.Text
clsCust.PhoneNumberR = Text8.Text
clsCust.PhoneNumberM = Text9.Text
clsCust.RegistrationDate = Text6.Text
clsCust.RegistrationNumber = Text7.Text
clsCust.Customer_ID_Date = MonthView1.Value
clsCust.customer_update
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Call customer_ID_auto
Text1.Text = str
End Sub