Sie sind auf Seite 1von 25

B.Sc.

IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Section 01
Q1. What is the significance of the procedure and the
function in Visual Basic?
Ans: - Firstly we should know what is a function and a procedure.
Procedure and function are sub program’s that carry out a well
defined task. We can simplify program. Tasks by breaking the
prog. Into smaller logical components. This component is called
procedure and thus serves as the building blocks that let the
programmer to enhance and extend Visual Basic.
A procedure can receive one or more arguments and perform a
calculation. The sequence of statements can be placed with the
pair of sub and end sub statement and can be evoked by a name.

The major benefits or we can say the main significance of


using a procedure are:

 Procedure allows us to break the complex prog. Into a smaller


units i. logical units and each of these can be debugged very
easily than the entire prog. Without procedure.
 A procedure once created can be used several times in a prog.
And it can also be used or it acts as a building block for other
prog. Usually with little or no modification.
 Thus pre created procedure saves time and makes a prog. Short
and less complicated.
 A procedure can be embedded in any prog. Without any problem
very easily. A procedure is called by using call statement
Syntax:- call <procedure name> (parameter)

In visual Basic there are two types of sub procedures:-

1. General Procedure:- A general procedure tells the application how


to perform a specific task. Once a general procedure is defined it
must be specifically evoked by the application.
2. Event procedure:- when an object in VB recognizes that an event
has occurred, it automatically invokes the event procedure with
the name corresponding to the event

FUNCTION:-

A complex problem may be decomposed into a small or easily


manageable parts or modules called function. It is similar to the
procedure except that it always allows return value to the calling
environment. The main difference between both of them is that a
procedure can receive arguments, perform a task but doesn’t
return anything to the calling environment but a function does it.
A function returns a value so it must have a data type.
Syntax:-

-1- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================

Function<function name> (arguments)


As<data type>
<Statements>
end function

The function keyword is used to specify VB functions, VB


functions typically contains a series of VB statements that are
grouped together to perform a specific task VB provides some
system provided or intrinsic functions like SQL, cos, chr

In addition we can use the function statement to write our own


function.

The main significance of using a function are:-

1. A complex prog. Can be decomposed into a small or easily


manageable parts or modules called functions.
2. Functions are very easy to read, write, debug and modify
complex prog.
3. They can be easily incorporated in the main prog.
4. It is easy to maintain and modify a function
5. Small function tends to be self documenting and highly readable
6. It can be called any number of times in any place without
different parameters
7. A function may behave like a traditional subroutine or as a
function or as a both
8. The function name can be any name conforming to the syntax rules
of the variables.

Q2. What is OLE? Why do we use this is VB? Explain?

Ans:- OLE stands for object linking and embedding. It is a method


where in an object from one application can be pasted to another
application and any changes in the original picture can be
brought to the pasted picture also. There is a provision to change
the pasted picture also by opening the application in which it was
originally created.
A progr. That includes OLE capabilities can communicate data
(text & Graphics) to other window application that are OLE based.
OLE control can be easily added to a form. Double click on the
OLE icon in the toolbox. VB responds by placing the OLE control
in the form and displays the insert object dialog box. The insert
object dialog box displays a list of objects that can be embedded
in a prog.

1. Select the BMP image from the list then click OK


2. Save the project

-2- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
3. Run the project

OLE has its own importance in VB. Through this tool we can make
the project/form more attractive and creative. Through this we can
call any object from any application. OLE is mainly used for
importing picture from any application to the VB form. OLE supports
the graphical features as well as text. We use OLE in VB to enhance
its working capacity/capability and to communicate data (text &
Graphics) from one window application to another OLE based.
We can add power point presentation to our form by selecting
power point presentation from the insert object dialog box.

OLE control

The insert object dialog box contains the following


objects:-

 Action BVR Control.


 Bitmap image
 Calendar control 8.0
 Color Bar class
 DHTML Edit control for IE-5
 Image documents
 Medic clip
 Microsoft- Active X control
 Clipart Gallery
 Excel worksheet & charts
 Ms-Power point presentation
 Ms-word presentation
 Video clips & wave sounds
 Windows media player
 Oracle Graphics Display
 Packages and many more

