Directory listing of http: uap.unnes.ac.id ebook biblebook Visual Basic 6 com & Library Books

7

C H A P T E R

Codeless
Database
Programming









In This Cha pter
Ho w bo und
co ntro ls wo rk

I


n this c hapter, I’m go ing to sho w yo u ho w to c reate a wo rking Visual Basic database pro gram witho ut writing a single
line o f c o de. I’ll use the ADO Data Co ntro l and so me c o mmo n
c o ntro ls fo und in nearly every Visual Basic Pro gram.

Data Binding
One o f the mo st po werful c o nc epts in Visual Basic is the c o nc ept o f data binding. By using data binding, yo u c an delegate
many o f the details o f mo ving data between yo ur database
and yo ur pro gram.

What is data binding?
Data binding is a tec hnique that allo ws a data so urc e to be
tied to a data c o nsumer. Then when the data values asso c iated with the data so urc e c hange, the updated info rmatio n is
reflec ted in the data c o nsumer. Likewise, data values that are
updated in the data c o nsumer are passed bac k to the data
so urc e fo r updating.

In the c ase o f a Visual Basic pro gram, bo th the data so urc e and
data c o nsumer are typic ally Ac tiveX c o ntro ls, altho ugh o ther
types o f COM o bjec ts may be used (see Part III, Hardc o re ADO,

fo r mo re info rmatio n abo ut this subjec t). The c lassic data
so urc e in a Visual Basic pro gram is the data c o ntro l, while the
c lassic data c o nsumer is a text bo x c o ntro l.

The ADO
Data Co ntro l
Co mmo n Bo und
Co ntro ls
Co nnecting to
a database
Building a pro g ram
witho ut co de
Binding a text bo x to
a data co ntro l










106

Part II ✦ Beginning Database Programming

Note

One data control doesn’t fit all: There are three different data controls, one for
each object m odel (DAO, RDO, and ADO). The DAO control is sim ply know n as the
Data Control, w hile the RDO data control is also know n as the Rem oteData control. The ADO Data Control is also called the ADODC. You should pick the data
control corresponding to the object m odel you w ish to use and if you don’t have a
specific need to use the DAO or RDO object m odels, you should use the ADO Data
Control to take advantage of the enhancem ents and features in the ADO object
m odel.

How does data binding work?
Data b inding is a two -step pro c ess. First, yo u c reate a data so urc e and pro vide the
info rmatio n nec essary fo r it to c o nnec t to the datab ase server. Then yo u c reate

o ne o r mo re data c o nsumers that are in turn c o nnec ted to a spec ific datab ase
field returned b y the data so urc e. Sinc e the data c o nsumers are typic ally c o mmo n
c o ntro ls suc h as the text b o x c o ntro l, they are also kno wn as b o und c o ntro ls.
The binding info rmatio n is handled by setting vario us pro perty values in the bo und
c o ntro ls. Two pro perties in partic ular are very impo rtant when binding a c o ntro l to
a data so urc e. The DataSource pro perty identifies the name o f the data so urc e
c o ntro l, while the DataField pro perty identifies whic h database field will supply
the data fo r the bo und c o ntro l.
Then, when yo ur applic atio n begins, the data c o ntro l establishes a c o nnec tio n with
the database server and eac h o f the bo und c o ntro ls establishes a c o nnec tio n with
the data c o ntro l. Whenever the data c o ntro l mo ves to a new rec o rd, the info rmatio n
in eac h o f the bo und c o ntro ls c an be updated auto matic ally. Likewise, whenever
the user c hanges a value in a bo und c o ntro l, the info rmatio n is passed bac k to the
data c o ntro l, whic h in turn will auto matic ally update the database when the user
mo ves to a different rec o rd.

Connecting to the database
After binding the c o ntro ls the user will interac t with the data c o ntro l, whic h needs to
be c o nnec ted to the database. Again, this is handled via a set o f pro perties. The key
pro perty is the ConnectionString pro perty, whic h ho lds the info rmatio n needed to

