Thursday, May 27, 2010

Search for text files in your drives and display them

This program let you choose your drives and folders and displays all the text files from the chosen folder in a list box. You need to place a combo box, a DriveListBox, a DirListbox and a Listbox into your form.
The code is as follows:

Private Sub Form_Load()
Left = (Screen.Width - Width) \ 2
Top = (Screen.Height - Height) \ 2
Combo1.Text = "All Text Files"
Combo1.AddItem "All Text Files"
Combo1.AddItem "All Files"
End Sub

Private Sub Combo1_Change()
If ListIndex = 0 Then
File1.Pattern = ("*.txt")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub

Scientific Calculator

The value of pi is now obtained by using the Atn function (Arc Tangent). We know that Tan(pi/4)=1, so Atn(1)=pi/4, therefore pi=Atn(1)*4. By using this function, we will obtain a more accurate value of pi. 

In this project, we shall also limit the numbers to be less than 30 digit. If any user enters a number with more than 30 digits, an error message "data overflow" will be shown. 
The Interface:

Excel VBA Objects

Most programming languages today deal with objects, a concept called object oriented programming. Although Excel VBA is not a truly object oriented programming language, it does deal with objects. VBA object is something like a tool or a thing that has certain functions and properties, and can contain data. For example, an Excel Worksheet is an object, cell in a worksheet is an object, range of cells is an object, font of a cell is an object, a command button is an object, and a text box is an object and more.

An Excel VBA object has properties and methods. Properties are like the characteristics or attributes of an object. For example, Range is an Excel VBA object and one of its properties is value. We connect an object to its property by a period(a dot or full stop). The following example shows how we connect the property value to the Range object.

InputBox and MsgBox

The InputBox function and the MsgBox function are two very important functions in VB. The InputBox function is to accept data from the user and the MsgBox function is to output data to the user. They are considered a form of simple dialog box for the user to interact with the VB program.
The Inputbox function with return a value of any data type, which means the value could be integer,single, string, boolean ,data and more. Normally you need to declare the data type for values to be accepted in the Inputbox. 

Example:
Dim StudentName As String
Dim Mark As Integer
StudentName=InputBox("Enter Student Name")
Mark=InputBox("Enter Student Mark")

VB Mathematical Functions

Mathematical functions are very useful and important in programming because very often we need to deal with mathematical concepts in programming such as chance and probability, variables, mathematical logics, arithematic calculations, coordinates system, time intervals and more. Some of the common mathematical functions in Visual Basic are Rnd, Sqr, Int, Abs, Exp, Log, Sin, Cos, Tan , Atn, Fix and Round. 

For example, Rnd is very useful when we deal with the concept of chance and probability. The Rnd function returns a random number between 0 and 1. In Example 1.

Monday, May 24, 2010

The use of Option Exlicit in VB program

The use of Option Explicit is to help us to track errors in the usage of variable names within a VB program code. For example, if we commit a typo, Visual Basic will pop up an error message “variable not defined”. Indeed, Option Explicit forces the programmer to declare all the variables using the Dim keyword. It is a good practice to use Option Explicit because it will prevent us from using incorrect variable names due to typing errors, especially when the program gets longer. With the usage of Option Explicit, it will save our time in debugging our programs. For example, In the following program, a typo was committed, EmployeID, which was declared as EmployeeID.

Option Explicit
Private Sub Command1_Click()
Dim EmployeeID As String
EmployeID = "John"
Text1.Text = EmployeeID
End Sub 


When you run the program, a dialog box with an error message 'Variable not defined" will appear ,when you click the OK button, it will show the highlighted typo, enabling us to correct the mistake.

Thursday, May 20, 2010

Adding Menus to Your VB Application

Most windows programs provide menu bar for easy navigation and control. The most common items are like File, Edit, View, Tools, Help and more. Each item on the main menu bar also provide a list of options in the form of a pull-down menu. 

Adding menu bar is easily accomplished in Visual Basic. To start adding menu items to your application, open your file and then click on Tools in the menu bar of the Visual Basic IDE, then select Menu Editor. When you click on the Menu Editor, the following window will appear. In the Menu Editor, key in the items such as File, Edit, View and etc

Monday, May 10, 2010

Creating a Calender in VB6

Creating a calender in VB6 is a relatively simple task. There is no need to write your own code. What you do is insert the calender component of an ActiveX Control that comes with VB6 into the form. However, the ActiveX control is not included into the toolbox by default, you have to add it with the following steps:
1. From the Project menu, choose Components
2. In the Components dialog, select Microsoft Windows Common Controls 2-6.0 and click OK.
The Calender control will be added to the toolbox. 
3. Select the MonthView control icon from the Visual Basic ToolBox 
4. Drag the MonthView control into the form. 
Now run the program and there you have your very own calender!
 

There is another way to create a calender in VB. This time you use the Microsoft Calender Control 11. As this is also an ActiveX control, it is not launched into the toolbox by default, you need to add it in using the method similar to the above example.
1. From the Project menu, choose Components
2. In the Components dialog, select Microsoft Calender Control 11 and click OK.
Now the calender control will appear in the toolbox. Drag it into the form and press F5 to run the program, and you have your calender as shown below:
 
Try them out yourself. Everyone can do it!

Saturday, May 8, 2010

Function to calculate the sum of numbers between 1 and 100 that are divisible by 4

Function SumOfNum(ByVal x As Integer) As Integer 

Dim i As Integer
SumOfNum = 0
For i = 1 To 100
If i Mod x = 0 Then
SumOfNum = SumOfNum + i
Else
SumOfNum = SumOfNum
End If
Next i 

End Function 

Private Sub Command1_Click()
Dim w As Integer
x = Text1.Text
w = SumOfNum(x)
Label1.Caption = w 

End Sub

Friday, May 7, 2010

Temperature Converter


With winter around and daily temperature change has become a concern for many people especially those who travel a lot. They are constantly checking out temperature readings everyday However, different parts of the world often use different temperature scales, some of them use Fahrenheit and some use Celsius, so the readings can be confusing. Therefore, it is nice if we have a program that can convert Celsius to Fahrenheit and vice versa. And I have done just that using Visual Basic 6. The code is like this:

Private Sub Command1_Click()
Dim cel, Fah As Single
cel = Val(txtC.Text)
Fah = Val(txtF.Text)

If txtC.Text <> "" And txtF = "" Then
Fah = ((9 / 5) * cel + 32)
txtF.Text = Round(Fah, 1)
Else
cel = (5 / 9) * (Fah - 32)
txtC.Text = Round(cel, 1)
End If

End Sub

Private Sub Command2_Click()
txtC.Text = ""
txtF.Text = ""

End Sub

Wednesday, May 5, 2010

Changing Background and Foreground Color at Run Time for VB6

It is very easy to change background color and foreground color at run time, just use the code below:
Private Sub Form_Load()
Form1.Show
Form1.BackColor = vbRed
Form1.ForeColor = vbBlue
Form1.Print "Foreground"
End Sub