All the above mentioned items are the elements of OLE object insert
dialog box. And we all know about most of the elements and how
they work and why they are used. Then we can say that OLE works
as a bridge i.e. it minimizes the difference and gap between all
these elements and the user and thus after successful
implementation of these elements/objects in the visual Basic form
makes it more appealing thus it opens a no. Of windows to work on
it. It makes the visual basic broad in its application and thus it
creates new demands. OLE works like a soul in a body.
We can play music see movies/pictures, work with word
processors, spreadsheets, paint images and many more things
through visual basic with the help of OLE objects.

-3- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
It is easy to sue and apply in a form. It enhances the quality of
your work/project
And finally it also saves time.

Q3. Discuss the use of MDI APPLICATION?

Ans: - The MDI application allows us to create an application that


maintains multiple form within a single container form.
Applications such as MS-Word and Excel have Multiple document
interfaces.
The MDI application allows the user to display multiple
documents at the same time each of which document display in
its own window.
The MDI application can have at least two forms one is parent
form
(MDI form) and another is child form.
Document windows are contained in a parent window,
which provides a workspace for all the document windows in the
application. Each application can have only one MDI form, which
contains all the application’s child forms.
The greatest advantage of using a MDI form is that in an
MDI application, the menus for each child are displayed on the
MDI form rather than on the child form them selves. And if the
child with the focus does not have a menu, the MDI form’s menu
is displayed.
Thus in an MDI form the menu’s are created once and they
can be used with any child form on focus thus it saves time as
there is no need to create separate menus for each form.
The MDI application uses several sets of menus depending on the
type of document.
A MDI application provides a common workspace for all the child
windows.
The menu control of the MDI form or MDI child form can be
used to display the list of open child forms by setting the window
list property for that menu control to true
We can use the arrange method to rearrange the child
forms in the MDI form. The child forms can be displayed as
cascading or horizontally tiled in the lower portion of the MDI
form.

Q4. Explain the objects available in visual Basic to


access the DataBase?

Ans:- A Data Base is an organized collection of data where data is


stored in tables. Each table is a collection of columns and each
columns represents a data item.

-4- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
The database can be created in VB using the add-in prog.
The visual Data Manager. The visual Data Manager can be used
to create a Jet DataBase.
Data in these databases can be accessed in Visual Basic by
using the following options:-

 Data Control:- the data control in visual basic provides you


with the ability to write powerful database application with
very little code. we need to set just a few properties of data
control and use regular controls such as label text boxes etc to
display the value of the field. This technology provides access
to many database formats. We can easily navigate between
the records. It is easy to understand and simple to use. If the
user changes the data in bound control, these changes are
automatically updated in the Database when the user moves
to the next record.
Its important properties are

1. Connect
2. Database name
3. Exclusive
4. Read only property
5. Record source

 DAO (Data Access Object):-


The DAO uses the Jet Data Base engine to retrieve data
from jet data base like Ms-Access and ISAM data base like FoxPro
paradox etc. This is fully code approach to access the data base.
All the functionality available in Data Control is also available and
supported by DAO. It is best suited for either single system
application or for small, local deployments. Using DAO we can
create data base and build full featured application that access
existing database in many popular formats, including Ms-Access,
Btrieve, Dbase, Paradox as well as ODBC Client/server database
like Ms-SQL Server.

It provides the following programming advantages:-

1. It lets you write prog. That uses existing database.


2. It allows your application to share data with other prog.
3. It simplifies your programming since you do not need to
handle low level file accessing and searching

Database programming in VB consists of creating DAO such as


Database, TableDef, field, index etc. the properties and methods
of these objects are used to perform operations on the data base.
The result is seen in the VB form and input can be accepted using

-5- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
bound and unbound control. A DAO is a collection of object
classes that model the structure of relation database system.

VB supports DAO such as:-

 DBENGINE
 WORKSPACE
 DATABASE
 TABLEDEF
 FIELDS
 INDEX AND
 RECORDSET OBJECTS
 RDO(Remote Data Objects):-

In order to access external data source like oracle, SQL server etc
we must create an ODBC data source. The ODBC databases are
created to help VB link the specified database in VB project.
The RDO is used to read and update data stored in Relational
DataBase Management System (RDBMS) that are external to VB and
MS Jet Data Engine. RDO has methods and properties that are better
suited to work with Remote database.

 ADO(Active X Data Object):-

Visual Basic supports ADO, which can be used to access


any data source- relational databases, non-relational databases,
including Ms-Excel or any text file.
ADO accesses database through an OLEDB provider. OLEDB
is a set of general interfaces to access any data source, which
cannot be accessed directly from visual basic.