c o nnec t to the database server. This is a String value that c o ntains fo ur main piec es
o f info rmatio n. The name o f the database server and the name o f the data pro vider
are used to c reate a vehic le that c an be used to link the pro gram to the database
server. Then the user name and passwo rd are used to authentic ate the user and
determine the user’s privileges in the database.
Onc e yo u have a valid ConnectionString, yo u need to spec ify the so urc e o f the
data. This is kept in the RecordSource pro perty. This value c an be the name o f a
database table, an SQL Select statement, o r a sto red pro c edure. Typic ally with a
data c o ntro l, yo u’ll want to spec ify a table name, sinc e it will make all o f the rec o rds

Chapter 7 ✦ Codeless Database Programming

in the table available to the user. Also , by spec ifying a table name, yo u wo n’t run
into any c o mplic atio ns with adding rec o rds to yo ur database.
CrossReference

See Chapter 4 for details about Select statem ents.

Intrinsic bound controls
Visual Basic inc ludes two main types o f c o ntro ls: intrinsic and Active X. Intrinsic c o ntro ls are inc luded with the Visual Basic runtime library and are always available to

the Visual Basic pro grammer. While these c o ntro ls are no t true COM o bjec ts, they
are muc h mo re effic ient and the mo st frequently used. Many o f these c o ntro ls c an
be bo und to a data c o ntro l, inc luding:

✦ CheckBox

✦ ListBox

✦ ComboBox

✦ PictureBox

✦ Image

✦ TextBox

✦ Label
Of these c o ntro ls, pro bably the o ne yo u’ll use mo st o ften is the TextBox c o ntro l,
sinc e this c o ntro l makes it easy to display a database value to a user and allo ws
them to mo dify it. Other c o ntro ls yo u might find yo urself using are an Image o r

Picture c o ntro l when yo u want to display a pic ture o n yo ur fo rm and the CheckBox
c o ntro l when yo u want to display a Boolean value fro m yo ur database. While yo u
might think that the ComboBox c o ntro l might also be heavily used, there is a mo re
database-friendly Ac tiveX c o ntro l c alled the DataCombo c o ntro l that yo u will find
yo urself using in plac e o f the ComboBox c o ntro l in mo st applic atio ns.

ActiveX bound controls
Unlike the intrinsic bo und c o ntro l, the Ac tiveX bo und c o ntro ls are true COM o bjec ts
and are external to the Visual Basic runtime libraries. Also , unlike the intrinsic bo und
c o ntro ls, the Ac tiveX c o ntro ls are mo re c o mplex and have the ability to wo rk with
mo re than o ne database field at a time.

✦ DataList

✦ MaskEdit

✦ DataCombo

✦ MonthView


✦ DataGrid

✦ MSFlexGrid

✦ DateTimePicker

✦ MSChart

✦ ImageCombo

✦ RichTextBox

Of these c o ntro ls, pro bably the mo st useful are the DataCombo, the DateTimePicker,
the MaskEdit, and the MonthView c o ntro l. All o f these c o ntro ls have o ne c o mmo n
feature: they make it harder fo r a user to enter an inc o rrec t value into the pro gram.

107

108


Part II ✦ Beginning Database Programming

Keep the Garbage Out
You’re probably fam iliar w ith the old expression “Garbage in, garbage out”. One of the m ost
im portant goals of a database program m er is to prevent bad data from getting into the
database. When you get bad data in your database, you m ay find it hard to isolate and correct. I rem em ber a situation w here one slightly corrupted field in a database prevented a
financial application from closing the books at the end of a fiscal year. It took nearly a w eek
to track dow n and correct the bad piece of data. In the m eantim e, none of the other program s in the application w ould run correctly, and other processing involving the general
ledger cam e to a com plete halt. In a sm all business this m ay not be a big problem , but in a
billion dollar a year organization, you can believe the top-level m anagem ent w asn’t very
happy. So think about it this w ay, keeping bad data out of your database is a good w ay of
keeping yourself happily em ployed.

