Visual Basic is an object-oriented programming language that uses the Microsoft Windows platform. The programs that are created using Visual Basic will look and act like standard Windows programs. Visual Basic provides one the tools to create windows with elements such as menus, text boxes, command buttons, option buttons, list boxes and scroll bars.
This tutorial does not completely cover all “basis” of Visual Basic. This is just basically an over view to learn a bit about VB. Databases, Crystal Reports and others are not included to keep this tutorial under 500 pages and to keep carpal tunnel from occurring.
This tutorial was written assuming that you POSSIBLY have SOME programming knowledge. Like what an IF / THEN / ELSE statement is. So, if you have taken Basic Or Qbasic, that’s ideal before reading this tutorial.
Also, it would not help much reading this tutorial if you do not have VB 6.0 on your computer. I think you can download it from Kazaa Lite (kinda big though) if you do not have it. But get it if you don’t have it, open it and read this.
Procedural vs. Non-Procedural Languages
Procedural Languages – Programming languages that have a set plan that you follow to execute a program. You have a series of statements that you execute where you start with the first statement. The statements are executed in order from beginning to end. The program terminates after the last statement is executed.
Examples of Procedural Languages:
- FORTRAN
- COBOL
- BASIC
- C
- PASCAL
- and more…
Non-Procedural Languages – Object-Oriented programming languages that are Event-driven. Here you don’t just have a series of statements that are executed, you have several choices of different things you can do in a program. You select the event that you want to occur. Only the code for that event is executed.
Examples of Procedural Languages:
- Visual Basic
- C++
- DELPHI
- JAVA
Labels, Text Boxes and Command Buttons
Label – A control that is used to display text as a caption. Labels cannot be altered by the used. They are simply used to display headings and results of processing as well as other information on a form. Labels begin with a .lbl extension.
Examples of labels:
- lblName
- lblAddress
- lblCity
Text Box – Control that is used to enter information onto a form. Text boxes can have focus and are the primary means of providing input to a project in Visual Basic. Text boxes begin with the prefix of .txt
Examples of Text Boxes:
- txtSponge
- txtBob
- txtNum1
Command Buttons – Control used to activate a procedure. When a command button is clicked on or activated using an access key, and event will take place (the code behind the command button is executed.) Command buttons begin with the prefix .cmd
Types of Errors
- Syntax Errors – Compiler errors that occur when your project code is converted to machine language. These are errors that occur when you violate the syntax rules of Visual Basic. VB will highlight these errors in RED.
- Run-Time Errors – Errors that occur when programming is actually executing. These types of errors will cause the program to terminate abnormally. Examples of run-time errors are division by zero, or trying to do calculations with non-numeric data. VB will display a dialog box, pull up the affected code on screen, and highlight where the error occurred in YELLOW.
- Logical Errors – Errors which allow your project to run, but will produce incorrect results. These errors are tough to find and debug because no error message will be displayed or highlighted. You will have to dig through the code to find the error(s). Examples would be : Adding 3 instead of subtracting 3, or displaying the wrong info in a label.
Special Functions and Methods
- VAL – A function that will convert a string to a numeric value. It begins with the left-most character of the string. If that character is a numeric digit, decimal point or a sign, VAL will convert the character to a numeric and move on to the next character. Once a non-numeric character is found, the VAL function will stop.
- PrintForm – A method that will print the current form on the printer. This is executed during run-time.
- FORMAT – This is used to format a variable. Variables can be formatted as fixed numbers, currency and percents.
Syntax :
<var> = FORMAT(<var>,"<type of format>")
Examples:
lblTotal = Format(lblTotal, "Currency")
- Formats number with a $ and two decimal places.
lblArea = Format(lblTotal, "Fixed")
- Formats number as decimal with two decimal places.
LblPctTotal = Format(lblPctTotal, "Percent")
- Formats number as percent with two decimal places and adds % to end of number.
FormatCurrency – This function will take a number and format it as currency with two decimal places and a $ sign. There is only one argument, the number you want to format.
lblSum = FormatCurrency(NumToFormat) lblTotalAmount = FormatCurrency(curTotal)
FormatNumber – This function will format a number as a decimal with a set number of decimal places. The first argument is the number you want to format. The second is the number of decimal places you want the number to have.
lblTotal = FormatNumber(NumToFormat,NumOfDecimalPlaces) lblSum = FormatNumber(Sum,2)
FormatPercent – This function will take the number and format it as a percent. The function will multiply by 100 and add a percent sign to the end of the number. The number by default will be rounded to zero decimal places unless you specify a 2nd argument.
lblTotalPct = FormatPercent(NumToFormatAsPercent) lblSumPct = FormatPercent(NumToFormatAsPercent, NumOfPlaces) lblCurrInterest = FormatPercent(CurrentInterest)
FormatDate Time – This function will take a string and format it as a Date, Time or both. The first argument is the date or time to be formatted. The 2nd argument is the format of date/time you wish to use.
LblCurrentDate = FormatDateTime(StartingDate, vbShortDate) LblCurrentDate = FormatDateTime(EndingDate, vbLongDate) LblCurrentTime = FormatDateTime(StartingTime, vbShortTime) LblCurrentTime = FormatDateTime(StartingTime, vbLongTime)