The ADO data model provides 3 components to Access


Data

1. Active X Data Control.


2. Active X Connection.
3. Active X Records.

It works just like the Data control and DAO. It can be added
easily into the project by selecting project then components and
then Ms-ADO Data Control 6.0(OLEDB) from the list.

 Data Bound Grid Control:- It is most powerful and flexible


control of VB. It can display an entire record set. Each Row in the
Grid holds a record from the databases. It also allows direct
editing of the cell value as well as the addition of new row and
the deletion of existing ones.

-6- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================

Q5. What are Active X components? How do you create Active


x control? Explain?

Ans:- In the process of developing applications, programmers write


functions/procedures which may have to be used in more than
one visual basic program. If a programmer copy the code that is
already present in another prog. Then he will have to make many
changes in the original function/procedure. To solve such a
problem the programmer can create a component of Active X.
The Active X components can be used not only in visual basic
programs but also with Vc++, Borland C++ or any other
language that supports OCX Active x control . It is based on the
old technology known as OLE extended with technology to
support Internet
The active X components are:-
 Active x control
 Active x code – EXE or DLL
 Active x Document – EXE or DLL

Active x Control:- An active x control is an interface that can be


added to any Active X consuming applications like visual basic,
Vc++. The learning edition of VB contains a no. of Active X controls
that allows you to add advanced features to your applications.
Active X control have the file name extension .OCX and can be used
in your project by manually adding them in the toolbox.

These are some of the standard Active X controls:-

 Common dialog
 Data Bound Combo Box
 Apex Data Bound Grid
 Data Bound List Box
 Microsoft Flex Grid

To create an active X control:-

1. The foundation for creating an active x control is the user control


object.
2. The user control is like the form object and adding controls is
similar to adding controls to a form object.
3. The user contain file has the extension .CTL and the graphical
elements on the user control is stored in files with extension CTX.
4. The user control object can contain code as well as visual objects
like text-box, list box etc, which are standard controls.

-7- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
5. The user control can be used in any application and the user may
resize the user control by using the user control_resize() event.

STEPS:-

 Start a new project by selecting Active x control then a user


control is created and VB automatically adds a user control
designer to the project with the default name user control1
 Add your items on the user control form like shape control text
box, timer ets.
 Write the appropriate code for the controls.
 Select project -> properties and in the properties dialog box type
the project name and description
Project name: user control1
Project Description: control to print the name
 Then save the file (The file will be saved with extension .CTL)

The binary information in a control- such as bitmaps will be saved in


a binary file with same name and an extension of .CTX
 File then save project. The project will be saved with extension
. VBP
 Then we will convert the file into an .OCX file selecting File then
make .OCX.

Thus the OCX file is created in a specified directory

Active X code components:-

The Active X code component is a server application, which


can be an EXE or a DLL file. The server is available to any active
x client like a visual basic application and is capable of serving
the client with functions, properties, events or objects.
The DLL file contains subprograms designed to be used as
the building blocks when creating a stand-alone prog. The DLL
prog. Is in-process server and and Exe file is out-of-process
server.

Active X Document:-

An active x document is a specific type of Active x object


that can be placed and activated within Active x document
containers such as Ms-Internet Explorer. An active X document
when created is a special type of form that can be loaded on to a
web browser like Internet Explorer. Compiling an Active X
document creates both a visual basic Document file (with

-8- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
extension .VBD) and its corresponding server which can be an
Active X.Dll or Active X.Exe file. We cannot place an ActiveX.Dll or
ActiveX.exe file. we cannot place embedded objects or an OLE
container control on a user document.

---------------------------------------End Of Section
01-----------------------------------

-9- P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Section 02

Q1. Explain the syntax of control structures in visual


basic with one example for each?

Ans: A control structure allows us to control the flow of out prog.


Execution. It left unchecked by control flow statements.

 Decision structure –( IF ):-

Visual basic procedures can test conditions and then,


depending on the result of the test, perform different operations.
The decision structure that visual basic includes are:-
1. If..Then
2. If..Then..Else
3. Select case

 If…Then:-

We can use either single line syntax or multiple line block syntax.

If <condition> then <statements>


If<condition then
<statement>

 If…Then…Else:-

If<condition1> then
{statement block 1}
else if<condition2> then
{statement block2}

else
{
statement block n
}
end if

