Académique Documents
Professionnel Documents
Culture Documents
6793
8: Scope of a variable
9: Operators & expressions
10: How to make an executable(.exe) file
11: Naming Conventions
12: Common properties
13: Label & TextBox
14: The CommandButton control
15: Input and Output operations
16: Data type conversion
17: If Blocks
18: Nested If-Else
19: Select Case blocks
20: Do Loops
21: For...Next Loops
22: OptionButton & Frame
23: The CheckBox Control
24: Graphical style of OptionButton & CheckBox
25: Image & PictureBox
26: Common events
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
Lesson
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
Functions
Lesson 49
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
There are many useful functions for the date-time operations in VB6.
Visual Basic gives you enough power for handling the date and time. Here
youll learn about those functions in brief.
This post will include the functions that are defined in the DateTime
module of the VBA library. Search DateTime in Object Browser.
Weekday
The weekday function returns the day of the week. It returns a number
representing the day.
Syntax: Variable = weekday(Date, [FirstDayOfWeek])
This function takes two arguments, one is the optional. You need to pass
a date string to this function, and the first day of week which is optional is
set to vbSunday by default.
The constant values of FirstDayOfWeek paramenter are as follows:
vbSunday
vbMonday
vbTuesday
vbWednesday
vbThursday
vbFriday
vbSaturday
vbUseSystemDayOfWeek
Example:
Print Weekday("1/4/2014")
Output: 7
Year
The year function returns the year from the date.
Example:
Print Year("1/4/2014")
Output: 2014
Month
The month function returns the month of the year.
Example:
Dim m As Integer
m = Month("27 / 3 / 2013")
MsgBox m
Output: 3
DateValue
The DateValue function returns the date part from a date/time value.
Example:
Dim dt As Date
dt = DateValue(Now)
Print dt
Output: 1/4/2014
TimeValue
Returns the time part from a date/time value.
Example:
Dim dt As Date
dt = TimeValue(Now)
Print dt
Output: 12:51:25 PM
Day
Returns the day part from a date
Example:
Dt = Day(Now)
Hour
Returns the hour of the day.
Example:
Print Hour(Now)
Minute
Returns the minute of the hour.
Example:
Print Minute(Now)
Second
Returns the second of the minute.
Example:
Print Second(Now)
DatePart
Returns a specific part from a date.
Syntax: DatePart(Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear])
The interval parameter is the interval part of a date you want.
Pass a date value through the Date parameter.
FirstDayOfWeek and FirstWeekOfYear are optional parameters, the values
of which are vbSunday and vbFirstJan1
Example:
Print "year = " & DatePart("yyyy", "4/1/2014")
Print "month = " & DatePart("m", Now)
Print "day = " & DatePart("d", Now)
Print "week = " & DatePart("ww", Now)
Print "hour = " & DatePart("h", Now)
Print "minute = " & DatePart("n", Now)
Print "second = " & DatePart("s", Now)
Print "weekday = " & DatePart("y", Now)
The interval values can be:
yyyy-Year,
m- Month,
d-Day,
ww-Week,
h-Hour,
n-Minute,
s-Second,
y-weekday.
DateSerial
TimeSerial
Returns a time for a specific hour, minute and second.
Example:
Print TimeSerial(13, 15, 55)
DateDiff
Returns the number of time intervals between two dates.
Example:
dt = DateDiff("d", "5 / 5 / 1990", "26 / 4 / 2013")
The first argument is the interval which can have the value among the
interval constants mentioned above.
Related topics:
Formatting functions
<<Table Of Contents>>
Next Lesson>>
Main Menu
Home
Blog
Blog Articles
6 Effective Ways to Learn Visual Basic
Programming with Visual Basic for a Living
Guide to Good Programming Habits
Comparison between VB6 and VB.NET
5 Reasons Why VB is a Good Choice for Making Business Solutions
Google+ Followers
Get weekly updates
Email Address
Subscribe
xml search
Array in Visual Basic 6
Lesson 50
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
An array is a collection of items of the same data type. All items have the same
name and they are identified by a subscript or index. When you need to work
with several similar data values, you can use array to eliminate the difficulties of
declaring so many variables. For example, if you want to compute the daily
sales and sum the sales amount after 30 days, you don't need to have 30
variables. Just simply declare an array of size 30 and get your work done !
Declaring an array
Syntax: Dim Variable_Name(index) As [Type]
Example:
Dim month(10) As Integer '11 elements
'or
Dim month(1 to 12) as Integer '12 elements
In the first line, month(10) is a collection of 11 integer values or items.
month(0) is the 1st item and month(10) is the 10th & last item of the array. So
0 and 10 are respectively the lower bound and upper bound of the array.
In the other line, month(1 to 12) is a collection of 12 integer values or elements
or items where month(1) is the 1st item and month(12) is the last. So 1 and 12
are respectively the lower bound and upper bound of the array.
Types of array
The array used in the example is a one-dimensional and fixed-size array. An
array can have more than one dimension. The other types of arrays are multidimensional arrays, Dynamic arrays and Control arrays.
Fixed-Size Array: We know the total number of items the array in the above
example holds. So that is a Fixed-Size array.
The LBound and Ubound functions return the lower bound and upper bound of
an array respectively.
Example:
Private Sub cmdDisplay_Click()
Dim arr(10) As Integer
a = LBound(arr)
b = UBound(arr)
MsgBox "Lower bound = " & a & " Upper bound = " & b
End Sub
Initializing an array
You can use For Loop to initialize an array.
Example:
Dim day(10) As Integer, i As Integer
For i = 0 To 10
day(i) = InputBox("Enter day value")
Next i
You can also initialize each array item separately in the way a variable is
initialized.
Example: This program inputs the Sale amount of each day and sums the total
amount of 5 days.
Private Sub cmdStart_Click()
Dim SaleDay(1 To 5)
'Sale in a particular day
Dim i As Integer, Sale As Long
Sale = 0
For i = 1 To 5
SaleDay(i) = InputBox("Enter Sale amount of Day " & i)
Sale = Sale + SaleDay(i)
Next i
MsgBox "Total Sale of 5 days = $" & Sale
End Sub
Related topics:
Multi-dimensional array
Dynamic array
Control array
Collection
Multi-Dimensional Arrays
Lesson
51
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
An array can be multi-dimensional that means, it can have more than one
dimension. A list of data is represented in a one-dimensional array where a
multi-dimensional array represents a table of data. An array can be two
dimensional, three dimensional and so on. We generally don't need an array of
more than two dimensions, it is enough to use one and two dimensional arrays.
You can use a higher dimensional array when the program is too complex. A two
dimensional array takes the row-column form.
Declaration:
Dim value(5, 5) As Integer
'two dimensional
'Or,
Dim value(1 to 5, 1 to 5) As Double
Dim number(6, 9, 8) As Integer 'three dimensinoal
Initialization:
To initialize the array elements, you first need to recognize the array elements.
For example, the array 'value(2, 2)' has 9 elements. They are value(0,0),
value(0,1), value(0,2), value(1,0), value(1,1), value(1,2), value(2,0),
value(2,1), value(2,2). For the initialization, you may wish to use For Loop or
initialize each element like variables. Using For Loop is a better choice as it
reduces the number of lines of code. But sometimes, separately initializing each
element like a variable is much more convenient. And it also depends on the
type of program you are writing .
Next i
End Sub
Sample program:
Matrix Addition
Related topics:
Array
Control array
Dynamic array
Collection
Dynamic Array
Lesson
52
<<Previous Lesson <<Table Of Contents>> Next Lesson>>
In case of a fixed size array, we have seen that the size of the array is fixed or
unchanged, but there may be some situations where you may want to change
the array size. A dynamic array can be resized at run time whenever you want.
Note: Unlike the Dim and Static statements, the ReDim statements are
executable. So a ReDim statement can only be in a procedure and when you
execute the ReDim statement, all the values stored in the array are lost. You can
use the ReDim statement repeatedly to change the array size.
Related topics:
Array
Multi-dimensional array
Control array
Collection - part 1
Collection - part 2
Dynamic Array
Lesson
52
<<Previous Lesson <<Table Of Contents>> Next Lesson>>
In case of a fixed size array, we have seen that the size of the array is fixed or
unchanged, but there may be some situations where you may want to change
the array size. A dynamic array can be resized at run time whenever you want.
The ReDim statement deletes all the values stored in the array. You can preserve
the element values using the Preserve keyword. So using Preserve keyword with
ReDimstatements enables you to change the array size without losing the data in
the array.
Example:
Dim arr() As Integer
ReDim arr(2) As Integer
For i = 0 To 2
arr(i) = InputBox("Enter the value")
Next i
ReDim Preserve arr(3) As Integer
arr(3) = 9
Print arr(0), arr(1), arr(2), arr(3)
Output: If the input values through InputBox are 5,6,7 then the following will be
printed on the form.
5
6
7
9
Related topics:
Array
Multi-dimensional array
Control array
Collection - part 1
Collection - part 2
Control Array
Lesson
53
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Till now we have discussed array of variables. Similarly you can also have an
array of controls by grouping a set of controls together. The controls must be of
the same type like all TextBoxes or all CommandButtons.
Or, after placing a control on the form, copy that and paste on the form. It will
create control array for you.
2. Set the Index property of each control or you may not change as it is
automatically set.
3. Now its done. You are ready to use the control array in your code.
Command1(i).BackColor = vbBlue
Next i
End Sub
You can also pass a value to the Index parameter from other procedures.
Control array is useful when you want to clear a set of TextBox fields. Create a
control array of 5 TextBox controls and write the following code in the Click
event procedure of a CommandButton control.
Example:
Private Sub cmdClearAllFields_Click()
Dim i As Integer
For i = 0 To 4 'Or, For i=Text1.LBound To Text1.UBound
Text1(i).Text = ""
Next i
End Sub
Output:
Example:
Related topics:
Array
Multi-dimensional array
Dynamic array
Collection - part 1
Collection - part2
User-defined type
Lesson 54
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Collections are objects in Visual Basic that are used to store a group of data
values. Collections are similar to array. But there are some special features that
differentiate the collections from arrays.
We have seen that array elements must be of the same data type. But the
Collection members can be of any data type and you don't need to set the size
of the Collection object. You can easily add items to the collection and it will
grow accordingly.
Creating a Collection
To use a Collection in your code, you first need to declare and create it.
Example:
Dim names As Collection
'Declaring the Collection
Set names = New Collection 'Creating the Collection
Or, replace the above code with this one line code
Dim names As New Collection
You can add one item at a time using the Add method.
Example:
Dim names As New Collection
names.Add "john"
names.Add "david"
You may use a string key associated with the item. The string key is used to
refer to a Collection item.
Dim names As New Collection
names.Add "John", "one"
Related topics:
Array
Multi-dimensional array
Dynamic array
Control array
User-defined type
55
<<Previous Lesson <<Table Of Contents>> Next Lesson>>
You can remove a particular item from the Collection using the Remove method.
Example:
Private Sub Command1_Click()
Dim country As New Collection
country.Add "USA"
country.Add "UK"
country.Add "Japan", "j"
country.Remove (1)
country.Remove ("j")
End Sub
Related topics:
Array
Multi-dimensional array
Dynamic array
Control array
User-defined type
<<Previous Lesson
Using the data types [Part 1]
Lesson
<<Table Of Contents>>
Next Le
56
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Visual Basic 6 is rich in data type. So you should know the usage of all the data
types to make a powerful application.
Integer values
Among the data types for storing integer values, Integer is the most used data
type for its efficiency. But sometimes you may use the Long data type instead of
Integer especially when you want to reduce the overflow error as it can hold a
very large value, from -2,147,483,648 to 2,147,483,647 where an Integer can
only store a value from -32,768 to 32767.
For small integer values, use the Byte data type, the value range is from 0 to
255 only in this case.
The Boolean variables stores only 0 and 1 that stand for False and True
respectively. To use a Boolean variable in your code, firstly declare the variable
as Boolean and then assign a True/False value.
Example:
Private Sub cmdCheck_Click()
Dim TextOff As Boolean 'Declaring the variable
'Assigning value
If Text1.Visible = False Then
TextOff = True
End If
'Using the Boolean variable
If TextOff = True Then
MsgBox "TextBox is hidden"
Else
MsgBox "TextBox is shown"
End If
End Sub
Output:
Related topics:
57
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
The fixed-length string in the above example takes 10*2=20 bytes memory
space.
Example:
Private Sub cmdShow_Click()
Var = Now
Var = Var + 14 '14 days from today
Print Var
End Sub
After assigning, you can use the object variable to access the properties and
methods of the original object.
Example:
'Declaration
Dim frm As Form
Example:
Private Sub cmdClear_Click()
Dim field As TextBox 'Declaring as Object
For Each field In Text1 'Text1 is a control array
field.Text = ""
Next
End Sub
This is just a brief discussion on Object data type. You'll learn more later in the
tutorial.
Related topics:
Lesson 58
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
User defined type in Visual Basic 6 is a compound data structure. It holds several
variables of different data types. After defining a UDT, you can assign values to
the member variables of the UDT.
Assigning values
Private Sub cmdSetValue_Click()
Dim book1 As BookDetails 'Declaring book1 as the type
'BookDetails
'Assigning values
book1.author = "Balagurusamy"
book1.title = "C Programming"
book1.pages = 600
End Sub
ph_no As String
End Type
Private Type StudentDetails
name As String
stream As String
DepartmentId As String
address As AddressDetails
End Type
__________________________________________________________
Private Sub cmdSetDetails_Click()
Dim student1 As StudentDetails
'Nested With...End With structure
With student1
.name = "XYZ"
.stream = "Computer Science"
.DepartmentId = "C900"
MsgBox .name & vbTab & .stream & vbTab & .DepartmentId
With .address
.city = "London"
.state = "abc gdf"
.pin = 77764
.ph_no = "998765432"
MsgBox .city & vbTab & .ph_no & vbTab & .pin & vbTab _
& .state
End With
End With
End Sub
Related topics:
Array
Multi-dimensional array
Control array
Dynamic array
Collection - part 1
Collection - part 2
Lesson 59
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Every procedure has a unique name, a scope and a list of arguments. The
function procedure, in addition, has a return value. Sub procedure and property
procedure does not return value.
Scope of a procedure
A procedure can be Private, Public or Friend. You can invoke a Private procedure
only from within the module. A Public procedure can be called from anywhere of
the current project -- from within the module, from other modules of your
application project, and in some cases from outside the program using COM. And
a friend procedure can be called from anywhere of the current project but not
from outside.
MsgBox "hello"
End Sub
'In Form1
Private Sub Command1_Click()
Call Form2.Command1_Click
End Sub
'In form2
Public Sub Command1_Click() 'Scope is Public
MsgBox "You have called a procedure of form2"
End Sub
When you'll click the CommandButton on Form1, Command1_Click procedure of
the form2 module will be called. As the scope of the procedure in Form2 is
Public, the procedure is accessible from anywhere of the current project, and if
the procedure is Private, the procedure cannot be invoked from other modules.
In the Form2 module, if you change the procedure scope to Private, it can then
only be called from the Form2 module. This aspect will be clearer when you'll
learn about working with multiple forms in the next lessons.
Related topics:
Function procedures
Scope of a variable
Lesson 60
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Example:
Private Sub Command1_Click()
Call xprint
'Function calling
End Sub
___________________________________________________________
Private Sub xprint() 'Function Definition
Print "Hello World"
Print "";
Print "*****"
Print "New"
End Sub
___________________________________________________________
Private Sub Command2_Click()
Call xprint
'Function calling
End Sub
'In form2
Public Sub xprint()
Form2.Show
Print "Hello World !"
Print "";
Print "***"
Print "New"
End Sub
_____________________________________________________________
Private Sub Command1_Click()
Call xprint
End Sub
The concept of Public general sub procedure will be clearer to you when you'll
learn about working with multiple forms in the next chapters.
Related topics:
Scope of a variable
Function procedures
Lesson 61
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Output:
End Function
____________________________________________________________
Private Sub Form_Load()
Text1.Text = ""
'Function calling and assigning the returned value
Text1.Text = sum(60, 40)
End Sub
On the other hand, when the arguments are passed by value, the actual values
are passed. So the called procedure cannot change their original values in any
way.
Example:
'Calling procedure
Private Sub Command1_Click()
Dim a As Integer 'The value of a is 0 after declaration
Call num(a)
Print a 'The value of a is 0, its unchanged
End Sub
________________________________________________________________
___
'Called procedure
Public Function num(ByVal x As Integer)
x=x+1
End Function
Related topics:
Scope of a variable
Lesson 62
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Menus are one of the most important features of a software product. Every
standard software must have menus. You generally see a menu on top of a
software interface. Menus are controls but different from the controls in the
ToolBox and they don't work like the other controls. You can drop a menu on the
form from the Menu Editor Window. Press Ctrl+E to show the Menu Editor
Window or right-click on the form and click Menu Editor. The Menu Editor
Window can also be shown from the Menu Editor icon of the ToolBar.
Building a menu
Building a menu is very simple, you can do it on your own. Simply fill the
Caption and Name field in the Menu Editor Window and click ok to create it.
Click the right-arrow button to create a submenu. Click Next to create the next
menu item, and click ok once you're done editing the menu items.
Now run the program and Click on the menu items and see what happens.
End If
End Sub
Output:
Related topics:
Popup menu
CheckBox control
<<Previous Lesson
Visual Basic 6 Popup Menu
<<Table Of Contents>>
Next Lesson>
Lesson 63
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Most of the commercial software products have popup menus as they can make
the application more user-friendly and powerful. When you right click on
windows desktop, a popup menu appears. Visual Basic 6.0 provides a Popup
menu method that you can use in your program to show the popup menu on the
form's surface.
To use the Popup menu method, you first need to create a menu. For example,
create a menu with the name "View". See the example given below.
If you need only the popup menu but not the menu bar, set the Visible property
of the menu control to False in design time.
Sample: Colors PopUp
Related topic:
64
<<Previous Lesson
In Visual Basic 6, a windows application may have more than one window. Most
of the windows applications contain several windows. You can make a multiple
windows software by adding some forms to your project as because forms are
nothing but the windows of the final product.
You may change the Name property of the added form for your advantage.
Remember, the forms are considered as objects.
Showing a form
You can show the newly added form using the Show method.
Syntax:
Form.Show [Modal], [Owner form]
[Modal] means whether the form is modal or not. A modal form forces you to
stay on the form until you close it or enter some data. You may have noticed this
kind of form in several windows programs.
[Owner form] constant determines which form to be set as the owner form.
Example:
Form2.Show
or, Form2.Show vbModal
or, Form2.Show vbModeless
or, Form2.Show vbModal, Form1
Example:
'in Form1
Private Sub cmdShowForm2_Click()
Form2.Show vbModal
End Sub
'in Form1
Private Sub cmdShowForm2_Click()
Form2.Show
Unload Me 'unloads the current form
End Sub
Unloading a form
'in Form1
Private Sub cmdUnloadForm2_Click()
Unload Form2
End Sub
You can also use the Hide method.
Example:
Form2.Hide
When you unload a form, it releases the memory which was allocated for the
form. But the Hide method only hides the form, it doesn't unload it from the
memory.
Example:
'in Form1
Private Sub Command1_Click()
Form2.BackColor = vbBlue
End Sub
You can even set the controls' properties which are on another form.
Example:
'in Form1
Form2.Text1.Text = "Welcome!"
In the next lesson, you'll learn about the splash screen which is a special form
template.
Related topics:
MDI forms
Splash screen
<<Previous Lesson
57
Google +
49
Lesson 65
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
You can use splash screen in your windows app to make the program more
attractive. The splash screen is a form template which you should set to appear
before the main window or the actual program appears. This screen only stays
for some second showing the company name, logo, product name, product
version, copyright information and some other information.
Example:
On executing the code, the splash screen stays for 3 seconds and then the Main
Window is shown after the splash form is unloaded.
Related topics:
MDI forms
<<Previous Lesson
MDI Forms in Visual Basic 6
<<Tab
Lesson 66
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Today I'll share my knowledge of MDI forms with you. In the previous lessons
you've learned about forms -their properties, events and methods. The concept
of form is very vast, so many things have been discussed, and many things are
yet to be discussed.
Project -> Add MDI form. Click Project from the menu bar and click Add MDI
form. Its simple! Remember, a project can have only one MDI form.
MDI child form: To add a child form, you have to add a regular form,
and set the MDIchild property to True. You can have many child forms.
You can show an MDI child form using the Show method as same as the
regular forms.
Now coming to the point, how the MDI form works. There is generally a menu
bar in the parent form. From there the user opens or creates a new document.
In this way, the user accomplishes his/her work in one or multipledocuments,
then saves and closes the document(form). You create instances of a single form
in the code using the Set keyword ( Remember the object variables).
'Inside the MDIForm module
Private Sub mnuFileNew_Click()
Dim frm As New Form1
frm.Show
End Sub
active form. For example, you can close the active form using this
property from the Close menu command of the menu bar.
'In the MDI form
Private Sub mnuFileClose_Click()
If Not (ActiveForm Is Nothing) Then Unload ActiveForm
End Sub
If you cannot understand the above piece of code, don't worry. You'll get it when
you'll learn about Classes and Objects later in this tutorial. For now, just note
that (ActiveForm Is Nothing) represents that there is no active form. The Not
keyword before it negates the value.
Sample program
I've written an MDI demo application to simplify the topic for you.
Download it now! MDI demo
Related topics:
Multiple forms
BAS module
<<Previous Lesson
126
<<Table Of Contents>>
Next Lesson>>
Lesson 67
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Now after the previous lesson (MDI forms), lets move on to the next topic 'the
Screen Object'. After coming to the 67th lesson, I think its not necessary to tell
you about what an object is. Although I'll give you some hints.
Object
You have come across many controls throughout this tutorial. All the controls i.e
CommandButton, Label, TextBox are objects. Besides the form is also an object.
Collection is an object. See the lessons of collection object to clear any doubt.
Collection Object - part 1
Collection Object - Part 2
Some more examples are Clipboard, Dictionary object, Drive and folder objects,
App object, and printer object. There are many. The Screen object is also an
object like them.
Screen.MousePointer = vbCross
FontCount and Font properties: The FontCount property gives you the
number of fonts for the current display device, and the Fonts property
returns the font names.
The ActiveForm property: This property returns the form that is the active
window.
The ActiveControl property: Returns the control that has the focus.
Text1.SetFocus
Screen.ActiveForm.Caption = "New Form"
Screen.ActiveControl.Text = "New text"
The ActiveForm and ActiveControl properties also only applies to the current
application.
Note: All the properties except MouseIcon and MousePointer are read-only
properties here.
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
<<Table Of Contents>>
Next Lesson>>
1409
Lesson 68
<<Previous Lesson
Visual Basic 6 stores its code in the form of module that makes your program
more structured. So its very important to learn about it.
Scope of a variable
Procedures - Part 1
Procedures - Part 2
Function procedure
Multiple Forms
What is a module?
Module is a separate code file in Visual Basic that contains procedures and
declarations.
Your entire VB6 program code does not come line by line. Instead they are
separated in code files called modules, each of them is separated into
procedures. Thus your VB6 program becomes well structured which is the key
part of developingVisual Basic applications.
Form Module
The complete code including procedures and declarations part of a form is
referred to as the form module.
A separate project file with the .bas extension is saved on your hard disk as soon
as you add a standard module to your current project. You can change the Name
property of your newly added standard module from the properties window.
Choose a meaningful name, it will benefit you while writing the code.
The modules are shown in project explorer window:
The variables and constants declared using the Public keyword in the Declrations
section of the module are global, accessible from all parts of your current
application.
An easy example:
'Inside the BAS Module
'Scope is Public to make it accessible from anywhere of the application
Public Sub show()
MsgBox "Welcome to vbtutes"
MsgBox "This is a message"
MsgBox "New message!"
MsgBox "List of messages"
End Sub
__________________________________________________
Public Function increment(number As Integer) As Integer
increment = number + 1
End Function
'In form1
Private Sub cmdShow_Click()
Dim num As Integer
Dim m As Integer
num = InputBox("Enter the number", "Input")
m = Module1.increment(num)
MsgBox m
Call Module1.show
End Sub
' In Form2
Private Sub Form_Load()
Call Module1.show
End Sub
' In Form3
'The show sub procedure is global
Private Sub Form_Load()
Call Module1.show
End Sub
Related topics:
Multiple forms
MDI forms
Lesson 69
<<Previous Lesson
<<Table Of Contents>>
Next Lesson>>
Today I'll show you the techniques of validation in VB6. Validation is a very
useful feature of VB6 which you implement not only in the registration and login
forms but also in a variety of situations. Many applications have login systems.
So while designing the login and registration forms, it is a must to include the
validations for the controls on the forms.
What is validation?
You must have noticed in many applications that when you enter a data
incorrectly or when you leave the field blank, it warns you with a message. This
is called validation.
More precisely, when you leave the text field and move to another control, you
get a warning message and the input focus goes back to the previous field.
Examples:
I have already shown you an example of this type in the Registration program
sample, but VB6 validation techniques were not used in that sample.
You may say that you can do it without using the validation code, but VB6
provides a better way, a more convenient way to achieve the same which is
obviously the better solution to the problem.
The Validation event and the CausesValidation property makes the solution
working together. And sometimes the ValidateControls method is required. That's
all.
The default value of the CausesValidation property is True, and most of the
controls, even the external controls, support this property.
You have to write the code in the Validate event of the control where you input
the data, may be the TextBox control. After that you move to another control,
may be a CommandButton, a TextBox or any other. That means the first control
where you entered the data lost the input focus, and the other control is about
to recieve the focus. If the CausesValidation property of the control, which is
about to recieve the focus, is True only then Visual Basic fires the Validate event
of the first control that lost the focus.
Example:
Take two TextBox controls, a submit button; set their CausesValidation property
to True. Take a Cancel button, set its CausesValidation property to False.
Set the TabIndex value of the txtName control to 0.
End If
End Sub
In this case, the input focus moves back to the txtPhone control when the the
Cancel parameter is set to True.
Cancel = TrueEnd If
This method returns an Error 380 in the case when the cancel paramter was set
to True in the Validate event procedure of the control having the input focus.
Related topics:
Form events
TextBox control
<<Previous Lesson
2402
Google +
<<Table Of Contents>>
Next Lesson>>
218
Lesson 70
<--Previous Lesson <Table of Contents> Next Lesson-->
In this lesson, I'll talk about error handling in Visual Basic 6. I will discuss, in
brief, how you can handle errors in your Visual Basic program. I'm not going to
give you an in-depth concept of error handling. Because that is beyond the
scope of this text which is targeted to the beginner learners. But I will discuss a
few features of Visual Basic 6 that enable you to efficiently manage the errors in
your program.
Related topic:
On Error Goto label: If any error occurs, the control jumps to a label.
On Error Goto 0: This statement cancels the effect of 'On Error Resume
Next' and 'On Error Goto label' statements.
On Error Goto 0
On Error Goto 0 statement tells Visual Basic to cancel any effect of 'On Error
Resume Next' and 'On Error Goto label' statements. So this statement cancels
the effect of errorhandling in your program.
Related topic:
Lesson 71
<--Previous Lesson <Table of Contents> Next Lesson-->
The clipboard object is a simple object in Visual Basic 6 that stores data. You can
retrieve the data from the clipboard using appropriate methods of the clipboard
object.
In the Windows operating system, when you copy some data, the data is
automatically copied or stored in the clipboard object. You can place data in the
clipboard object and retrieve the data from clipboard.
You can easily copy and paste data among applications - from one application to
another using the clipboard object.
The clipboard object has no properties. It has a few methods that help you work
upon the data.
Download this sample: Copy-paste sample
The SetTex method puts text in the clipboard. In other words, you can place text
in the clipboard object using this method.
Example:
Clipboard.SetText Text1.Text
The GetData method retrieves data, for example an image, from the clipboard
object.
Example:
Picture2.Picture = Clipboard.GetData
Tutorials:
More >>>>>>>
Code samples:
Calculator
Photo Viewer
Contacts Manager
More >>>>>>>>>