Building the Codeless Program
Building a c o deless pro gram is an interesting exerc ise and o ne that is pro bably
wo rth yo ur time, espec ially if yo u are relatively new to Visual Basic . Bo und c o ntro ls
will make yo ur life muc h easier, espec ially in mo re c o mplex applic atio ns. They definitely reduc e the amo unt o f c o de yo u have to write in yo ur applic atio n and anything
that reduc es the amo unt o f c o de yo u have to write appeals to the lazy pro grammer
in everyo ne. Fo r the rest o f this c hapter, I’m go ing to sho w yo u ho w to build a c o deless pro gram that allo ws yo u to ac c ess the Custo mers table in the database I
designed in Chapter 3.

On the
CD-ROM

It’s your turn: This program is available on the CD-ROM as \VB6DB\
Chapter07\NoCode\Project1.vbp. To run this program , sim ply create a
Data Link File as described in Configuring the ADO Data Control below for your
ADODC1 control that reflects your database server, database nam e, user nam e,
and passw ord inform ation. Then run the program .

Preparing your project
When yo u start Visual Basic , yo u have a number o f different pro jec t templates yo u
c an c ho o se fro m. These templates allo w yo u to build many different types o f Visual
Basic pro grams. Fo r this pro gram, I’m go ing to take the Standard EXE pro jec t and
add everything I need to build the pro gram (see Figure 7-1).
The next step is to add the ADO Data Co ntro l to the pro jec t. This is do ne using the
Co mpo nents dialo g bo x. To o pen this dialo g bo x, c ho o se Pro jec t ➪ Co mpo nents
fro m the main menu. Then selec t the Co ntro ls tab if it isn’t already selec ted and
sc ro ll do wn the list o f c o ntro ls until yo u find the Mic ro so ft ADO Data Co ntro l (see
Figure 7-2). Clic k o n the c hec kbo x and press OK to add the c o ntro l to yo ur To o lbo x.


Chapter 7 ✦ Codeless Database Programming

Figure 7-1: Selecting a
Standard EXE project

Note

The service pack game: Figure 7-2 reflects the nam es of the controls that w ere
updated using Visual Studio Service Pack 3. This is identified by ( SP3) in the nam e
of the control.

Figure 7-2: Adding the ADO
Data Control to the project

Configuring the ADO Data Control
Onc e yo u add the ADO Data Co ntro l to yo ur pro jec t, yo u need to plac e a c o py o f
the data c o ntro l o n yo ur fo rm and set the pro perties so that it will ac c ess yo ur
database. Yo u c an either set the pro perties tho ugh the Visual Basic Pro perties
Windo w, o r yo u c an right c lic k o n the c o ntro l and selec t ADODC Pro perties fro m
the po pup menu. This will display a set o f Pro perty Pages fo r the c o ntro l (see
Figure 7-3).

109

110

Part II ✦ Beginning Database Programming

Figure 7-3: Setting the data
control’s properties

On the General tab , yo u’ll see that there are three different ways to c o nnec t to a
datab ase server. Yo u c an use a Data Link File, an ODBC Data So urc e Name, o r a
Co nnec tio n string. I’m go ing to sho w yo u ho w to b uild a Data Link File in this
c hapter and yo u c an see ho w to c reate a Co nnec tio n String in Chapter 9.
Note

OLE DB, not ODBC: The ADO Data Control is based on the OLE DB architecture,
w hich is m ore efficient and offers a m ore flexible architecture. While you can create an ODBC Data Source, you w ould be m uch better off using a Data Link File or
creating a Connection string.