Example:-
Dim marks as integer
Marks=val(text1.text)
If marks>90 then
Text2.text=”good”
Else if
Marks>70 then
Text2.text=”satisfactory”
Else

- 10 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Text2.text=”labour hard”
End if

 Select case:- It is analternate to If-Then-Else if for selective


executing one block of statements from among multiple
blocks of statements. It provides capability similar to the if..
then …else but it makes code more efficient and readable.
Syntax:-select case text expressin
{case expressionlist
[statement block 1]
{case expression list 2
[select blick 2]
…………..
…………..
}
}

Example:-Dim marks as integer


Marks=val(text1.text)
Select case marks
Case is>90
Text2.text=”excellent”
Case is>70
Text2.text=”Good”
Case is>50
Text2.text=”satisfactory”
Case else
Text2.text=”labour hard”
End select

 Loop structure-Do-While –loop:

Loop structure allows us to execute one or more lines of


code repetitively. The loop structure that visual basic supports
include
1. Do.. loop
2. For…loop.
Do loop is used to execute a block of statements an indefinite no
of times.

Syntax:- Do while<condition>
Statement
Loop
Example: dim k as integer
K=5
Do while k<10
Text1.text=k
K=k+1

- 11 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Loop

For structure-Do..loop while:

Another variation of the do..loop statement. This executes


the statement first and then test condition after each execution.
This variation guarantees at least one execution of statements.

Syntax:- Do
Statement
Loop while<condition>

Example:-

Dim dev as integer


Dev=10
Do
Text1.text=dev
Dev=dev+1
Loop while dev<10
This loop is executed once even though the condition is false

Some more syntax:-


a) Do..until <condition>
Statements
Loop
b) Do
Statmetns
Loop until <condition>

 Loop structure – For… Next:-

This loop structure is used to execute the statement a


specific no. of times for loop uses a counter variable that
increases or decreases in value during each repetition of the
loop.
Syntax:
For [counter]=start To End [step increment]
Statements
Next [counter]
The arguments counter, start and end are all integers

Example: dim k as integer


For k=1 to 10
Text1.text=k
Next k

- 12 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Loop structure- for each Next:- The loop structure is used to
repeat a group of statements each element in an array or
collection.
Dim ctrl as control
Dim ctr as integer
For each ctrl in form 1
If type of ctrl is text box then
Ctr=ctr+1
End if
Next
Msgbox(“Total no. of text boxes=”&ctr);

Q2. what are data files ? How do you create it and how do
you make use of it explain?

Ans: - Various programs need to read to read and write data to


disk to save the data for future. This is possible using data file. In
data file we save data permanently. Different types of data file
can be created in visual basic. Every file handling operation
involves operation like creation of files, writing to the file and
reading from the file.

There are three types of data file.

1. Sequential files Sequential files are most basic


type of files we can write a line of text into an opened file and
also read data from the file.

Creation of Sequential files :-



To create a sequential file we must
be open in output mode. The syntax to create a sequential file is

Open <File Name> For Output as #<File Number>

If the given file name doesn’t exist then it is


created and if the file name does exist then the old file will be
erased and created new one.
 How can we use of sequential files :-We can read
existing data from this any sequential file and also write
new data into sequential file.

(a) Writing data into sequential file -: Once file is opened in


either output or append mode means user can write a line of
text into file, for this the print # statement is used. Syntax is
:-
Open <File Name> for output as #<File number>
Or

- 13 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Open <File Name> for append as #<File number>

Print # <File Number>, <Text to be written>

(b)Readind data from sequential file-: To read the content of


an existing file we must open it for input mode. “ INPUT()”
function is used to read the content of any existing file. It takes
two parameters, first no. of bytes to be read and second file
number. Its syntax is -:

Open <File Name> for input as #<File number>


<Variable/Control>=input(<No. of bytes>, #<File
number>)

(c)Closing sequential files -: To close an opened sequential


file “CLOSE” statement is used. It takes one parameter that is file
number. Syntax :-

close #<File number>

2. Random files A random file is like a database


which is made up of records of identical size each record is
made up of field, that stores data. The advantage of Random
file over Sequential file that we can access any desire record
directly in the random file.
 Creation of random file :-
The open statement is used to
create and open a random file if the given file name is not exist
it creates a new random file otherwise is opens an existing
random file.

Syntax to create random file is :-

Open <File name.dat> for random as #<File Number>


len=<Length of record>

 How can we use random file :-


By using random file
we can write data record by record if to the disk permanently.
Using random file we can maintain data of employee, student
etc.

