Sunday, March 29, 2009

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.

Sunday, March 22, 2009

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. Whenever you run the program, you will get an output of 10 random numbers between 0 and 1.

Example 1:

Private Sub Form_Activate

For x=1 to 10

Print Rnd

Next x

End Sub


For more info on Mathematical Functions, please refer to


http://www.vbtutor.net/lesson11.html







Thursday, March 12, 2009

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")

The first InputBox will return a string and the second InputBox will return an Integer. You can write error handling code to deal with wrong entry of data type using On Error Goto Error_Handler statement. For Example, if the user enters student Mark instead of student name into the first InputBox, you can write the error handling code as follow, which include the usage of the MsgBox function:

StudentName=InputBox("Enter Student Name")
On Error Goto Error_Handler

Error_Handler:

MsgBox "You have entered wrong data, please enter the student name again"

For more info on InputBox and MsgBox, please refer to Vbutor.net lesson 10:


http://www.vbtutor.net/lesson10.html