Saturday, January 31, 2009

A question from the Reader-Sum of Numbers divisible by 4

A reader emailed me and asked for help. He wants to create a function to calculate the sum of numbers between 1 and 100 that are divisible by 4. Here is the solution:

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

Visual Basic is popular in the business sector because one can create business and accounting software easily with VB. For example, you can create an amortization table with the following code:

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

List_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 "_______________________________________________________________________"
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

End Sub

The sample output screen is shown here:

For detail explanation of the code, please refer to the following link:

Sunday, January 25, 2009

VB2008 e-book Ready for Download !

Good News! VB2008 e-book is now ready and you can download it from

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

Hi VB friends,

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

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

We are happy to inform you that we have just performed a server upgrade a couple of days ago. Now the server is running at a higher bandwidth, and all the pages should be loading at a faster rate now. So please take more time to browse through our website, who knows you might discover something that would help you greatly. For students who are taking VB as a subject, the website is a great resource for you; and for the programmers, you would also find some interesting programs here to help in your projects. Please also ask your friends to visit this website so that more people will benefit from the tutorial. And you can also encourage your friends to pick up VB programming.

Happy learning.

Sunday, January 11, 2009

Simple Slot Machine

Slot machine is a game of chance. It can be created quite easily in Visual Basic. I have created a simple slot machine in VB, though it is no where near the actual slot machine, it is a simulation o a slot machine. In this program, I inserted an array of shapes and change their appearance as well as color using the properties of the shape control. And I use the Rnd function to randomize the process. So when the user press the play button, different combination of shapes will appear, something like a slot machine. Off course you can add many features like amount of bets and amount the player wins, but I will leave that to you. Maybe somebody can come out with a sophisticated slot machine and share with us here.

View the following link for the simple slot machine.


Sunday, January 4, 2009

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. 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.