- 14 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================

(a)Opening random file :- Open statement is used to open


an existing random file or to create a new random file. Syntax
:-

Open <File name.dat> for random as #<File Number>


len=<Length of record>

(b)Writing record into a random file :- The “PUT”


statement is used to write data into an opened Random File.
This statement takes three parameters, first specifies file
number, second parameter specifies the record number and
the third parameter specifies the name of the variable whose
content will be written into the file. Syntax of “PUT “
statement is-:

Put #<File Number>, <Record Number>, <Variable Name>

(c)Reading record form a random file :- The “GET”


statement is used to read data from an opened random file. It
also takes three parameters. First parameter specifies the file
number, the second parameter specifies record number and
third parameter specifies the name of the variable which hold
the data. Syntax :-

Get #<File number>, <Record Number>, <Variable


name>

(d)closing random file :- “CLOSE” statement is used to


close an opened random file. It takes one parameter that is
file number.
Syntax :-

close #<File Number>

3. Binary files sequential files are accesed line by


line , random files are accessed record by record and binary
files are accessed byte by byte. In binary files we can read
from and write to any byte location.

Creation of binary file :-


“OPEN” statement is used to create binary
file. If the given file name is not exist then it creates new file
other wise it opens existing file. Syntax to create binary

- 15 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Open <File Name.txt> for Binary as #<File Number>

 How can we use binary file

(a)Opening binary file :- The “Open” statement is used to


open binary file. If the given file name is not exist then it
creates new file other wise it opens existing file. Syntax to open
binary file :-

Open <File Name.txt> for Binary as #<File Number>

(b)Reading data from binary file :- “Get” statement is used


to read the content of an opened binary file. It takes three
parameters. First for file number, second for byte location from
where reading start, third specifies name of the variable. syntax
:-

Get #<File Number>, <Byte location>, <variable name>

(c)Writing data into binary file :- The “Put” statement is


used to write data into an opened binary file. It also takes
three parameters. First for file number, second for byte location
from where writing start, third specifies name of the variable
whose content will be written. syntax :-

Put #<File Number>, <Byte Location>, <Variable name>

(d)Closing binary file :- The “Close” statement is used to


close an opened binary file. It takes one parameter that is file
number. Syntax of closing an opened binary file :-

close #<File Number

Q3. How do you create Reports using visual Basic?


List the syntax of any 5-system functions?

Ans: - Data in a database can be presented to the user in a


meaningful formal by creating Reports. Visual basic supports a
built in report generator, which can be used for this. In order to
create Report we use the Ms Data Report Tool. The Ms Data
report designer is a versatile data report generator that features
the ability to create banded hierarchical reports. Used in
conjunction with a data source such as the Data Environment
designer, you can create reports from several different relational
tables. In addition to creating printable reports, you can also

- 16 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
export the report to HTML or text files. The Data Report Designer
has

several functions:-
1. Drag and drop functionality for fields.
2. Toolbox controls
3. Print preview
4. Print reports
5. File export
6. Export templates
7. Asynchronous operation.

STEPS TO CREATE A REPORT :-

 Start a new project by selecting data project and then we see


that two designers data report designer and data environment
designer and standard form gets added to the project.
 Select the Data Environment Designer from the project explorer.
The connection object is available as connection 1. Then right
click on the connection 1 and select properties then the data link
properties dialog box is displayed then select Microsoft jet 3.51
OLEDB provider from the list
 Right click on the connection tab then select(….) the database to
fetch the records (e.g. student.mdb) , then close the dialog box.
 Right click on the connection 1 icon, then click Add command.
Then we see that command 1 is added Right click on the
command 1 object and select properties to display the command
1 dialog box. In the dialog box set the properties as shown
delow:-

Property Setting

Command name stdmask

Connection student

Data base object Table

Object name stdmast

 Then click ok to close the dialog box and save the project. Once
the Data Environment Designer has been created, you can create
a data report. To do so select the data report designer in the

- 17 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
project explorer window and set the properties of the data report
object according to the table below.

Property Setting

Name rptstud
Caption student data report

 On the properties window, click the data source and then click
data environment1

 Then click data member and click stdmast

 Right click on the data report designer and click retrieve


structure
By now we have added a new group section to the designer. Each
group name has one to one name corresponding to a command
object in the Data Environment. We also notice that he group
header has a matching group footer also from the data
Environment designer, drag the fields to the report section.
Delete the label control that is attached to the textbox control
Save the project thus the report is being created.