Selecting a Data Link File
After selec ting Use Data Link File as the So urc e o f Co nnec tio n, press the Bro wse butto n to either selec t an existing Data Link File o r c reate a new o ne. The Selec t Data
Link File dialo g bo x (see Figure 7-4) will be displayed with the default direc to ry fo r
data link files ( \Program Files\Common Files\System\OLE DB\Data Links\).
If yo u have an existing Data Link File, simply selec t it and press Open. If no t, yo u c an
c reate o ne by right c lic king in the file area o f the dialo g bo x and c ho o sing New ➪
Mic ro so ft Data Link. A new file c alled New Mic ro so ft Data Link will be c reated.

Figure 7-4: Choosing a Data Link File

Chapter 7 ✦ Codeless Database Programming

Tip

Name of the game: I like to include the database server and the default database
nam e in the nam e of m y Data Link files. This m akes it easy to identify the connection inform ation.

Choosing an OLE DB provider
Yo u c an edit the pro perties in a Data Link File by right c lic king o n its ic o n and
selec ting Pro perties fro m the po pup menu. This will display the Pro perties dialo g
bo x fo r the data link file. Selec t the Pro vider tab o f the Pro perties dialo g bo x to
begin c o nfiguring yo ur data link (see Figure 7-5).

Figure 7-5: View ing the properties of
a Data Link File

Note

Define me before you use me: If you just created this file, you m ust edit the
properties before it can be used.

There are a number o f c ho ic es fo r the data pro vider. Fo r best perfo rmanc e, yo u
sho uld always c ho o se the OLE DB pro vider fo r yo ur database system. If yo u c an’t
lo c ate o ne fo r yo ur spec ific database management system, then c ho o se Mic ro so ft
OLE DB Pro vider fo r ODBC Drivers. Onc e yo u’ve c ho sen yo ur pro vider, press the
Next butto n.
Tip

It isn’t there: Microsoft only supplies OLE DB providers for SQL Server and Oracle.
If you are using a different database system , contact your database vendor to get
their OLE DB provider.

111

112

Part II ✦ Beginning Database Programming

Entering provider-specific information
Eac h OLE DB pro vider has a list o f info rmatio n it needs in o rder to c o nnec t to a
database server. Figure 7-6 sho ws the info rmatio n required fo r the Mic ro so ft OLE
DB Pro vider fo r SQL Server.

Figure 7-6: Selecting the database
server and default database

There are three basic piec es o f info rmatio n needed in this fo rm: the name o f the
database server, lo gin info rmatio n, and the name o f the default database. In this
example, I’m using Athena as my database server, Windo ws NT Authentic atio n fo r
my lo gin info rmatio n and VB6DB as the default database.

Testing the connection
Onc e yo u’ve finished entering the pro perties, press the Test Co nnec tio n butto n to
verify that yo u c an c o nnec t to the database server. If the info rmatio n yo u spec ify
is c o rrec t, yo u sho uld see a message bo x saying, “Test c o nnec tio n suc c eeded”. If
there is a pro blem, yo u will see an erro r message desc ribing the pro blem. Yo u
sho uld then c o rrec t the info rmatio n yo u pro vided and try it again.
After yo u are able to test the c o nnec tio n suc c essfully, yo u sho uld press the OK butto n to c lo se the Pro perties dialo g bo x, and then press the Open butto n o n the Selec t
Data Link File dialo g bo x to c ho o se the data link file. This will return yo u to the data
c o ntro l’s Pro perties windo w.

Chapter 7 ✦ Codeless Database Programming

Choosing a RecordSource
The last step o f c o nfiguring the data c o ntro l is to c ho o se a so urc e o f data. This
info rmatio n is sto red in the RecordSource pro perty. Yo u c an edit this pro perty o n
the Rec o rdSo urc e tab o f the Pro perties windo w. In this c ase, sinc e yo u want to
make the Custo mers available via this pro gram, yo u sho uld c ho o se a CommandType
o f adCmdTable and then selec t the Custo mers table fro m the dro p-do wn list fo und
immediately belo w that field (see Figure 7-7). This will auto matic ally be entered into
the RecordSource pro perty. Then I c an press OK to c lo se the Pro perty Pages dialo g bo x and save these values in the data c o ntro l.

