This is a blog that discusses anything related to Visual Basic and provides updates for VISUAL BASIC TUTORIAL website. Your comments and suggestions are welcome here. Thank you.
Friday, November 20, 2009
Visual Basic- A personal experience Part 2
There was no hard disk, you need a boot diskette to start it, then you insert a 5 and a half inch floppy disk that contain the BASIC program to start the BASIC DOS-based IDE. I can't remember what version of BASIC was it, could be GWBASIC, a dialect of BASIC developed by Microsoft from BASICA, originally for Compaq.
Tuesday, November 10, 2009
Visual Basic- A personal experience
10 Let X=100
20 Let Y =200
30 Z=X+Y
40 Print Z
Monday, October 26, 2009
Visual Basic for Business
Monday, September 21, 2009
New Lesson- Using the Printer
In previous lessons, we have only written programs that send output to the screen and not the printer. In this new lesson, we will learn how to send an output to the printer and get it printed. Sending output to the printer is a simple task in Visual Basic, it involves the use of the Printer object and the print method. The standard code of sending an output to the printer and get it printed out is as follows:
Private Sub Form_Load()
Printer.Print "Welcome to Visual Basic"
End Sub
For more details, refer to
http://www.vbtutor.net/lesson39.html
Wednesday, September 2, 2009
New Lesson!
Thursday, August 27, 2009
Keyboard Handling
Keyboard handling is relatively easy in VB6. There are basically only three events associated with the keyboard, i.e. KeyPress, KeyUp and KeyDown. You can write code for each of the aforementioned events. Before you can do that, you need to know the ASCII code as well as the chr function. ASCII stands for American Standard Code for Information Interchange. Each character or symbol on a keyboard is represented by a numerical code (ASCII code) which can be understood by the computer. It may be even an action such as Enter or Backspace. For a complete set of ASCII code, please refer to this link below:
http://www.asciitable.com/.
On the other hand, the Chr function is to convert an ASCII code into a meaningful character which can be undertood by a human being.
A standard VB code for handing keyboard is shown in the example below:
Private Sub Form_KeyPress(KeyAscii As Integer)
Print Chr(KeyAscii)
End Sub
The program will display the correponding character on the form as the user press a key on the keyboard that sends the ASCII code to the computer and the VB program convert that code into a character and display it on the form, it is that simple!
More to come.
Sunday, July 26, 2009
New lesson for Excel VBA Tutorial
Sunday, July 19, 2009
The Trigonometric Functions
I have just Updated lesson 11 with some trigonometric functions.
The common trigonometric functions are Sin, Cos, Tan and Atn.
a) Sin is the function that computes the value of sine of an angle in radian.
b) Cos is the function that computes the value of cosine of an angle in radian.
c) Tan is the function that computes the value of tangent of an angle in radian.
d) Atn is the function that computes the value of arc tangent of an angle in radian.
An angle in degree has to be converted to radian before it can be calculated by the above trigonometric functions. From high school mathematics, we know that π radian is equivalent to 180°; which means 1 radian is equivalent to π divided by 180. Therefore, in order to convert an angle x from degree to radian, we have to multiply x by (π/180). However, there is a small problem because it is rather difficult to obtain the precise value of π, fortunately, there is a way to do it in VB. First of all, we know that an arc tangent of 1 will return the value of 45° which is π/4 radian. So, to obtain the value of π, just multiply the arc tangent of 1 with 4. Let’s examine how all the above calculations can be done in the following example:
In this example, the program will display the values of sine, cosine and tangent for various angles in degree between 0° and 360° in a table form . The value of π is obtained using the equation pi=4*Atn(1). The angle in degree is converted to radian by multiplying the angle by (π/180). Different angles are obtained through the use of For...Next Loop. The program is shown below
Private Sub Form_Activate ()
pi = 4 * Atn(1)
Print "angle", "Sin x", "Cos x", "Tan x"
For degree = 0 To 360 Step 30
angle = degree * (pi / 180)
Print degree, Round(Sin(angle), 4), Round(Cos(angle), 4), Round(Tan(angle), 4)
Next degree
End Sub
The Output
Saturday, July 11, 2009
Scientific Calculator
I will also replace the display panel with a label so that the users won't be able to erase the number. They can only clear the number by clicking the cancel button. The alignment property of the label is set to right justified, so that the digit entered will start from the right position, similar to the conventional 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:
The code :
Dim Num_of_digit As Integer
Private Sub buttonNum_Click(Index As Integer)
If Num_of_digit > 0 And Num_of_digit <> "0" Then
panel.Caption = panel.Caption + Right$(Str$(Index), 1)
ElseIf panel.Caption = "0" Then
panel.Caption = Str$(Index)
End If
ElseIf Num_of_digit >= 30 Then
panel.Caption = "Data Overflow"
Else
panel.Caption = Right$(Str$(Index), 1)
End If
Num_of_digit = Num_of_digit + 1
End Sub
Private Sub Clear_Click()
panel.Caption = "0"
Num_of_digit = 0
End Sub
Private Sub Command1_Click(Index As Integer)
Dim x As Double
Dim pi As Double
pi = Atn(1) * 4
Select Case Index
Case 0
x = Val(panel.Caption)
panel.Caption = Round((Sin(x * pi / 180)), 4)
Num_of_digit = 0
Case 1
x = Val(panel.Caption)
panel.Caption = Round((Cos(x * pi / 180)), 4)
Num_of_digit = 0
Case 2
x = Val(panel.Caption)
panel.Caption = Round((Tan(x * pi / 180)), 4)
Num_of_digit = 0
End Select
End Sub
Friday, July 3, 2009
New Project-Creating a Scientic Calculator
Right now, take a look at the simple interface.
The code :
Private Sub Command1_Click(index As Integer)
Select Case index
Dim x As Double
Case 0
x = Val(Text1.Text)
Text1.Text = Round((Sin(x * 3.14159265 / 180)), 4)
Case 1
x = Val(Text1.Text)
Text1.Text = Round((Cos(x * 3.14159265 / 180)), 4)
Case 2
x = Val(Text1.Text)
Text1.Text = Round((Tan(x * 3.14159265 / 180)), 4)
End Select
End Sub
Saturday, June 20, 2009
String Handling Functions-InStr
The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The format is
Instr (n, original phase, embedded phrase)
Where n is the position where the Instr function will begin to look for the embedded phrase.
For example
Instr(1, “Visual Basic”,” Basic”)=8
Wednesday, June 10, 2009
New Book Release! Title: Excel VBA Made Easy
I have just published another e-book today. The title is "Excel VBA Made Easy- A Concise Guide for Beginners". This book is written for the benefits of readers who are keen to master Excel VBA in a short time. It is written in plain English so everyone should find it easy to understand. Plenty of examples are presented in the book. It is a must-have Excel VBA guide book for beginners and even intermediate learners!
Please go to the link below to download the book, it is selling at $15.00
http://www.vbtutor.net/VBA/vba_tutorial.html
Monday, June 1, 2009
Check Box
The Check Box control lets the user selects or unselects an option. When the Check Box is checked, its value is set to 1 and when it is unchecked, the value is set to 0. You can include the statements Check1.Value=1 to mark the Check Box and Check1.Value=0 to unmark the Check Box, as well as use them to initiate certain actions. For example, the program will change the background color of the form to red when the check box is unchecked and it will change to blue when the check box is checked.
Example 3.4
Private Sub Command1_Click()
If Check1.Value = 1 And Check2.Value = 0 Then
MsgBox "Apple is selected"
ElseIf Check2.Value = 1 And Check1.Value = 0 Then
MsgBox "Orange is selected"
Else
MsgBox "All are selected"
End IfEnd Sub
Tuesday, May 19, 2009
Excel VBA Objects
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.
Example 8.1
Private Sub CommandButton1_Click()
Range("A1:A6").Value = 10
End Sub
In this example, by using the value property, we can fill cells A1 to A6 with the value of 10. However, because value is the default property, it can be omitted. So the above procedure can be rewritten as
Example 8.2
Private Sub CommandButton1_Click()
Range("A1:A6")= 10
End Sub
To learn more about Excel VBA, log on to VBA tutorial site:
http://www.vbtutor.net/VBA/vba_tutorial.html
Saturday, May 9, 2009
Excel VBA
VBA is the acronym for Visual Basic for Applications. It is an integration of the Microsoft's event-driven programming language Visual Basic with Microsoft Office applications such as Microsoft Excel, Microsoft Word, Microsoft PowerPoint and more. By running Visual Basic IDE within the Microsoft Office applications, we can build customized solutions and programs to enhance the capabilities of those applications.
Among the Visual Basic for applications, Microsoft Excel VBA is the most popular. There are many reasons why we should learn VBA for Microsoft Excel, among them is you can learn the fundamentals of Visual Basic programming within the MS Excel environment, without having to purchase a copy of Microsoft Visual Basic software. Another reason is by learning Excel VBA; you can build custom made functions to complement the built-in formulae and functions of Microsoft Excel. Although MS Excel has a lot of built-in formulae and functions, it is still not enough for certain complex calculations and applications. For example, it is very hard to calculate monthly payment for a loan taken using Excel's built-in formula, but it is relatively easy to program a VBA for such calculation. This book is written in such a way that you can learn VBA for MS Excel in an easy manner, and everyone shall master it in a short time!
You can program Excel VBA in every version of Microsoft Office, including MS Office 97, MS Office2000, MS Office2002, MS Office2003 and MS Office XP. The reason VBA is needed is due to the limitations in using the built-in functions of MS Excel and macro recording. By using VBA, you can build some very powerful tools in MS Excel, including financial and scientific applications such as getting financial data from the Internet as well as linear programming.
You can learn about Excel VBA in my VBA tutorial at:
http://www.vbtutor.net/VBA/vba_tutorial.html
Tuesday, May 5, 2009
Prime Number Tester
In this program, I use the Select Case ......End Select statement to determine whether a number entered by a user is a prime number or not. For case 1, all numbers that are less than 2 are prime. In Case 2, if the number is 2, it is a prime number. In the last case, if the number N is more than 2, I need to divide this number by all the numbers from 3,4,5,6,........up to N-1, if it can be divided by any of these numbers, it is not a prime number, otherwise it is a prime number. I use a Do......Loop While statement to control the program flow. Here I also used a tag="Not Prime' to identify the number that is not prime, so that when the routine exits the loop, the label will display the correct answer.
Please refer to the link below for the source code.
http://www.vbtutor.net/VB_Sample/Prime.htm
Thursday, April 23, 2009
Visual Basic Polling System
Survey and polling tools are often used in marketing or politics to assess
rating for certain services or products. Polling tools can take many forms, some just use a simple dichotomous scale of Yes and No, or Likert Scale that consists of three or more choices. You can create Polling tool in Visual Basic easily by using the option buttons. In my program, I give the users five choices, Excellent, Very Good, Good, Satisfactory and Bad. Then the results are presented in frequency and percentage respectively.
To learn how to program the above polling system in VB, please view my sample polling program at
Sunday, April 12, 2009
VB Snakes and Ladders game
To design the program in VB, it took me one day to think out the mathematical logics and wrote out the routines on paper (not in front of the computer). Since the board comprises 10 rows and 10 columns, each box thus represents a cell with the coordinate (column, row). Therefore, I figured out that I need to define the coordinate of every cell by declaring two arrays row(10) and col(10) at the beginning of the procedure. To move the chess piece, I employed the method object.move col(i), row(j), where I initiate the values of col(i) and row(j) in a For....Next loop. As the motion is in a zigzag manner, I need to control the motion using reverse order and by imposing some conditions.
To learn more about the program, check out the link below:
Snakes and Ladders Game
Saturday, April 4, 2009
Microsoft Multimedia Control
To be able to play multimedia files or multimedia devices, you have to insert Microsoft Multimedia Control into your VB applications that you are going to create. However, Microsoft Multimedia Control is not normally included in the default toolbox, therefore you need to add the MM control by pressing Ctrl+T and select it from the components dialog box that is displayed.
To learn how to create multimedia applications, please refer to the following lessons in Vbtutor.net.
http://www.vbtutor.net/lesson19.html
http://www.vbtutor.net/lesson20.html
http://www.vbtutor.net/lesson21.html
http://www.vbtutor.net/lesson22.html
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 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
Sunday, February 22, 2009
Another VB6 New Lesson-Creating Menus
To start viewing the new lesson, please click the link below:
http://www.vbtutor.net/lesson37.html
Happy learning!
Thursday, February 19, 2009
Creating a CD player
Please click the following link to view the code:
http://www.vbtutor.net/lesson19.html
Thursday, February 5, 2009
VBA-Some Basic Concepts
Many of you may not be familiar with VBA, or may not heard of it at all. In any case, I suggest you take up VBA programming seriously because it is so useful! Let me share with you some facts about VBA, particularly Excel VBA.
VBA is the acronym for Visual Basic for Applications. It is an integration of the Microsoft's event-driven programming language Visual Basic with Microsoft Office applications such as Microsoft Excel, Microsoft Word, Microsoft PowerPoint and more. By running Visual Basic IDE within the Microsoft Office applications, we can build customized solutions and programs to enhance the capabilities of those applications.
Among the Visual Basic for applications, Microsoft Excel VBA is the most popular. There are many reasons why we should learn VBA for Microsoft Excel, among them is you can learn the fundamentals of Visual Basic programming within the MS Excel environment, without having to purchase a copy of Microsoft Visual Basic software. Another reason is by learning Excel VBA; you can build custom made functions to complement the built-in formulae and functions of Microsoft Excel. Although MS Excel has a lot of built-in formulae and functions, it is still not enough for certain complex calculations and applications. For example, it is very hard to calculate monthly payment for a loan taken using Excel's built-in formula, but it is relatively easy to program a VBA for such calculation. This book is written in such a way that you can learn VBA for MS Excel in an easy manner, and everyone shall master it in a short time!
To learn more about VBA, please log on to
http://www.vbtutor.net/VBA/vba_tutorial.html
Sunday, February 1, 2009
Creating a Calender in VB6
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, January 31, 2009
A question from the Reader-Sum of Numbers 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
When you run the program, enter 4 into textbox 1 and you can get the answer. If you key in other numbers, you will get the sum of numbers divisible by that number. Anyone has better solution can email me or write in the comments of this blog.
Thursday, January 29, 2009
Creating Business and Accounting Software with VB
Dim Num, n As Integer
Dim I, P, PVIFA, r, pmt, PI, PP As Double
Public Sub Cmd_Calculate_Click()
P = Txt_Principal.Text
Num = Txt_Num_payment.Text
r = Txt_Interest.Text
I = r / 100
PVIFA = 1 / I - 1 / (I * (1 + I) ^ Num)
pmt = P / PVIFA
Lbl_Amtpayment.Caption = Round(pmt, 2)
End Sub
Private Sub Cmd_Create_Click()
End SubList_Amortization.AddItem "n" & vbTab & "Periodic" & vbTab & vbTab & "Payment" & vbTab & vbTab & "Payment" & vbTab & vbTab & "Balance"
List_Amortization.AddItem "" & vbTab & "Payment" & vbTab & vbTab & "Interest" & vbTab & vbTab & "Principal"
List_Amortization.AddItem "_______________________________________________________________________"
Do
n = n + 1
PI = P * I
PP = pmt - PI
P = P - PP
List_Amortization.AddItem n & vbTab & Round(pmt, 2) & vbTab & vbTab & Round(PI, 2) & vbTab & vbTab & Round(PP, 2) & vbTab & vbTab & Round(P, 2)
If n = Num Then
Exit Do
End If
Loop
The sample output screen is shown here:
For detail explanation of the code, please refer to the following link:
http://www.vbtutor.net/VB_Sample/amortize.htm
Sunday, January 25, 2009
VB2008 e-book Ready for Download !
http://www.vbtutor.net/vb2008/vb2008tutor.html
The title is 'Visual Basic 2008 Made Easy- A Concise Guide for Beginners'. It covers all the topics in the VB2008 tutorial. You can print out the book and use it as a guide to learn VB2008.
Be the first one to grab the book!
Friday, January 23, 2009
Visual Basic 2008 Tutorial Textbook
The other day I received an email from a VB friend who is asking for a printable version of VB2008 tutorial. He is finding it difficult to read the whole tutorial online. I am sure many of you are facing the same problem.
In order to help you learn VB2008 more efficiently and comfortably, I have upgraded the present server to a high end server with faster transfer rate. The web pages are loading must faster now. Please check it out for yourself at vbtutor.net
The next thing is I am writing a VB2008 textbook based on the VB2008 tutorial. I am adding extra stuff than the online version to make it more comprehensive. It is 80% completed now. Once it is completed, I will announce it in the blog and you will be able to download it from our website. By having a text book, I believe it will help you to learn much better.
Sunday, January 18, 2009
VB Story 2-My personal connection
That year our faculty got our first personal computer, it immediately became the sweetheart of everybody, and everyone can't wait to have a hand on it! And it could run BASIC! There was no hard disk, you need a boot diskette to start it, then you insert a floppy disk that contain the BASIC program in order to program BASIC. I can't remember what version of BASIC was it, could be GWBASIC, a dialect of BASIC developed by Microsoft from BASICA, originally for Compaq.
Although the PC was comparatively primitive, it could run powerful program especially in solving mathematical problems, even by today's standard. I still can remember a program that can sort numbers in ascending order, let me list down the subroutine here:
510 For J=1 to N-1
520 For I=1 to N-J
530 If A(I)<=A(I+1) Then 570
540 Let T=A(I)
550 Let A(I)=A(I+1)
560 Let A(I+1)=T
570 Next I
580 Next J
Can you figure out the logic? Maybe you could transform this into a VB6 program. Anyway, BASIC was the preferred computer language of the college students because it was so easy to learn, even though we have to do FORTRAN (Formula Translation) in our coursework. I picked up interest in programming from using BASIC but some how or rather left BASIC a few years later and ventured into database programming using DBASE II . I was asked to compile a database program to process students' academic results.
It was only after many years that I went back to BASIC again, that was the time Gates already made a fortune out of his software business. It was the late eighties. I bought a PC that run on MS_DOS, with 4MB RAM, 50MB Hard disk, and its processor was a 386 machines produced by Epson. I was a little surprise that a version of BASIC was included in DOS, and it was QBASIC. With the advent of QBASIC, DOS Based BASIC has reached the greatest height. It has done away with line numbers, and it could program colors as well as music, what a great improvement! There was also a more powerful version of QBASIC by the name of QuickBAsIC in the sense that it could compile the programs into EXE files.
And finally QBASIC also became obsolete with the arrival of the famous Windows, and Visual Basic, but the legacy of BASIC continues...........
Saturday, January 17, 2009
VB Story-My personal connection
How's programming lately? If you're getting slightly bored, perhaps you should read something not so technical about VB, for example, the history part.
Do you know that computer software have been around since world war II? However, before 1975, most of the software were free, and one of the earliest computer language was BASIC. Those were the days without any PC, you have to type your program on the punch cards and then feed them to the main frames and pick up the output the next, how tedious and slow was it!
I was one of the hobbyists of BASIC back then, Bill Gates was one too! Programming back then was simple , the program is sequential and you need to type in the line numbers . However, the program can become very messy if you need to program something more complex, the line number can run to a couple of thousands, which means you have to punch a few thousand cards! A BASIC program typically looks like this 10 Let X=100 20 Let Y =200 30 Z=X+Y 40 Print Z One good thing about programming those days was that we get a lot of freebies! However, since Gates and his partner Paul Allen started commercializing BASIC, we no longer enjoy such benefits. But this is a good development because we learn to respect intellectual properties, and this will encourage creativity and innovations. True enough, with the advent of personal computers and commercialization of software, we get faster computers and more efficient software! We no longer have to use the punch cards and we can get the output of a program instantly, how nice! That' enough for today, lets go programming!
Tuesday, January 13, 2009
Server Upgrade
Happy learning.
Sunday, January 11, 2009
Simple Slot Machine
View the following link for the simple slot machine.
SIMPLE SLOT MACHINE
Sunday, January 4, 2009
Adding Menus to Your VB Application
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. The & sign will be the shortcut to initiate the action under this item by pressing the Alt key and the letter shown after the & sign. The letter will be underlined. Below are the diagrams:
More details will be discussed in future posts.