To view the Report:-

 On the project explorer window, double click on the form 1


icon to display the form designer.
 Place an instance of the command button on the form and set
the following properties.

Properties Setting

Name cmdshow

Caption Show Report

 In the command button pass the following codes:-

Private sub cmdshow-click ()

Data report1.show

End sub

 Save and Run (F5) the project

- 18 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
 Click the show Report Button to display the Report in print
preview mode.

SYSTEM FUNCTIONS:-

The Basic library functions also called as standard or build


in functions. Provided a quick and easy way to evaluate many
mathematical functions and carry out certain logical operations.
They are the system or library functions. These library functions
are pre written routines that are included as an integral part of
the language. Each function is accessed simply by stating its
name, followed by whatever information must be supplied to the
function, enclosed in parenthesies once the library function has
been accessed the desired operation will be carried out
automatically without the need of detailed explicit programming
Visual Basic has got a rich collection of these functions.

Some of the system functions are listed below:-

String functions:-

1) String functions:-

 Space:- Returns a string consistin of a specified no. of spaces.

Syntax:- space (number)


The number argument is the number of spaces you
want in the string. The space function is used for
formatting data output and clearing data in fixed length
strings.

e.g.

‘returns a string with 10 spaces


mystring=space(10)
‘insert 10 spaces between two strings.

Mystring=”hello”&spaces(10)&”world”

 Str: Returns a string representation of numbers.


Syntax: str(number)
e.g
mystring=str(125)
‘returns “125”

- 19 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
 Right : Returns a specified no. of characters from the right
side of the string
Syntax:- right (stringlength)
e.g

a=”hello Dev”
b=right(a,c)
‘returns “ev”

 Ltrim , Rtrim:- Returns a copy of a string without leading


spaces(ltrim), and without trailing spaces (Rtrim) or
without both leading and trailing spaces (Trim)

Syntax:- Rtrim(string)
Ltrim(string)
Trim(string)
e.g
mystring=” dev ”
a=Ltrim(mystring)
‘result “dev ”

 Ucase :- Returns a string that has been converted to upper


case
e.g
x=”debabrata”
y=ucase(x)
‘returns ”DEBABRATA”

 Val:- Returns the number contained in a string.


Syntax: val(string)

e.g:
x-val(“29”)
‘returns x=29

 Now:- Returns the current system date and time

Syntax:- now
Example:-

Today = now
‘assigns the current system data & time.

 Time:- Returns he current system time

Syntax: time

- 20 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
Eg.
Today’s time= time

‘Assign’s the current system time.

Q4. How can you access the database in visual basic ?


Explain the steps and their significance ?

Ans: A database is an organized collection of data where data is stored


in tables. Each table is a collection of columns and each column
represents a data item.
The database can be created in VB using the add-in prog.
The visual Data Manager. The visual Data Manager can be used
to create a Jet DataBase.
Data in these databases can be accessed in Visual Basic by using
the following options:-

 Data Control:- the data control in visual basic provides you


with the ability to write powerful database application with
very little code. we need to set just a few properties of data
control and use regular controls such as label text boxes etc to
display the value of the field. This technology provides access
to many database formats. We can easily navigate between
the records. It is easy to understand and simple to use. If the
user changes the data in bound control, these changes are
automatically updated in the Database when the user moves
to the next record.

Its important properties are

 Connect
 Database name
 Exclusive
 Read only property
 Record source

 DAO (Data Access Object):-


The DAO uses the Jet Data Base engine to retrieve data
from jet data base like Ms-Access and ISAM data base like FoxPro
paradox etc. This is fully code approach to access the data base.
All the functionality available in Data Control is also available and
supported by DAO. It is best suited for either single system
application or for small, local deployments. Using DAO we can
create database and build full-featured application that access
existing database in many popular formats, including Ms-Access,
Btrieve, Dbase, Paradox as well as ODBC Client/server database
like Ms-SQL Server.

- 21 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================

It provides the following programming advantages:-

It lets you write prog. That uses existing database.

It allows your application to share data with other prog.

It simplifies your programming since you do not need to


handle low level file accessing and searching

Database programming in VB consists of creating DAO such as


Database, TableDef, field, index etc. the properties and methods of
these objects are used to perform operations on the database. The
result is seen in the VB form and input can be accepted using bound
and unbound control. A DAO is a collection of object classes that
model the structure of relation database system.