Figure 7-7: Selecting the Custom ers
table as the RecordSource

Adding bound controls
After adding and c o nfiguring the ADO Data Co ntro l, it’s time to add so me bo und
c o ntro ls. I’m go ing to start by adding a text bo x fo r the Name field and binding it to
the data c o ntro l (see Figure 7-8).
Drag a text bo x o nto yo ur fo rm, and size it to ho ld a perso n’s full name. Then view the
Pro perties windo w and sc ro ll it so that the DataSource and DataField pro perties
are bo th visible. Selec t the DataSource pro perty. A dro p-do wn arro w will appear at
the end o f the pro perty’s value field. Press it and selec t the data c o ntro l fro m the
dro p-do wn list. It sho uld be the o nly item o n the list.
Next, selec t the DataField pro perty and press the dro p-do wn arro w. A list o f all o f
the fields in the table will be listed. Selec t the Name field to bind the text bo x to the
Name c o lumn in the Custo mers table.

113

114

Part II ✦ Beginning Database Programming

Figure 7-8: Adding the text box for
the Nam e field to your form

Tip

The Database Connection: You m ay not have given this m uch thought at this
point, but the Visual Basic developm ent environm ent autom atically uses the connection inform ation you specified in the ADO Data Control to gather inform ation
about your database at developm ent tim e. It then uses this inform ation to help
you avoid m istakes w hen you enter values into the various properties. Of course,
you can enter all of the inform ation for the bound controls m anually and then configure the data control, but that isn’t the lazy program m er’s w ay of doing things.

Testing your program
Even tho ugh this pro gram c an o nly display the perso n’s name fro m the Custo mers
table, it is wo rth running it to verify that the pro gram ac tually wo rks. After c ho o sing Run ➪ Start fro m the main menu, the pro gram will lo ad, establish a c o nnec tio n
to the database, retrieve the first ro w fro m the Custo mers table, and display the
value fro m the Name field in the text bo x (see Figure 7-9).
Yo u c an press the Next Ro w butto n o n the data c o ntro l to display the name fro m the
sec o nd ro w in the table and yo u c an return to the first ro w by pressing the Previo us
Ro w butto n. Pressing the Last Ro w butto n will take yo u to the last ro w o f the table,
while pressing the First Ro w butto n will return yo u to the first ro w in the table.

Finishing your program
Of c o urse, a pro gram that lists o ne field fro m a database table is a signific ant
ac c o mplishment in terms o f all the little steps nec essary to make it wo rk, but it isn’t
a terribly useful pro gram. So at this po int, yo u need to add c o ntro ls fo r the rest o f
the fields in the table. Also , yo u sho uld take the time to plac e labels beside eac h
field so that the user will understand the info rmatio n displayed. When yo u’ve finished, yo ur fo rm might lo o k like the o ne sho wn in Figure 7-10.

Chapter 7 ✦ Codeless Database Programming

Previous row
First row

Last row
Next row

Figure 7-9: Running your codeless program for the
first tim e

Figure 7-10: Finishing your codeless program

Updating database information
To c hange a value in the database using this pro gram, simply c hange the value in
the field and mo ve to ano ther ro w. As yo u c hange the data displayed o n the fo rm,
the bo und c o ntro ls pass the c hanges bac k to the data c o ntro l, whic h will auto matic ally update the ro w when it mo ves to ano ther ro w. Of c o urse, if there is an erro r

115

116

Part II ✦ Beginning Database Programming

with the update pro c ess, a message bo x will appear and yo u will remain o n the c urrent ro w until yo u c o rrec t the pro blem.

