Visual Basic 6.0 Mr. Jose Marie M. Pacia
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Object Oriented Programming • uses "objects" and their interactions to design applications and computer programs • Programming techniques may include features such as encapsulation, modularity, polymorphism, and inheritance VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Encapsulation • principle of information hiding • the hiding of design decisions in a computer program that are most likely to change
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Modularity • software design technique which software is composed from separate parts, called modules
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Polymorphism • is the ability of objects belonging to different types to respond to method calls of the same name, each one according to an appropriate type-specific behavior. • controlling code can issue the same command to a series of objects and get appropriately different results from each one. VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Inheritance • is a way to form new classes (instances of which are called objects) using classes that have already been defined. • The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes).
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Getting Started with VB6 • Menu bar – a special kind of control that lets the users select options and issue commands • Tool Bar – toolbar is a panel on which onscreen buttons, icons, menus or other input or output elements are placed
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
• Toolbox - A collection of tools that act as storage area of controls you can place on your form. • Form window – a window you customize to create the user interface of your program
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
• Project window – lists its components in a tree structured listing. Related objects appear together. • Properties window – a list of property of every control • Project – a collection of file that make up your application
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Getting Started 1. Open visual basic (Start > All programs > Microsoft Visual Studio > Microsoft Visual Basic 6.0 2. On the new project dialog box select standard exe to create a new program then click open VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Saving a Project 1. On the menu bar, click File > Save project. 2. Save every form and modules on your project in the same directory.
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Events • An activity that occurs during a program’s execution, such as a mouse click or a keystroke
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Most Use Events • Activate – executes when form is activated or in focus • Click – executes when a certain object is click by a mouse • Load – executes when a certain object is loaded or run for the first time VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
• Unload - executes when a certain object is un loaded or terminated • Mouse Move – Executes when a mouse is move
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Form • Correspond with other controls • The main window of the program when run • Form Properties – – – –
Border Style Window State Caption Picture
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Changing properties during runtime Form1.BackColor = &H80FFFF Form1.Caption = “this change during runtime”
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Objects on Visual Basic • Label Box – control use to display text • Label Properties are: Alignment, Appearance, AutoSize, Fore Color, Caption
– Properties can be change on the properties window
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code Label1.caption = “” ‘clears the text of a label Label1.caption = “This is a label” Label1.font = tahoma Label1.fontsize = 12 VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Command Button • is a widget that provides the user a simple way to trigger an event • Executes when triggered or clicked – – – –
Appearance Picture Style Caption
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Event • Click • DblClick
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code • Command1.caption = “Save” • Command1.visible = false
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Text Box • Used to display information entered at design time or runtime. This is used to ask a user an input – – – – – – – –
Appearance BorderStyle Font Locked Maxlength Multiline ScrollBar Text
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code • Text1.text = “” • Text1.locked = true • Text1.visible = true
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Visual Basic Operator • • • • • • •
^ - Exponent * - Multiplication / - Division + - Addition - - Subtraction Mod – Integer & - Concatenation
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code • Text1.text = val(text1.text) + val(text2.text)
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Lab Exercise 1 • Create a simple payroll system which computes the basic salary of an employee – Example1
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Option Button • An object which allows the user to choose options or choices
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Properties of Option Box • Caption – Text displayed on option Button • Value – Can be True or False
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Combo Box • An object which allows the user to choose options or choices but in a drop down list
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Properties of a Combo Box • List – use to insert all the choices on your combo box • Text – the selected value or text • Style – type of a combo box (0Dropdown Combo, 1-Simple Combo, 2-Dropdown Combo) VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Methods • Actions can be triggered on an object • Example are: additem, clear ect.
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Adding Items on Combo on runtime Combo1.AddItem "Choice 1" Combo1.AddItem "Choice 2“ Clearing the items Combo1.clear VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Decision Making • If/then – Allows a program to make a decision based on the truth or falsity of some expression – It is a condition VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
If then / else • Allows a user to make a decision but has two default decision
If then/ elseif /else • For two or more choices VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Logical Operators • • • • • • • •
> - Greater Than < - Less Than >= - Greater Than Equal <= - Less Than Equal <> - Not equal = - Equal And – Two Conditions that should be true Or – Two conditions that at least one condition can be true
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code If Combo1.Text = "A" Then MsgBox "A" Else MsgBox "NOT A" End If VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Lab Exercise 2 • Click Here
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Date and Time • Date Function – Format(Date,”MMM DD, YYYY”)
• Time Function – Format(Time,”HH:MM:SS am/pm”)
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
ImageBox • Insert image on your form • LoadPicture(“”) is use to load a picture on runtime
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Properties • Stretch
– True – Picture will adjust according to the size of your picture box – False – Picture will load on its normal size
• Picture
– Specify the picture or target picture
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code • Image1.Picture = LoadPicture("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg")
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Timer • Label1.Caption = Format(Time, "HH:MM:SS")
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
MsgBox • Shows a pop up dialog box and informs the user • MsgBox "Prompt", button, "TitleBar"
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Sample Button • Vbinformation – Ok and Information Icon • VbYesNo – Yes and No Button • VbCritical – Critical Icon and Ok Button • Etc…
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Declaring a variable Dim – Dimension/use to declare a variable Dim lagyu As String
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Data Types • • • • • •
Boolean – true or false Char – One Unicode character Byte – 0 to 255 Date – Dates Decimal – 1.0E-28 to 7.9E+28 Short - -32,768 to 32,767
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
• Integer - -2,147,483,648 to 2,147,483,647 • Long - -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 • Single - +1.5E-45 to +3.4E+38 • Double - +5.0E-45 to +1.7E+308 • Object – Data of any type • String – 0 to 2000000000 Unicode characters
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Lab Exercise 3 • Exercise 3
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Loopings • Repetition of codes in a predefined counter or sequence
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
For Loop For variable = starting point to ending point Next variable
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Example Dim ctr As Integer ctr = 0 For i = 1 To 10 ctr = ctr + 1 Next i VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
While/ Wend • A repetition structure allows the programmer to specify that an action should be repeated depending on the value of the condition
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Syntax While condition Wend
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code Dim ctr As Integer i=0 While i < 10 ctr = ctr + 1 i=i+1 Wend VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Do While Loop Do while Condition Loop
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Codes Dim ctr As Integer i=0 Do While i < 10 ctr = ctr + 1 i=i+1 Loop VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Array • Can store information and has indexes or memory address • Array have the same name and same type • The first element of an array is 0th Element • The position on the array is called index
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Syntax Dim array_name(size) as data type Store value on array Array_name(index) = value VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Example Dim arraysamp(3) As String arraysamp(0) = "a" arraysamp(1) = "b" arraysamp(2) = "c" VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
List Box • ListBox – An area in which a list of items is displayed. The user can make a selection from the list by clicking on any item. Multiple elements can be selected.
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
List Box Properties • List – Use to insert choices • Style – 1- Checkbox / 0 - Standard • Sorted – Sorts the value in ascending order (True/False)
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Methods • .additem = add a new choice on runtime – List1.AddItem "Item to add“
• .clear = removes all the items on the listbox – List1.Clear VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Methods • .RemoveItem – Removes a specific item – List1.RemoveItem (index)
• .ListCount – Number of items on the list – listBought.ListCount VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Laboratory Exercise 4 • CLICK ME
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Being Familiar with Ascii Codes • 128-character set of alphanumeric symbols • Number symbols which represent numeric codes on the computer
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Two types of key events • Keypress – fires when a key representing an ASCII character is pressed
– Does not determined the SHIFT, ALT and Control Key was pressed
• It is necessary to handle the second type of key events, use the Key up and the Key Down Events
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
• KeyDown – Generated when key is initially pressed • KeyUp – Generated when key is released • KeyPress – Generated when key is pressed. Occurs repeatedly while key is held down
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
KeyCode of Arrow key • Up Arrow – 38 • Down Arrow – 40 • Left Arrow – 37 • Right Arrow – 39 • Letter S - 83 VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Function • Defined procedures to meet the requirements needed • Can be called to do a specific task
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to create functions Public Sub functionname() End Sub
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code Public Sub msgboxhello() msgbox “HELLO!!!” End Sub
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Timer • Are non visible components that reside on a form, generating tick events at a set interval. • Interval are set in the timers interval • 1000 interval is equal to 1 Second
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Timer Properties and Events • Interval – number of seconds when a code is triggered • Timer – The only event of a timer
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Position of an Object in a Form • Left – X Axis • Top – Y Axis • Leftmost side value = 0 • Rightmost side value = 0 VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Laboratory Exercise 5 • Click Me
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Adding Extra Components 1. On the Menu bar click on Project > Components (CTRL + T) 2. On the controls tab check the checkbox of the object that you want to add on your program 3. Click on Apply then Close 4. Example (Microsoft Common Dialog Control 6.0)
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Common Dialog Box • Allows you to call the open or save dialog box.
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Common Dialog Properties • Filter – Show what type of file should be open • MaxFileSize – Show how big the maximum size of the file
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Methods • ShowOpen – Show the Open Dialog Box • ShowSave – Show the Save Dialog Box
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to get the path • Cd.filename – save the whole path of the file • Cd.filetitle – save the file title of the file
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How t o add extra references • On the menu bar click project > references • Check the checkbox what command or references to add • Click OK VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Microsoft Scripting Runtime • Use to manipulate files and Directories
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
File.Copyfile Dim file As New Scripting.FileSystemObject
File.copyfile source, destination,true/false
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to create randomize number Randomize num = Int(Rnd * ctrRand) Where ctrRand = Range of random numbers
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to declare global variable • Public variable as DataType
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to add module • On the menu bar click Project > Add Module • Click Open
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to add Extra Form • On the menu bar click Project > Add Form • Click Form then Open
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
To open a form • Formname.show – Modal 1 = Disable the extra form – Modal 0 = Enable the extra form
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Unload or close a form • Unload Formname • Unload Me
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Laboratory Exercise 6 • Click Me
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Database Programming • References – used to load extra libraries
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to add Active Data Objects 2.5 • On the menu bar click project > References • On the References dialog box find Microsoft ActiveX Data Objects 2.5 and check the check box • Click Ok VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
How to Add modules • On the menu bar click Project > Add Modules • Click Open
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Declaring the ADODB.Connection • Public db As New ADODB.Connection
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Setting the ADODB Connection Public Sub dbConn() 'ACCESS CONNECTION STRING Set db = New ADODB.Connection db.CursorLocation = adUseClient db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\dbInformation.mdb;" & _ "Persist Security Info=False;" End Sub VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Declaring the ADODB.Recordset • Public rs As New ADODB.Recordset
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Setting the ADODB.Recordset Public Sub rsConn(table As String) Set rs = New ADODB.Recordset rs.CursorLocation = adUseClient rs.Open table, db, adOpenDynamic, adLockOptimistic End Sub
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Code for Add Records rs.AddNew rs(“Field Name") = source rs.Update
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Delete rs.Delete rs.Update
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Edit Records rs(“Field Name") = source rs.Update
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Search Record • rsConn “select * from Table where Field=‘” & txtBox & “’”
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”
Checking if there is no record • Rs.recordcount -check the number of records • Rs.eof – end of file • Rs.bof – Beginning of file
VISUAL BASIC 6.0©
COPYRIGHT BY BADBOY INC
PRESENTED BY: SIR “JPACS”