VB supports DAO such as:-

 DBENGINE
 WORKSPACE
 DATABASE
 TABLEDEF
 FIELDS
 INDEX AND
 RECORDSET OBJECTS
 RDO(Remote Data Objects):-

In order to access external data source like oracle, SQL server etc
we must create an ODBC data source. The ODBC databases are
created to help VB link the specified database in VB project.
The RDO is used to read and update data stored in Relational
database Management System (RDBMS) that are external to VB and
MS Jet Data Engine. RDO has methods and properties that are better
suited to work with Remote database.

 ADO(Active X Data Object):-


Visual Basic supports ADO, which can be used to access
any data source- relational databases, non-relational databases,
including Ms-Excel or any text file.
ADO accesses database through an OLEDB provider. OLEDB
is a set of general interfaces to access any data source, which
cannot be accessed directly from visual basic.

The ADO data model provides 3 components to Access


Data:-

- 22 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================

1. Active X Data Control.


2. Active X Connection.
3. Active X Records.

It works just like the Data control and DAO. It can be added
easily into the project by selecting project then components and
then Ms-ADO Data Control 6.0 (OLEDB) from the list.

Data Bound Grid Control:- It is most powerful and flexible


control of VB. It can display an entire record set. Each Row in the
Grid holds a record from the databases. It also allows direct
editing of the cell value as well as the addition of new row and
the deletion of existing ones

Following are the steps to Access the database in Visual


Basic from STDMAST table using a Data Control.

1. Start a new standard exe project

2. On the form that appears add a data control object. By default it


will be named Data 1

3. Set the following properties of Data control


Data Base name: C:\my document\student.mdb
Record source: STDMAST

4. To display the data from the table, add text fields on to the form.
To display the data from stdmast text boxes are added to the
form. You must now bind the indivudual date bound controls to
the data control, and then specify which field in the table each of
the control will display

5. To bind a Data Bound control to a Data Control, two properties


must be set Data source and Data Fields. So select each of the
text boxes and set the properties as listed below:

NAME PROPERTY VALUE

1. TEXT1 DATASOURCE DATA1


DATA FIELD ST_ID

2. TEXT2 DATA SOURCE DATA1


DATA FIELD ST_NAME

- 23 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================

3. TEXT3 DATA SOURCE DATA1


DATA FIELD ST_CLASS

4. TEXT4 DATA SOURCE DATA1


DATA FIELD ST_CITY

At the design time, set the data source property of the bound
control to a data control. data field property can be set at
designed time or run time.

6) SAVE the project and run it (F5). Use the Arrow key of the data
control to scroll through the records of stdmast
Q5. How do you create Active x Documents? Explain?

Ans: - Active X Document:-

An active x document is a specific type


of Active x object that can be placed and activated within Active
x document containers such as Ms-Internet Explorer. An active X
document when created is a special type of form that can be
loaded on to a web browser like Internet Explorer. Compiling an
Active X document creates both a visual basic Document file
(with extension .VBD) and its corresponding server which can be
an Active X.Dll or Active X.Exe file. We cannot place an
ActiveX.Dll or ActiveX.exe file. We cannot place embedded
objects or an OLE container control on a user document.

These are the steps to create an active x document:-

On the file menu, click new project to open new project dialog
box. Double click on the active x document exe icon visual basic
automatically adds a user document designer to the project. If
the designer is not immediately visible right click over its default
name, user document 1, in the project explorer window, click new
object. When the designer appears its default name is visible in
the title bar.
In the properties window change the name of the user document
1 to first doc

In the project explorer window, double click the first doc icon to
bring it to the front
Place a command button on the form designer

Also place a text box control on the designer

- 24 -
P.T.O
B.Sc. IT 12-(01 & 02)

VISUAL BASIC
============================================================================
change the properties as

NAME PROPERTY VALUE


CAPTION

Command1 name cmdnavigate


Navigate To

Text1 Text Txtuse


http://www.manipal.com

on the firstdoc designer, double click on the command button


and add the following code

private sub cmd navigate_click()


hyperlink. Navigateto text1.text
end sub.

In order to view the first doc document, you must run the ACTIVE
X Doc project. It will load the container, Internet explorer in which
the document will be loaded.

-----------------------------------End Of Section
02-----------------------------------
THE END

- 25 -
P.T.O

Das könnte Ihnen auch gefallen