Using numeric fields
Yo u may rememb er that the Zip field sto res its info rmatio n using a 32-b it integer
rather than a c harac ter string. While yo u might think that so me spec ial c o de
might b e nec essary to handle the data c o nversio n fro m Long to String and b ac k
again, yo u wo uld b e wro ng. The c o nversio n is handled auto matic ally b y the text
b o x c o ntro l.
One limitatio n o f using text bo xes to display numeric info rmatio n is that yo u c an
enter no n-numeric info rmatio n into the text bo x witho ut immediately triggering an
erro r. Of c o urse, attempting to update the ro w will trigger a runtime erro r bec ause
yo u c an’t put a no n-numeric value into a numeric field.

Using Boolean fields
Boolean fields are a natural fit fo r the CheckBox c o ntro l. Yo u set the DataSource
and DataField pro perties just like the TextBox c o ntro l. The c o ntro l will display a
TRUE value by plac ing a c hec k mark in the c hec k bo x and will c lear the c hec k bo x
when the database value is FALSE.

Using Datetime fields
Like numeric values, date and time values also undergo a dynamic c o nversio n pro c ess when info rmatio n is displayed in the c o ntro l. As lo ng as the date and time
info rmatio n is pro perly fo rmatted and legal, the c o ntro l will auto matic ally handle
the c o nversio n. If there is an erro r, the o ld Operatio n c anc eled message will be displayed, and the o riginal values will be resto red.

Adding new records
By default, the ADO Data Co ntro l do esn’t allo w yo u to add new rec o rds to the
database. Ho wever, by c hanging o ne pro perty value, yo u c an easily inc lude this
c apability in yo ur c o deless pro gram. The EOFAction pro perty determines what
happens when the user attempts to mo ve beyo nd the last ro w in the table.
By default, the data c o ntro l will simply mo ve the user bac k to the last ro w in the
table if the user attempts to mo ve beyo nd it. Ho wever, if yo u set EOFAction to
adDoAddNew (2), the data c o ntro l will auto matic ally add a new blank ro w anytime
the user attempts to mo ve beyo nd the end o f the file.
Onc e the user adds a rec o rd and enters the pro per values into eac h o f the fields,
mo ving to ano ther ro w will save the new ro w to the database. Of c o urse, when yo u
add a rec o rd using this tec hnique, the final data must meet the database rules fo r
ac c eptability. Any invalid values must be c o rrec ted befo re yo u c an mo ve to
ano ther ro w.

Chapter 7 ✦ Codeless Database Programming

Thoughts on Codeless Programming
Okay, so a codeless program m ay not be perfect, but it w ill w ork and can often be useful in
the early stages of building an application. If you’re w illing to add a little code to handle
non-database functions, such as edit checking and m enu m anagem ent, Visual Basic offers
a m uch better alternative to building a database application in Access.
Access is prim arily an easy to use database form s generator w ith som e scripting capabilities
added. While it allow s you to build sim ple database applications quickly, w hen building
m ore com plex applications you constantly run into situations that aren’t easy to handle
because of the m any lim itations in Access. How ever, Visual Basic is a true program m ing language and doesn’t suffer from the sam e lim itations.
The prim ary draw back to using a true program m ing language like Visual Basic is that it can
be difficult to create program s that perform a com plex task like accessing a database.
How ever, by using bound controls and other tools in Visual Basic, you can easily create
database program s that require very little code and that offer m ore pow er and flexibility
than their Access counterparts.

Caution

I’ve added a record and I can’t get out: One dow nside to using a codeless program to add records to your database is that there isn’t a convenient m ethod to
abort the add process. In fact, the only w ay to abort the add process is to end the
program , w hich is pretty drastic. Of course, this is a situation w here a little code
can go a long w ay tow ards m aking the program m uch easier to use.

Summary
In this c hapter yo u learned the fo llo wing:

✦ What data binding is and ho w it wo rks.
✦ Whic h c o ntro ls yo u c an bind in yo ur Visual Basic pro gram.
✦ Ho w yo u c an use bo und c o ntro ls to build a meaningful pro gram witho ut
any c o de.

✦ Ho w to c ho o se an OLE DB pro vider and build an ADO c o nnec tio n string to
c o nnec t to yo ur database.







117