Directory listing of http: uap.unnes.ac.id ebook biblebook Visual Basic 6 com & Library Books
29
C H A P T E R
Overview of
M icrosoft Jet
✦
✦
✦
✦
In This Cha pter
I
n this c hapter, I’ll disc uss the Mic ro so ft Jet database system.
I’ll explain its arc hitec ture, data types, and sec urity mo del.
I’ll also c o ver ho w to use ADO with Jet, inc luding ho w to build a
c o nnec tio n string and use the spec ial fac ilities built into Visual
Basic to suppo rt designing Jet databases.
Intro ducing
Micro so ft Jet
Understanding the
Database architecture
Examining the
security mo del
Overview of Jet
There are perhaps mo re c o pies o f Mic ro so ft Jet installed o n
PCs than any o ther database engine, due to the fac t that Jet is a
c ritic al part o f Mic ro so ft Ac c ess, whic h is part o f the Mic ro so ft
Offic e family. This is the ideal c ho ic e when yo u want to build a
single-user database system o r share yo ur database o n a file
server with a small gro up o f peo ple.
Mic ro so ft Jet c o ntains the fo llo wing key features:
✦ Standard part o f Visual Basic Pro fessio nal Editio n and
Enterprise Editio n
✦ Limited versio n inc luded in the Visual Basic Learning
Editio n.
✦ Can be inc luded with yo ur Visual Basic applic atio n at
no c harge
✦ Requires little o r no maintenanc e to maintain
✦ Do esn’t require a dedic ated database server
✦ Co mpatible with Mic ro so ft Ac c ess
✦ Suppo rts bo th ADO and DAO o bjec t mo dels
Co nnecting to Jet
with ADO
✦
✦
✦
✦
650
Part IX ✦ M icrosoft Jet
Note
Jet and Access or Access and Jet: Since the Jet database is so tightly coupled to
the Access application, m any people use the term s interchangeably. For instance,
the Visual Data Manager tool refers to Access databases rather than Jet databases.
I w ill refer to the database itself as Jet and the application developm ent tool as
Access, unless I’m referring to a m enu item or com m and that specifically refers to
Access rather than Jet.
M icrosoft Jet versions
Mic ro so ft Jet is just the database engine c o mpo nent fro m the Mic ro so ft Ac c ess
database system. Jet bec ame available fo r the first time in 1992, when Ac c ess 1.0
was released. When Visual Basic Versio n 3.0 was released in 1993, Jet versio n 1.1 was
inc luded. Eac h new versio n o f Ac c ess resulted in a new versio n o f the Jet database
engine. When a new versio n o f Visual Basic was ready fo r release, it wo uld inc lude
the latest versio n o f the Jet database, with a few mino r impro vements.
Bec ause the same database engine is used fo r bo th Ac c ess and Visual Basic , yo u
c an build an applic atio n that inc ludes c o mpo nents in Ac c ess and in Visual Basic .
Of c o urse, yo u will need to use c o mpatible versio ns o f the so ftware. In o ther wo rds,
do n’t expec t to use an Ac c ess 1.0 database with Visual Basic 6.
Visual Basic 6 inc ludes versio n 3.51 o f the Jet database engine, whic h is basic ally
the same as the versio n 3.5 that was shipped with Ac c ess 97. Ac c ess 2000 inc ludes
versio n 4.0 o f the Jet engine, whic h c an also be used with Visual Basic . These are
the two mo st c o mmo n versio ns o f the Jet engine yo u are likely to enc o unter as a
Visual Basic pro grammer.
Caution
The tools don’t work: The tools included w ith Visual Basic 6 only w ork on version
3.5 and earlier Jet databases. If you plan to use a version 4.0 database, you m ust
have Access 2000 installed on your system or be prepared to w rite your ow n code
using ADOX or DAO to adm inistrator your database.Jet version 3.5
If yo u develo p VB6 applic atio ns with Jet, yo u are mo st likely using versio n 3.51.
This is a very stable release, and it wo rks well with Ac c ess 97. It is signific antly
faster than previo us versio ns o f the Jet engine and designed to be a little bit mo re
friendly when ac c essing a database sto red o n a file server.
Key features o f Jet 3.5 inc lude:
✦ Suppo rt fo r the mo st c o mmo n SQL statements, suc h as Select
✦ Database kept in a single file
✦ Ability to sec ure info rmatio n based o n user name and sec urity gro up
Chapter 29 ✦ Overview of M icrosoft Jet
✦ Ability to link to remo te tables and treat them as lo c al tables
✦ Database replic atio n with o ther database servers
✦ Page-level lo c king
Jet version 4.0
Ac c ess 2000 intro duc ed a newly revised Jet database system. Mo st o f the c hanges
made to Jet between Versio n 3.5 and 4.0 are transparent to the typic al user. These
c hanges were made to impro ve Ac c ess’s integratio n with o ther Offic e 2000 applic atio ns. Ho wever, so me impro vements were made in the database engine itself:
✦ Suppo rt fo r Unic o de data
✦ Rec o rd-level lo c king
✦ Migratio n utility to c o nvert versio n 3.5 and earlier databases to the versio n
4.0 fo rmat
Note
Bigger is not always better: If you convert a database from Jet 3.5 to Jet 4.0 and
the database contains a lot of character data, expect the size to double. This is due
to the fact that the actual data in the database is stored using Unicode characters.
So, even if you try to store sim ple ASCII characters, the Jet database w ill store them
using their Unicode equivalents.
Jet and DAO
Jet was designed aro und using the DAO (Data Ac c ess Objec ts) to ac c ess the c o ntents o f the database. DAO is a highly-struc tured o bjec t mo del that has evo lved
alo ngside Jet sinc e its beginnings. In many ways, it is diffic ult to view the Jet
database engine witho ut thinking o f the DAO o bjec t library.
CrossReference
See Chapter 6, “Accessing Databases from Visual Basic,” for explanations of the
DAO, ADO, and RDO m odels.
There are mo re pro grams to day that use DAO, rather than ADO, to ac c ess Jet
databases. Ho wever, I rec o mmend using ADO o ver DAO fo r two reaso ns. First the
ADO o bjec t library o ffers a simpler interfac e to yo ur database, whic h translates
into less c o de needed to do the same wo rk. Sec o nd, Mic ro so ft has dec ided that
ADO is the way everyo ne sho uld ac c ess databases using Mic ro so ft develo pment
to o ls suc h as Visual Basic . While this do esn’t mean that DAO is DOA, it do es mean
that its days are numbered. Ac c ess 2000 already suppo rts ADO ac c ess to the Jet
database, whic h inc identally allo ws yo u to use Ac c ess 2000 as a develo pment to o l
fo r SQL Server databases. Also , Mic ro so ft has develo ped a c lient c o mputer friendly
versio n o f SQL Server c alled the Mic ro so ft Database Engine (MSDE), whic h is 100%
upwards c o mpatible with SQL Server — so mething that Jet is no t.
651
652
Part IX ✦ M icrosoft Jet
Note
Jet 4.0, DAO 3.6: Even though the Jet database engine has been upgraded to version 4.0, the Data Access Objects (DAO) m ost often associated w ith the Jet
database have been enhanced to version 3.6.
Jet utilities
Fo r the mo st part, there are very few utilities asso c iated with the Jet database,
o ther than Ac c ess o f c o urse. Visual Basic inc ludes a to o l c alled the Visual Data
Manager. This is an add-in to o l that allo ws yo u to design Jet databases. (I’ll talk
mo re abo ut the Visual Data Manager in Chapter 30).
Understanding the Database Architecture
Mic ro so ft Jet is designed to be an SQL-based database management system that
suppo rts a handful o f c o nc urrent users. It wo rks espec ially well with o nly a single
user. While it c an easily be used with Visual Basic applic atio ns, muc h o f the arc hitec ture revo lves aro und suppo rting Ac c ess applic atio ns.
Unlike mo st database systems, Mic ro so ft Jet relies o n shared ac c ess to a single file
to c o o rdinate ac tivities amo ng multiple users. This means that the c o de that manages the database must run in eac h user’s address spac e and that yo u c an’t buffer
data in memo ry, bec ause different pro grams c an’t share their memo ry. All sharing
must be do ne at the file level, using file lo c king and o ther tec hniques. This really
limits perfo rmanc e and means that yo u pro bably wo n’t even get c lo se to Jet’s 255
c o nc urrent user limit.
.M DB files
All o f the info rmatio n fo r the Jet database is sto red in a single file, c alled the .MDB
file. This file c o ntains all o f the data, inc luding tables and indexes, that c o mprise
the Jet database. In additio n, fac ilities are inc luded that permit Ac c ess to sto re
info rmatio n in the same file witho ut sto ring it in regular database tables.
Data is sto red in an .MDB file using different types o f pages. The first page in the
file is the database header page, whic h c o ntains general info rmatio n abo ut the
database, plus info rmatio n that trac ks user o peratio ns in a multi-user enviro nment. Table pages are used to ho ld info rmatio n sto red in a table, while index pages
ho ld index info rmatio n. Lo ng value pages keep lo ng values suc h as Text and Memo
fields, whic h c an exc eed the size o f a no rmal page. Finally, system table info rmatio n is sto red in its o wn unique type o f page.
Chapter 29 ✦ Overview of M icrosoft Jet
There are fo ur system tables:
✦ MSysACES ho lds sec urity permissio ns.
✦ MSysObjects ho lds info rmatio n abo ut eac h database o bjec t, suc h as a table o r
index, plus info rmatio n o n Ac c ess o bjec ts, suc h as fo rms and repo rts.
✦ MSysQueries ho lds info rmatio n abo ut predefined queries in the database.
✦ MSysRelationships ho lds info rmatio n abo ut predefined relatio nships between
tables in the databases.
In additio n to these tables, o ther tables will sto re info rmatio n abo ut o bjec ts used
o nly in Ac c ess applic atio ns.
Note
You didn’t hear this from me: Microsoft considers the system tables in Access to
be undocum ented. This basically m eans that Microsoft reserves the right to
change the organization and num ber of system tables w ithout notifying anyone.
Therefore, you shouldn’t attem pt to use these tables as part of your application.
.LDB files
When so meo ne o pens an .MDB file fo r the first time, an .LDB file is c reated in the
same direc to ry as the .MDB file. After the last user c lo ses the database, the .LDB file
will auto matic ally be deleted. This file c o ntains lo c king info rmatio n fo r the .MDB file.
Eac h user that has a c o nnec tio n to the database has an entry in the .LDB file. This
entry c o ntains the user name and the name o f the c o mputer that is being used.
When the user lo c ks a piec e o f data in the .MDB file, an extended byte range lo c k is
plac ed o n the .LDB file. With a little c reativity, this type o f lo c k pro vides bo th shared
and exc lusive ac c ess to data in the .MDB file witho ut the o verhead o f using physic al
lo c ks o n the .MDB file itself.
.M DW Files
The sec urity info rmatio n fo r a Jet database is sto red in an .MDW file, whic h is also
kno wn as a wo rkgro up file. This file c o ntains info rmatio n abo ut users and gro ups,
inc luding their lo gin passwo rds. Onc e a user is validated, they will be assigned an
internal sec urity identifier (SID). The permissio ns that asso c iate the SID with the
vario us database o bjec ts are sto red inside the .MDB file.
Note
M DA or M DW: Depending on the version of Access you have installed, the
SYSTEM.MDA file m ay also be know n as SYSTEM.MDW.
653
654
Part IX ✦ M icrosoft Jet
Database objects
As yo u might expec t, a Jet database c an ho ld many o f the same database o bjec ts
that are fo und in o ther database management systems. Ho wever, due to the nature
o f the target audienc e, so me o f the database o bjec ts yo u wish to use may no t be
available. While tables and indexes are present in Jet, views and triggers are no t.
Key database o bjec ts inc lude:
✦ Tables , whic h sto re user data in fields.
✦ Indexes , whic h pro vide quic k ways to lo c ate info rmatio n in a table.
✦ Relationships , whic h are used to define relatio nships between tables and to
enfo rc e referential integrity.
✦ Queries , whic h are used to define standard queries (similar to views) against
the database.
✦ Users and Groups , whic h are used to asso c iate individuals with database permissio ns.
Linked databases
One o f the nic er fac ilities in Jet is the ability to c reate a link to a table in ano ther
database in suc h a way that the pro grammer thinks the table is lo c al to the Jet
database. Besides linking to o ther Jet databases, yo u c an also link to these
databases:
✦ dBase
✦ Fo xPro
✦ Parado x
✦ Exc el
✦ Exc hange and Outlo o k data files (read ac c ess o nly)
✦ So me text file fo rmats, inc luding c o mma-separated value files
✦ Any ODBC-c o mpliant database, suc h as SQL Server o r Orac le8
Tip
Link-be-gone: In the past, the only w ay to access a rem ote database w as to create a link using a Jet database. With Visual Basic 6, both DAO and ADO m ake this
feature unnecessary. You can now access any ODBC-com patible database directly
w ith DAO and the ODBCDirect interface, or you can use the m ore flexible OLE DB
w ith ADO. (Of course, you can alw ays use RDO, but ADO m akes that tool even
m ore obsolete than DAO).
Chapter 29 ✦ Overview of M icrosoft Jet
Database capacities
Table 29-1 lists so me o f the key c apac ities and limitatio ns o f the Jet database.
Table 29-1
Database Capacities
Item
Capacity
Maxim um database size
1 gigabyte
Maxim um characters per object nam e
64
Maxim um characters per user nam e
20
Maxim um characters per user passw ord
14
Maxim um characters per database passw ord
20
Maxim um num ber of concurrent users
255
Maxim um characters in a table nam e
64
Maxim um characters in a field nam e
64
Maxim um table size
1 gigabyte
Maxim um num ber of fields per table
255
Maxim um num ber of indexes per table
32
Maxim um num ber of fields in an index
10
Maxim um num ber of fields in an Order By clause
10
Jet data types
Jet suppo rts a wide variety o f data types, as sho wn in Table 29-2.
Connecting to Jet with ADO
In o rder to c o nnec t to a Jet database using ADO, yo u c an use the fo llo wing c o nnec tio n string:
Provider=MSDAORA.1; Data source=vb6db.Athena.justpc.net
655
656
Part IX ✦ M icrosoft Jet
Table 29-2
Jet Data Types
Jet Data Type
Visual Basic Data Type
Comments
Boolean
Boolean
Null values can’t be used w ith a Bit field
Byte
Byte
Holds num bers from 0 to 255
Currency
Currency
Eight-byte scaled integer accurate to four
decim al places
Date
Date
A date and tim e value ranging from 1 Jan
100 to 31 Dec 9999
Double
Double
A 64-bit floating-point num ber
Integer
Integer
Holds num bers from –32,768 to + 32,767
(16-bit integer)
Long
Long
Holds num bers from –2,147,483,648 to
+ 2,147,483,647 (32-bit integer)
Memo
String
A text field containing up to 1.2 billion
characters
OLE Object
Byte Array
Holds pictures and other large, raw binary
values up to 1.2 gigabytes in length
Single
Single
A 32-bit floating-point num ber
Text
String
Variable length string up to 255 characters
in length
If yo u spec ified user-level sec urity in yo ur database, yo u c an also inc lude the User
ID= and Password= keywo rds if yo u want to inc lude that info rmatio n as part o f the
c o nnec tio n string — o r yo u c an supply them as arguments to the Open metho d.
Of c o urse, yo u c an use the Data Link Pro perties windo w to c o nfigure the Jet Pro vider.
Selec t the pro vider that c o rrespo nds to the versio n o f Jet yo u are using (see Figure
29-1). After pressing the Next butto n, enter the name o f yo ur database, and the user
name and passwo rd needed to ac c ess the database (see Figure 29-2). If yo u haven’t
implemented user-level sec urity, yo u c an leave the default values fo r user name
( Admin) and passwo rd (Blank passwo rd). To test the c o nnec tio n, press the Test
Co nnec tio n butto n.
Chapter 29 ✦ Overview of M icrosoft Jet
Figure 29-1: Choosing the proper OLE
DB Jet provider.
Figure 29-2: Specifying the nam e of
the database, user nam e, and passw ord
inform ation.
If yo u spec ified a database passwo rd, yo u c an spec ify that value o n the All tab o f
the Data Link Pro perties windo w. Simply selec t Jet OLEDB: Database Password
field, press the Edit Value butto n, and enter the appro priate value (see Figure 29-3).
657
658
Part IX ✦ M icrosoft Jet
Figure 29-3: Specifying a database
passw ord.
Jet security
Like o ther database systems, Jet inc o rpo rates its o wn sec urity subsystem. While
yo u c an c ho o se to use it o r igno re it, do n’t igno re it if yo u plan to let mo re than o ne
perso n ac c ess yo ur database. In fac t, yo u sho uld use it even if o nly o ne perso n will
use yo ur database. It just might sto p them fro m c o rrupting the database by preventing them fro m o pening the database with any o ther applic atio n.
Share-level security
In share-level sec urity, the database is assigned a passwo rd. Anyo ne that kno ws the
passwo rd c an ac c ess the database. While this isn’t as sec ure as user-level sec urity
(disc ussed next), it will prevent a user fro m ac c essing yo ur database with Exc el o r
Ac c ess. Yo u c an set the passwo rd using Ac c ess o r by using the DAO CreateDatabase
metho d in the Wo rkspac e o bjec t. Simply append the string ;pwd=MyPassword to the
end o f the Locale parameter to set the database passwo rd to MyPassword. Yo u then
spec ify the passwo rd in the Connect parameter o f the DAO OpenDatabase metho d. In
ADO, yo u simply spec ify the passwo rd as part o f the Connection string by using the
;pwd=MyPassword keywo rd.
Chapter 29 ✦ Overview of M icrosoft Jet
User-level security
User-level sec urity wo rks like the sec urity systems in SQL Server and Orac le8i. A
userID and passwo rd are c reated fo r eac h perso n who ac c esses the database. Then
the userID is granted permissio n to ac c ess the vario us o bjec ts in the database. Gro ups
c an be used with user-level sec urity to simplify administratio n. Yo u c an c reate a gro up
and assign permissio ns to it, just as if it was ano ther user. Then yo u c an assign gro ups
to vario us userIDs. When users lo g o n to the database, they will inherit all o f the permissio ns asso c iated with the gro up o r gro ups that have been assigned to them, plus
any individual permissio ns they may have been granted.
Finding the workgroup file
Sec urity info rmatio n is kept in a wo rkgro up file ( .MDW). Only o ne wo rkgro up file
is required per system, and the fo llo wing windo ws registry key will be used to
lo c ate it:
HKEY_LOCAL_SYSTEM\Software\Microsoft\Office\8.0\Access\Jet\3.5\
Engine\SystemDB
A typic al value fo r this key is:
C:\WINDOWS\SYSTEM\system.mdw
Of c o urse, if yo u are using a Jet 4.0 database with Ac c ess 2000 (Versio n 9.0), yo u
will need to adjust the registry key to inc lude the appro priate versio n numbers.
Note
Security with Access: In order to use security w ith a Jet database, you m ust have
installed Access 97 (or Access 2000 for a Jet 4.0 database). The file SYSTEM.MDW
w ill autom atically be installed.
SIDs, user ids, personal ids, and passwords
All info rmatio n inside a database file is sec ured o n the basis o f a Sec urity ID, o r SID.
A SID is derived by enc rypting the user id and a spec ial value kno wn as a perso nal
id. If yo u c reate a user id and do n’t bo ther to assign a perso nal id, it is po ssible fo r
so meo ne else to c reate ano ther wo rkgro up file with the same user id and their o wn
passwo rd and user id to gain ac c ess to database reso urc es fo r whic h they do no t
have permissio n.
The perso nal id is used to ensure that the SID is uniq ue and c an’t b e duplic ated,
so yo u sho uld always c ho o se a value that is hard to duplic ate. Simply c ho o sing
so meo ne’s first name as the user id and their last name fo r the perso nal id is no t
a go o d idea.
659
660
Part IX ✦ M icrosoft Jet
By default, the user id Admin exists in all database systems with the same perso nal
id. This means that Admin has the same SID no matter whic h c o mputer they use.
The same is true fo r the Users gro up. Ho wever, this is no t true fo r the Admins gro up.
The SID asso c iated with the Admins gro up is always unique fo r eac h wo rkgro up file.
Sinc e anyo ne in the Admins gro up has ac c ess to the entire database, the first thing
yo u want to do is c reate a new user id and assign it to the Admins gro up. Then yo u
sho uld remo ve the Admin user id fro m the Admins gro up to ensure that so meo ne
do esn’t use their o wn wo rkgro up file to o verride yo ur database’s sec urity.
Permissions, SIDs, and ownership
The wo rkgro up file exists simply to return a SID fo r a partic ular user and ano ther
SID fo r eac h gro up that the user is a member o f. This value is used as a key into
so me o f the system tables inside the database file to determine whic h permissio ns
the user has.
If the user has o verlapping permissio ns fro m multiple gro ups and/ o r explic it permissio n assignments, the least restric tive permissio n will apply. Thus, if a user has read
and write permissio n to the Custo mers table via the Clerks gro up and has been
explic itly granted read permissio n to the Custo mers table, the user will have
read/ write ac c ess to the Custo mers table.
Whenever an o b jec t is c reated in the datab ase, the user id o f the individual that
c reated the o b jec t is used as the o b jec t’s o wner. The o b jec t’s o wner always has
ac c ess to the o b jec t. Yo u c an and sho uld c hange the o wnership o f the o b jec ts in
yo ur datab ase to a user id o ther than Admin to prevent a sec urity pro b lem. I suggest that yo u use the Admins gro up as the o b jec t’s o wner, whic h means that anyo ne in the Admins gro up c an always ac c ess the o b jec t.
Thoughts on M icrosoft Jet
Microsoft Jet is a low -end database system designed prim arily for use w ith Microsoft
Access. Much of the design revolves around features that are im portant to Access users,
w hile features that m ight be useful to Visual Basic program m ers aren’t present. Jet is not
upw ards-com patible w ith SQL Server and m ost other databases, due to these lim itations.
Jet is also lim ited to a m axim um of 255 concurrent users. How ever, its true m axim um is far
less, depending on how frequently the database is accessed. While m any im provem ents
have been m ade to m ake Jet m ore efficient, it is still lim ited by its file-oriented approach to
synchronization for m ultiple users.
Just because Jet isn’t perfect doesn’t m ean you should avoid using it. The Jet database
engine is a standard part of the Visual Basic Professional Edition. This m eans that you don’t
have to purchase it separately. Also, there aren’t any licensing charges associated w ith Jet.
Chapter 29 ✦ Overview of M icrosoft Jet
This m akes it ideal for those applications that are cost-sensitive, such as sharew are, or part
of a large distributed application w here you w ant to cache data on the local com puter.
Jet has also been around for a long tim e, though the ability to access it using ADO hasn’t.
Until recently, DAO included the ability to create database objects through its object m odel,
w hile ADO could only create a database object by executing the appropriate SQL statem ent
using the Command object. Since Visual Basic 6 w as released, ADO has gone from version
2.0 to 2.1, and now to 2.5. The new er versions of ADO now include an independent object
library know n as ADOX, or ADO Extensions for Data Definition Language and Security. These
objects im prove on DAO’s ability to create database objects w hile using an OLE DB interface to the database. So if you need to build database objects directly from your code, you
no longer have to use DAO.
Summary
In this c hapter yo u learned:
✦ abo ut the vario us versio ns o f the Jet database system.
✦ that the to o ls in Visual Basic do no t suppo rt Jet 4.0.
✦ that yo u c an use DAO 3.6 o r ADO with the appro priate pro vider to write pro grams that wo rk with Jet 4.0.
✦ abo ut the arc hitec ture o f the Jet database.
✦ ho w to c o nnec t to a Jet database.
✦ abo ut the sec urity c apabilities fo r a Jet database.
✦
✦
✦
661
C H A P T E R
Overview of
M icrosoft Jet
✦
✦
✦
✦
In This Cha pter
I
n this c hapter, I’ll disc uss the Mic ro so ft Jet database system.
I’ll explain its arc hitec ture, data types, and sec urity mo del.
I’ll also c o ver ho w to use ADO with Jet, inc luding ho w to build a
c o nnec tio n string and use the spec ial fac ilities built into Visual
Basic to suppo rt designing Jet databases.
Intro ducing
Micro so ft Jet
Understanding the
Database architecture
Examining the
security mo del
Overview of Jet
There are perhaps mo re c o pies o f Mic ro so ft Jet installed o n
PCs than any o ther database engine, due to the fac t that Jet is a
c ritic al part o f Mic ro so ft Ac c ess, whic h is part o f the Mic ro so ft
Offic e family. This is the ideal c ho ic e when yo u want to build a
single-user database system o r share yo ur database o n a file
server with a small gro up o f peo ple.
Mic ro so ft Jet c o ntains the fo llo wing key features:
✦ Standard part o f Visual Basic Pro fessio nal Editio n and
Enterprise Editio n
✦ Limited versio n inc luded in the Visual Basic Learning
Editio n.
✦ Can be inc luded with yo ur Visual Basic applic atio n at
no c harge
✦ Requires little o r no maintenanc e to maintain
✦ Do esn’t require a dedic ated database server
✦ Co mpatible with Mic ro so ft Ac c ess
✦ Suppo rts bo th ADO and DAO o bjec t mo dels
Co nnecting to Jet
with ADO
✦
✦
✦
✦
650
Part IX ✦ M icrosoft Jet
Note
Jet and Access or Access and Jet: Since the Jet database is so tightly coupled to
the Access application, m any people use the term s interchangeably. For instance,
the Visual Data Manager tool refers to Access databases rather than Jet databases.
I w ill refer to the database itself as Jet and the application developm ent tool as
Access, unless I’m referring to a m enu item or com m and that specifically refers to
Access rather than Jet.
M icrosoft Jet versions
Mic ro so ft Jet is just the database engine c o mpo nent fro m the Mic ro so ft Ac c ess
database system. Jet bec ame available fo r the first time in 1992, when Ac c ess 1.0
was released. When Visual Basic Versio n 3.0 was released in 1993, Jet versio n 1.1 was
inc luded. Eac h new versio n o f Ac c ess resulted in a new versio n o f the Jet database
engine. When a new versio n o f Visual Basic was ready fo r release, it wo uld inc lude
the latest versio n o f the Jet database, with a few mino r impro vements.
Bec ause the same database engine is used fo r bo th Ac c ess and Visual Basic , yo u
c an build an applic atio n that inc ludes c o mpo nents in Ac c ess and in Visual Basic .
Of c o urse, yo u will need to use c o mpatible versio ns o f the so ftware. In o ther wo rds,
do n’t expec t to use an Ac c ess 1.0 database with Visual Basic 6.
Visual Basic 6 inc ludes versio n 3.51 o f the Jet database engine, whic h is basic ally
the same as the versio n 3.5 that was shipped with Ac c ess 97. Ac c ess 2000 inc ludes
versio n 4.0 o f the Jet engine, whic h c an also be used with Visual Basic . These are
the two mo st c o mmo n versio ns o f the Jet engine yo u are likely to enc o unter as a
Visual Basic pro grammer.
Caution
The tools don’t work: The tools included w ith Visual Basic 6 only w ork on version
3.5 and earlier Jet databases. If you plan to use a version 4.0 database, you m ust
have Access 2000 installed on your system or be prepared to w rite your ow n code
using ADOX or DAO to adm inistrator your database.Jet version 3.5
If yo u develo p VB6 applic atio ns with Jet, yo u are mo st likely using versio n 3.51.
This is a very stable release, and it wo rks well with Ac c ess 97. It is signific antly
faster than previo us versio ns o f the Jet engine and designed to be a little bit mo re
friendly when ac c essing a database sto red o n a file server.
Key features o f Jet 3.5 inc lude:
✦ Suppo rt fo r the mo st c o mmo n SQL statements, suc h as Select
✦ Database kept in a single file
✦ Ability to sec ure info rmatio n based o n user name and sec urity gro up
Chapter 29 ✦ Overview of M icrosoft Jet
✦ Ability to link to remo te tables and treat them as lo c al tables
✦ Database replic atio n with o ther database servers
✦ Page-level lo c king
Jet version 4.0
Ac c ess 2000 intro duc ed a newly revised Jet database system. Mo st o f the c hanges
made to Jet between Versio n 3.5 and 4.0 are transparent to the typic al user. These
c hanges were made to impro ve Ac c ess’s integratio n with o ther Offic e 2000 applic atio ns. Ho wever, so me impro vements were made in the database engine itself:
✦ Suppo rt fo r Unic o de data
✦ Rec o rd-level lo c king
✦ Migratio n utility to c o nvert versio n 3.5 and earlier databases to the versio n
4.0 fo rmat
Note
Bigger is not always better: If you convert a database from Jet 3.5 to Jet 4.0 and
the database contains a lot of character data, expect the size to double. This is due
to the fact that the actual data in the database is stored using Unicode characters.
So, even if you try to store sim ple ASCII characters, the Jet database w ill store them
using their Unicode equivalents.
Jet and DAO
Jet was designed aro und using the DAO (Data Ac c ess Objec ts) to ac c ess the c o ntents o f the database. DAO is a highly-struc tured o bjec t mo del that has evo lved
alo ngside Jet sinc e its beginnings. In many ways, it is diffic ult to view the Jet
database engine witho ut thinking o f the DAO o bjec t library.
CrossReference
See Chapter 6, “Accessing Databases from Visual Basic,” for explanations of the
DAO, ADO, and RDO m odels.
There are mo re pro grams to day that use DAO, rather than ADO, to ac c ess Jet
databases. Ho wever, I rec o mmend using ADO o ver DAO fo r two reaso ns. First the
ADO o bjec t library o ffers a simpler interfac e to yo ur database, whic h translates
into less c o de needed to do the same wo rk. Sec o nd, Mic ro so ft has dec ided that
ADO is the way everyo ne sho uld ac c ess databases using Mic ro so ft develo pment
to o ls suc h as Visual Basic . While this do esn’t mean that DAO is DOA, it do es mean
that its days are numbered. Ac c ess 2000 already suppo rts ADO ac c ess to the Jet
database, whic h inc identally allo ws yo u to use Ac c ess 2000 as a develo pment to o l
fo r SQL Server databases. Also , Mic ro so ft has develo ped a c lient c o mputer friendly
versio n o f SQL Server c alled the Mic ro so ft Database Engine (MSDE), whic h is 100%
upwards c o mpatible with SQL Server — so mething that Jet is no t.
651
652
Part IX ✦ M icrosoft Jet
Note
Jet 4.0, DAO 3.6: Even though the Jet database engine has been upgraded to version 4.0, the Data Access Objects (DAO) m ost often associated w ith the Jet
database have been enhanced to version 3.6.
Jet utilities
Fo r the mo st part, there are very few utilities asso c iated with the Jet database,
o ther than Ac c ess o f c o urse. Visual Basic inc ludes a to o l c alled the Visual Data
Manager. This is an add-in to o l that allo ws yo u to design Jet databases. (I’ll talk
mo re abo ut the Visual Data Manager in Chapter 30).
Understanding the Database Architecture
Mic ro so ft Jet is designed to be an SQL-based database management system that
suppo rts a handful o f c o nc urrent users. It wo rks espec ially well with o nly a single
user. While it c an easily be used with Visual Basic applic atio ns, muc h o f the arc hitec ture revo lves aro und suppo rting Ac c ess applic atio ns.
Unlike mo st database systems, Mic ro so ft Jet relies o n shared ac c ess to a single file
to c o o rdinate ac tivities amo ng multiple users. This means that the c o de that manages the database must run in eac h user’s address spac e and that yo u c an’t buffer
data in memo ry, bec ause different pro grams c an’t share their memo ry. All sharing
must be do ne at the file level, using file lo c king and o ther tec hniques. This really
limits perfo rmanc e and means that yo u pro bably wo n’t even get c lo se to Jet’s 255
c o nc urrent user limit.
.M DB files
All o f the info rmatio n fo r the Jet database is sto red in a single file, c alled the .MDB
file. This file c o ntains all o f the data, inc luding tables and indexes, that c o mprise
the Jet database. In additio n, fac ilities are inc luded that permit Ac c ess to sto re
info rmatio n in the same file witho ut sto ring it in regular database tables.
Data is sto red in an .MDB file using different types o f pages. The first page in the
file is the database header page, whic h c o ntains general info rmatio n abo ut the
database, plus info rmatio n that trac ks user o peratio ns in a multi-user enviro nment. Table pages are used to ho ld info rmatio n sto red in a table, while index pages
ho ld index info rmatio n. Lo ng value pages keep lo ng values suc h as Text and Memo
fields, whic h c an exc eed the size o f a no rmal page. Finally, system table info rmatio n is sto red in its o wn unique type o f page.
Chapter 29 ✦ Overview of M icrosoft Jet
There are fo ur system tables:
✦ MSysACES ho lds sec urity permissio ns.
✦ MSysObjects ho lds info rmatio n abo ut eac h database o bjec t, suc h as a table o r
index, plus info rmatio n o n Ac c ess o bjec ts, suc h as fo rms and repo rts.
✦ MSysQueries ho lds info rmatio n abo ut predefined queries in the database.
✦ MSysRelationships ho lds info rmatio n abo ut predefined relatio nships between
tables in the databases.
In additio n to these tables, o ther tables will sto re info rmatio n abo ut o bjec ts used
o nly in Ac c ess applic atio ns.
Note
You didn’t hear this from me: Microsoft considers the system tables in Access to
be undocum ented. This basically m eans that Microsoft reserves the right to
change the organization and num ber of system tables w ithout notifying anyone.
Therefore, you shouldn’t attem pt to use these tables as part of your application.
.LDB files
When so meo ne o pens an .MDB file fo r the first time, an .LDB file is c reated in the
same direc to ry as the .MDB file. After the last user c lo ses the database, the .LDB file
will auto matic ally be deleted. This file c o ntains lo c king info rmatio n fo r the .MDB file.
Eac h user that has a c o nnec tio n to the database has an entry in the .LDB file. This
entry c o ntains the user name and the name o f the c o mputer that is being used.
When the user lo c ks a piec e o f data in the .MDB file, an extended byte range lo c k is
plac ed o n the .LDB file. With a little c reativity, this type o f lo c k pro vides bo th shared
and exc lusive ac c ess to data in the .MDB file witho ut the o verhead o f using physic al
lo c ks o n the .MDB file itself.
.M DW Files
The sec urity info rmatio n fo r a Jet database is sto red in an .MDW file, whic h is also
kno wn as a wo rkgro up file. This file c o ntains info rmatio n abo ut users and gro ups,
inc luding their lo gin passwo rds. Onc e a user is validated, they will be assigned an
internal sec urity identifier (SID). The permissio ns that asso c iate the SID with the
vario us database o bjec ts are sto red inside the .MDB file.
Note
M DA or M DW: Depending on the version of Access you have installed, the
SYSTEM.MDA file m ay also be know n as SYSTEM.MDW.
653
654
Part IX ✦ M icrosoft Jet
Database objects
As yo u might expec t, a Jet database c an ho ld many o f the same database o bjec ts
that are fo und in o ther database management systems. Ho wever, due to the nature
o f the target audienc e, so me o f the database o bjec ts yo u wish to use may no t be
available. While tables and indexes are present in Jet, views and triggers are no t.
Key database o bjec ts inc lude:
✦ Tables , whic h sto re user data in fields.
✦ Indexes , whic h pro vide quic k ways to lo c ate info rmatio n in a table.
✦ Relationships , whic h are used to define relatio nships between tables and to
enfo rc e referential integrity.
✦ Queries , whic h are used to define standard queries (similar to views) against
the database.
✦ Users and Groups , whic h are used to asso c iate individuals with database permissio ns.
Linked databases
One o f the nic er fac ilities in Jet is the ability to c reate a link to a table in ano ther
database in suc h a way that the pro grammer thinks the table is lo c al to the Jet
database. Besides linking to o ther Jet databases, yo u c an also link to these
databases:
✦ dBase
✦ Fo xPro
✦ Parado x
✦ Exc el
✦ Exc hange and Outlo o k data files (read ac c ess o nly)
✦ So me text file fo rmats, inc luding c o mma-separated value files
✦ Any ODBC-c o mpliant database, suc h as SQL Server o r Orac le8
Tip
Link-be-gone: In the past, the only w ay to access a rem ote database w as to create a link using a Jet database. With Visual Basic 6, both DAO and ADO m ake this
feature unnecessary. You can now access any ODBC-com patible database directly
w ith DAO and the ODBCDirect interface, or you can use the m ore flexible OLE DB
w ith ADO. (Of course, you can alw ays use RDO, but ADO m akes that tool even
m ore obsolete than DAO).
Chapter 29 ✦ Overview of M icrosoft Jet
Database capacities
Table 29-1 lists so me o f the key c apac ities and limitatio ns o f the Jet database.
Table 29-1
Database Capacities
Item
Capacity
Maxim um database size
1 gigabyte
Maxim um characters per object nam e
64
Maxim um characters per user nam e
20
Maxim um characters per user passw ord
14
Maxim um characters per database passw ord
20
Maxim um num ber of concurrent users
255
Maxim um characters in a table nam e
64
Maxim um characters in a field nam e
64
Maxim um table size
1 gigabyte
Maxim um num ber of fields per table
255
Maxim um num ber of indexes per table
32
Maxim um num ber of fields in an index
10
Maxim um num ber of fields in an Order By clause
10
Jet data types
Jet suppo rts a wide variety o f data types, as sho wn in Table 29-2.
Connecting to Jet with ADO
In o rder to c o nnec t to a Jet database using ADO, yo u c an use the fo llo wing c o nnec tio n string:
Provider=MSDAORA.1; Data source=vb6db.Athena.justpc.net
655
656
Part IX ✦ M icrosoft Jet
Table 29-2
Jet Data Types
Jet Data Type
Visual Basic Data Type
Comments
Boolean
Boolean
Null values can’t be used w ith a Bit field
Byte
Byte
Holds num bers from 0 to 255
Currency
Currency
Eight-byte scaled integer accurate to four
decim al places
Date
Date
A date and tim e value ranging from 1 Jan
100 to 31 Dec 9999
Double
Double
A 64-bit floating-point num ber
Integer
Integer
Holds num bers from –32,768 to + 32,767
(16-bit integer)
Long
Long
Holds num bers from –2,147,483,648 to
+ 2,147,483,647 (32-bit integer)
Memo
String
A text field containing up to 1.2 billion
characters
OLE Object
Byte Array
Holds pictures and other large, raw binary
values up to 1.2 gigabytes in length
Single
Single
A 32-bit floating-point num ber
Text
String
Variable length string up to 255 characters
in length
If yo u spec ified user-level sec urity in yo ur database, yo u c an also inc lude the User
ID= and Password= keywo rds if yo u want to inc lude that info rmatio n as part o f the
c o nnec tio n string — o r yo u c an supply them as arguments to the Open metho d.
Of c o urse, yo u c an use the Data Link Pro perties windo w to c o nfigure the Jet Pro vider.
Selec t the pro vider that c o rrespo nds to the versio n o f Jet yo u are using (see Figure
29-1). After pressing the Next butto n, enter the name o f yo ur database, and the user
name and passwo rd needed to ac c ess the database (see Figure 29-2). If yo u haven’t
implemented user-level sec urity, yo u c an leave the default values fo r user name
( Admin) and passwo rd (Blank passwo rd). To test the c o nnec tio n, press the Test
Co nnec tio n butto n.
Chapter 29 ✦ Overview of M icrosoft Jet
Figure 29-1: Choosing the proper OLE
DB Jet provider.
Figure 29-2: Specifying the nam e of
the database, user nam e, and passw ord
inform ation.
If yo u spec ified a database passwo rd, yo u c an spec ify that value o n the All tab o f
the Data Link Pro perties windo w. Simply selec t Jet OLEDB: Database Password
field, press the Edit Value butto n, and enter the appro priate value (see Figure 29-3).
657
658
Part IX ✦ M icrosoft Jet
Figure 29-3: Specifying a database
passw ord.
Jet security
Like o ther database systems, Jet inc o rpo rates its o wn sec urity subsystem. While
yo u c an c ho o se to use it o r igno re it, do n’t igno re it if yo u plan to let mo re than o ne
perso n ac c ess yo ur database. In fac t, yo u sho uld use it even if o nly o ne perso n will
use yo ur database. It just might sto p them fro m c o rrupting the database by preventing them fro m o pening the database with any o ther applic atio n.
Share-level security
In share-level sec urity, the database is assigned a passwo rd. Anyo ne that kno ws the
passwo rd c an ac c ess the database. While this isn’t as sec ure as user-level sec urity
(disc ussed next), it will prevent a user fro m ac c essing yo ur database with Exc el o r
Ac c ess. Yo u c an set the passwo rd using Ac c ess o r by using the DAO CreateDatabase
metho d in the Wo rkspac e o bjec t. Simply append the string ;pwd=MyPassword to the
end o f the Locale parameter to set the database passwo rd to MyPassword. Yo u then
spec ify the passwo rd in the Connect parameter o f the DAO OpenDatabase metho d. In
ADO, yo u simply spec ify the passwo rd as part o f the Connection string by using the
;pwd=MyPassword keywo rd.
Chapter 29 ✦ Overview of M icrosoft Jet
User-level security
User-level sec urity wo rks like the sec urity systems in SQL Server and Orac le8i. A
userID and passwo rd are c reated fo r eac h perso n who ac c esses the database. Then
the userID is granted permissio n to ac c ess the vario us o bjec ts in the database. Gro ups
c an be used with user-level sec urity to simplify administratio n. Yo u c an c reate a gro up
and assign permissio ns to it, just as if it was ano ther user. Then yo u c an assign gro ups
to vario us userIDs. When users lo g o n to the database, they will inherit all o f the permissio ns asso c iated with the gro up o r gro ups that have been assigned to them, plus
any individual permissio ns they may have been granted.
Finding the workgroup file
Sec urity info rmatio n is kept in a wo rkgro up file ( .MDW). Only o ne wo rkgro up file
is required per system, and the fo llo wing windo ws registry key will be used to
lo c ate it:
HKEY_LOCAL_SYSTEM\Software\Microsoft\Office\8.0\Access\Jet\3.5\
Engine\SystemDB
A typic al value fo r this key is:
C:\WINDOWS\SYSTEM\system.mdw
Of c o urse, if yo u are using a Jet 4.0 database with Ac c ess 2000 (Versio n 9.0), yo u
will need to adjust the registry key to inc lude the appro priate versio n numbers.
Note
Security with Access: In order to use security w ith a Jet database, you m ust have
installed Access 97 (or Access 2000 for a Jet 4.0 database). The file SYSTEM.MDW
w ill autom atically be installed.
SIDs, user ids, personal ids, and passwords
All info rmatio n inside a database file is sec ured o n the basis o f a Sec urity ID, o r SID.
A SID is derived by enc rypting the user id and a spec ial value kno wn as a perso nal
id. If yo u c reate a user id and do n’t bo ther to assign a perso nal id, it is po ssible fo r
so meo ne else to c reate ano ther wo rkgro up file with the same user id and their o wn
passwo rd and user id to gain ac c ess to database reso urc es fo r whic h they do no t
have permissio n.
The perso nal id is used to ensure that the SID is uniq ue and c an’t b e duplic ated,
so yo u sho uld always c ho o se a value that is hard to duplic ate. Simply c ho o sing
so meo ne’s first name as the user id and their last name fo r the perso nal id is no t
a go o d idea.
659
660
Part IX ✦ M icrosoft Jet
By default, the user id Admin exists in all database systems with the same perso nal
id. This means that Admin has the same SID no matter whic h c o mputer they use.
The same is true fo r the Users gro up. Ho wever, this is no t true fo r the Admins gro up.
The SID asso c iated with the Admins gro up is always unique fo r eac h wo rkgro up file.
Sinc e anyo ne in the Admins gro up has ac c ess to the entire database, the first thing
yo u want to do is c reate a new user id and assign it to the Admins gro up. Then yo u
sho uld remo ve the Admin user id fro m the Admins gro up to ensure that so meo ne
do esn’t use their o wn wo rkgro up file to o verride yo ur database’s sec urity.
Permissions, SIDs, and ownership
The wo rkgro up file exists simply to return a SID fo r a partic ular user and ano ther
SID fo r eac h gro up that the user is a member o f. This value is used as a key into
so me o f the system tables inside the database file to determine whic h permissio ns
the user has.
If the user has o verlapping permissio ns fro m multiple gro ups and/ o r explic it permissio n assignments, the least restric tive permissio n will apply. Thus, if a user has read
and write permissio n to the Custo mers table via the Clerks gro up and has been
explic itly granted read permissio n to the Custo mers table, the user will have
read/ write ac c ess to the Custo mers table.
Whenever an o b jec t is c reated in the datab ase, the user id o f the individual that
c reated the o b jec t is used as the o b jec t’s o wner. The o b jec t’s o wner always has
ac c ess to the o b jec t. Yo u c an and sho uld c hange the o wnership o f the o b jec ts in
yo ur datab ase to a user id o ther than Admin to prevent a sec urity pro b lem. I suggest that yo u use the Admins gro up as the o b jec t’s o wner, whic h means that anyo ne in the Admins gro up c an always ac c ess the o b jec t.
Thoughts on M icrosoft Jet
Microsoft Jet is a low -end database system designed prim arily for use w ith Microsoft
Access. Much of the design revolves around features that are im portant to Access users,
w hile features that m ight be useful to Visual Basic program m ers aren’t present. Jet is not
upw ards-com patible w ith SQL Server and m ost other databases, due to these lim itations.
Jet is also lim ited to a m axim um of 255 concurrent users. How ever, its true m axim um is far
less, depending on how frequently the database is accessed. While m any im provem ents
have been m ade to m ake Jet m ore efficient, it is still lim ited by its file-oriented approach to
synchronization for m ultiple users.
Just because Jet isn’t perfect doesn’t m ean you should avoid using it. The Jet database
engine is a standard part of the Visual Basic Professional Edition. This m eans that you don’t
have to purchase it separately. Also, there aren’t any licensing charges associated w ith Jet.
Chapter 29 ✦ Overview of M icrosoft Jet
This m akes it ideal for those applications that are cost-sensitive, such as sharew are, or part
of a large distributed application w here you w ant to cache data on the local com puter.
Jet has also been around for a long tim e, though the ability to access it using ADO hasn’t.
Until recently, DAO included the ability to create database objects through its object m odel,
w hile ADO could only create a database object by executing the appropriate SQL statem ent
using the Command object. Since Visual Basic 6 w as released, ADO has gone from version
2.0 to 2.1, and now to 2.5. The new er versions of ADO now include an independent object
library know n as ADOX, or ADO Extensions for Data Definition Language and Security. These
objects im prove on DAO’s ability to create database objects w hile using an OLE DB interface to the database. So if you need to build database objects directly from your code, you
no longer have to use DAO.
Summary
In this c hapter yo u learned:
✦ abo ut the vario us versio ns o f the Jet database system.
✦ that the to o ls in Visual Basic do no t suppo rt Jet 4.0.
✦ that yo u c an use DAO 3.6 o r ADO with the appro priate pro vider to write pro grams that wo rk with Jet 4.0.
✦ abo ut the arc hitec ture o f the Jet database.
✦ ho w to c o nnec t to a Jet database.
✦ abo ut the sec urity c apabilities fo r a Jet database.
✦
✦
✦
661