You are on page 1of 9

Chng Mi Bn - Dng Control Data

Control Data
T VB5, Visual Basic cho lp trnh vin mt control truy cp c s d liu, tn n ch n s l Data. Nh ta bit, c mt c s d liu Microsoft gi km khi ta mua VB6 - l Jet Database Engine. Jet Database Engine l ci "phng my" ca chnh MS Access Database Management System. Cho n thi VB5, Microsoft cho ta ba k thut chnh:

DAO (Data Acess Objects): DAO l k thut b truyn ca Microsoft, ch dng vi Jet Database Engine. N rt d dng, hiu nng v tin, nhng b gii hn trong phm vi MS Access. Du vy, n rt thnh hnh v c li ch thc tin. ODBC (Open Database Connectivity): ODBC c thit k cho php users ni vi loi databases m ch dng mt method duy nht. iu ny ct bt gnh nng cho lp trnh vin, ch cn hc mt k thut lp trnh duy nht m c th lm vic vi bt c loi database no. Nht l khi sau ny nu cn phi thay i loi database, nh nng cp t Access ln SQLServer chng hn, th s sa i v coding rt t. Khi dng ODBC chung vi DAO, ta c th cho Access Database ni vi cc databases khc. C mt bt li ca ODBC l n rc ri. RDO (Remote Data Object): Mt trong nhng l do chnh RDO c thit k l gii quyt kh khn v s rc ri ca ODBC. Cch lp trnh vi RDO n gin nh DAO, nhng tht ra n dng ODBC nn cho php users ni vi nhiu databases. Tuy nhin, RDO khng c thnh hnh lm.

VB6 tip tc h tr cc k thut ni trn, v cho thm mt k thut truy cp database mi, rt quan trng, l ADO (ActiveX Data Objects). Trong mt bi ti ta s hc v ADO vi nhng u im ca n. Tuy nhin, v DAO rt n gin v hiu nng nn ta vn c th tip tc dng n rt hu hiu trong hu ht cc p dng. Do bi ny v bi k s tp trung vo nhng k thut lp trnh ph bin vi DAO. Cch dng gin tin ca control Data l t n ln mt Form ri lm vic vi nhng Properties ca n. Bn hy bt u mt d n VB6 mi, cho n tn DataControl bng cch click tn project trong Project Explorer bn phi ri edit property Name trong Properties Window. DoubleClick ln Icon ca Control Data trong Toolbox. Mt Control Data tn Data1 s hin ra trn Form. Mun cho n nm bn di Form, ging nh mt StatusBar, hy set property Align ca n trong Properties Window thnh 2 - Align Bottom. Click bn phi hng property DatabaseName, k click ln nt browse c ba chm chn mt file Access dabase t giao thoi cho Data1. y ta chn E:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB , trong computer ca bn c th n nm trn disk C hay D.

Trong chng trnh ny ta mun lm vic vi table Titles ca database BIBLIO.MDB, xem v edit cc records. property DefaultType ca Data1 c tr s 2- UseJet, tc l dng k thut DAO, thay v dng k thut ODBC. Khi bn click ln property Recordsource ca Data1, ri click ln ci tam gic nh bn phi, mt ComboBox s m ra cho ta thy danh sch cc tables trong database. Bn hy chn Titles. property RecordsetType ca Data1 c tr s l 0 - Table:

Ci t mi m ta s dng thng xuyn khi truy cp d liu trong VB6 l Recordset (b records). Recordset l mt Set of records, n c th cha mt s records hay khng c record no c. Mt record trong Recordset c th l mt record ly t mt Table. Trong trng hp y c th ta ly v tt c records trong table hay ch nhng records tha ng mt iu kin, th d nh ta ch mun ly cc records ca nhng sch xut bn trc nm 1990 (Year Published < 1990). Mt Record trong Recordset cng c th l tp hp cc ct (columns) t hai (hay ba) tables qua cc mi lin h one-to-one v one-to-many. Th d nh khi ly cc records t

table Titles, ta mun c thm chi tit tn cng ty (Company Name) v in thoi (Telephone) ca nh xut bn (table Publishers) bng cch dng Foreign Key PubID trong table Titles lm Primary Key trong table Publishers ly cc chi tit y. Nu bn cha nm vng nim Foreign Key th hy c li bi Database. Trong trng hp y ta c th xem nh c mt virtual (o) table l tp hp ca hai tables Titles v Publishers. By gi bn hy t ln Form 4 labels vi captions: Title, Year Published, ISBN v Publisher ID. K cho thm 4 textboxes tng ng v t tn chng l txtTitle, txtYearPublished, txtISBN v txtPublisherID. Chn textbox txtTitle, ri set property Datasource ca n trong Properties Window thnh Data1. Khi click ln property Datafield ca txtTitle v m ComboBox ra bn s thy lit k tn cc Fields trong table Titles. l v Data1 c coi nh trung gian ly table Titles t database. y ta s chn ct Title. Lp li cng tc ny cho 3 textboxes kia, v chn cc ct Year Published (nm xut bn), ISBN (s l lch trong th vin quc t), v PubID (s l lch nh xut bn) lm Datafield cho chng.

Ti y, mc du cha vit mt hng code, ta c th chy chng trnh c ri. N s hin th chi tit ca record u tin trong table Titles nh di y:

Bn c th bm cc nt di chuyn Navigator Buttons i n cc record u (first), trc (previous), k (next) v cui (last). Mi ln bn di chuyn n mt record mi l chi tit ca record y s hin th. Nu khng dng cc Navigator Buttons, ta cng c th code lm cng tc tng ong bng cch gi cc Recordset methods MoveFirst, MovePrevious, MoveNext v MoveLast. Khi record cui ca Recordset ang hin th, nu ta gi method MoveLast th property EOF (End-Of-File) ca Recordset tr thnh True. Tng t nh vy, khi record th nht ca Recordset ang hin th, nu ta gi method MovePrevious th property BOF (Begin-Of-File) ca Recordset tr thnh True. Nu mt Recordset khng c cha mt record no c th c hai properties EOF v BOF u l True. c tnh hin th d liu trong cc textboxex theo ng record hin thi (current record) c gi l data binding hay data bound (buc vo d liu) v control TextBox h tr chc nng ny c ni l Data Aware (bit b con d liu). Khi record u tin ang hin th, nu bn edit Year Published i t 1985 thnh 1983 ri click Navigator button Next hin th record th nh, k click Navigator button Previous hin th li record u tin th bn s thy l field Year Published ca record u tin tht s c thay i (updated) thnh 1983. iu ny c ngha rng khi Data1 navigates t record ny n record khc th nu record ny c s thay i v user edited, n lu tr s thay i trc khi di chuyn. Cha chc l bn mun iu ny, do , nu bn khng mun user tnh c edit mt record th bn c th set property Locked ca cc textboxes y thnh True user khng th edit cc textboxes nh trong hnh di y:

Ch nh v tr Database lc chy chng trnh


Cch ch nh tn DatabaseName trong giai on thit k (at design time) ta dng trc y tuy tin li nhng hi nguy him, v khi ta ci chng trnh ny ln computer

ca khch, cha chc file database y nm trong mt folder c cng tn. Th d trn computer mnh th database nm trong folder E:\Program Files\Microsoft Visual Studio\VB98, nhng trn computer ca khch th database nm trong folder C:\VB6\DataControl chng hn. Do , khi chng trnh khi ng ta nn xc nh li v tr ca database. Gi d ta mun database trong cng mt folder vi chng trnh ang chy, ta c th dng property Path ca Application Object App nh sau:
Dim AppFolder As String Private Sub Form_Load() ' Fetch Folder where this program EXE resides AppFolder = App.Path ' make sure it ends with a back slash If Right(AppFolder, 1) <> "\" Then AppFolder = AppFolder & "\" ' Assign Full path database filename to Data1 Data1.DatabaseName = AppFolder & "BIBLIO.MDB" End Sub

Vi cch code ni trn ta s m bo chng trnh tm thy file database ng ch, khng cn bit ngi ta ci chng trnh bn u trong hard disk ca computer khch. Nu bn ang hc VB6 t xa, khi np bi database cho gim th VB6 m bn hardcode (vit cht cng) v tr ca file database trong lc thit k th gim th (tutor) cng gp cng s kh khn ny v cha chc gim th s cha database trong mt folder c cng tn nh trong harddisk ca bn.

Thm bt cc Records
Chng trnh trn dng cng tm c, nhng n khng cho ta phng tin thm (add), bt (delete) cc records. By gi bn hy vo Form 5 buttons tn: cmdEdit, cmdNew, cmdDelete, cmdUpdate v cmdCancel. Mc du bn khng thy, nhng tht ra Control Data Data1 c mt property Recordset v khi ta dng Navigator buttons l di chuyn t record ny n record khc trong Recordset y. Ta c th ni n n bng Notation (cch vit) Data1.Recordset, v mi ln mun ly Recordset mi nht t database ta dng method Refresh nh Data1.Recordset.Refresh. Lc chung trnh mi khi ng, user ang xem (browsing) cc records th hai buttons Update v Cancel khng cn phi lm vic. Do ta s nhn tin Lock (kha) cc textboxes v disable (lm cho bt lc) hai buttons ny v khng cn dng chng. Trong Sub SetControls di y, ta dng mt parameter gi l Editing vi tr s False hay True ty theo user ang Browse hay Edit, ta gi l Browse mode v Edit mode. Trong Edit mode, cc Textboxes c unlocked (m kha) v cc nt cmdNew, cmdDelete v cmdEdit tr nn bt lc:
Sub SetControls(ByVal Editing As Boolean) ' Lock/Unlock textboxes txtTitle.Locked = Not Editing txtYearPublished.Locked = Not Editing txtISBN.Locked = Not Editing txtPublisherID.Locked = Not Editing

' Enable/Disable buttons CmdUpdate.Enabled = Editing CmdCancel.Enabled = Editing CmdDelete.Enabled = Not Editing cmdNew.Enabled = Not Editing CmdEdit.Enabled = Not Editing End Sub

Trong Browse mode, Form c dng nh sau:

Sub SetControls c gi trong Sub Form_Load khi chng trnh khi ng v trong Sub CmdEdit khi user click nt Edit nh sau:
Private Sub Form_Load() ' Fetch Folder where this program EXE resides AppFolder = App.Path ' make sure it ends with a back slash If Right(AppFolder, 1) <> "\" Then AppFolder = AppFolder & "\" ' Assign Full path database filename to Data1 Data1.DatabaseName = AppFolder & "BIBLIO.MDB" ' Place controls in Browse Mode SetControls (False) End Sub Private Sub CmdEdit_Click() ' Place controls in Edit Mode SetControls (True) End Sub

Khi ta Delete mt record trong recordset, v tr ca record hin ti (current record) vn khng thay i. Do , sau khi delete mt record ta phi MoveNext. Kh ni, nu ta va delete record cui ca Recordset th sau khi MoveNext, property EOF ca Recordset s thnh True. Thnh ra ta phi kim tra iu , nu ng vy th li phi MoveLast hin th record cui ca Recordset nh trong code ca Sub cmdDelete_Click di y:
Private Sub CmdDelete_Click() On Error GoTo DeleteErr With Data1.Recordset ' Delete new record

.Delete ' Move to next record .MoveNext If .EOF Then .MoveLast Exit Sub End With DeleteErr: MsgBox Err.Description Exit Sub End Sub

Trong lc code, ta Update (cp nht ha) mt record trong Recordset bng method Update. Nhng ta ch c th gi method Update ca mt Recordset khi Recordset ang trong Edit hay AddNew mode. Ta t mt Recordset vo Edit mode bng cch gi method Edit ca Recordset, th d nh Data1.Recordset.Edit. Tng t nh vy, ta t mt Recordset vo AddNew mode bng cch gi method AddNew ca Recordset, th d nh Data1.Recordset.AddNew.
Private Sub cmdNew_Click() ' Place Recordset into Recordset AddNew mode Data1.Recordset.AddNew ' Place controls in Edit Mode SetControls (True) End Sub

Sau khi Recordset gi method Update th Recordset y ra khi AddNew hay Edit modes. Ta cng c th t thot ra khi AddNew hay Edit modes, hay ni cho ng hn l hy b mi pending (ang ch i) Update bng cch gi method CancelUpdate, th d nh Data1.Recordset.CancelUpdate.
Dng DataBound Combo
Trong chng trnh hin ti ta ch hin th l lch nh xut bn (PubID) ca Title, ch khng c thm chi tit. Phi chi mc du chng trnh lu tr PubID, nhng hin th c Company Name ca nh xut bn cho ta lm vic khi phi nh cc con s th hay qu. Ta c th thc hin iu bng cch dng Control DBCombo (Data Bound Combo). Bn hy dng IDE Menu Command Project | Components... chn Microsoft Data Bound List Controls 6.0 ri click Apply.

K , thm mt DBCombo tn DBCombo1 vo Form. V ta cn mt Recordset khc cung cp Table Publisher cho DBCombo1, nn bn hy thm mt control Data th nh tn Data2 vo Form. Cho Data2, hy set property DatabaseName thnh E:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB v property RecordSource thnh Publishers. khng cho ngi ta thy hnh Data2 lc run-time, bn hy set property Visible n thnh False.

Ci mc ch ca chng ta khi dng DBCombo1 l hin th Company Name ca nh xut bn, nhng ng sau lng th khng c g thay i, tc l ta vn lm vic vi PubID cho cc record Title ca Data1. Khi user click ln DBCombo1 chn mt nh xut bn, th ta theo Company Name m cha PubID tng ng trong record Title ca Data1. Do c nhiu th ta phi sp t cho DBCombo1 nh sau:
Property Value Ch thch

RowSource

Data2

y l datasource ca chnh DBCombo1. N cung cp table Publishers. Khi RowSource pha trn c chn ri, Combo ca property Listfield ny s hin th cc fields ca table Publishers. Company Name l field ca RowSource m ta mun hin th trn DBCombo1. y l datasource ca record m ta mun. edit, tc l record ca table Titles Field (ca record Title) s c thay i. Field trong RowSource (table Publishers) tng ng vi item user chn trong DBCombo1 (Company Name).

Listfield

Company Name

DataSource Datafield BoundColumn

Data1 PubID PubID

Khi trong Edit mode user chn mt Company Name khc trong DBCombo1 ri click nt Update bn s thy Textbox txtPublisherID cng i theo v hin th con s l lch PubID mi. Nu trc khi Update bn mun thy PubID mi hin th trong Textbox txtPublisherID th bn c th dng Event Click ca DBCombo1 nh sau:
Private Sub DBCombo1_Click(Area As Integer) ' Display new PuBID txtPublisherID.Text = DBCombo1.BoundText End Sub

Property BoundText ca DBCombo1 l tr s ca BoundColumn m ta c th truy cp (vit hay c) c. Th d nh bn mun mi khi thm mt record Title mi th default PubID l 324, tc l Company Name= "GLOBAL ENGINEERING". Bn c th assign tr s 324 vo property BoundText ca DBCombo1 trong Sub cmdNew_Click nh sau:
Private Sub cmdNew_Click() ' Place Recordset into Recordset AddNew mode Data1.Recordset.AddNew ' Default Publisher is "GLOBAL ENGINEERING", i.e. PubID=324 DBCombo1.BoundText = 324 ' Place controls in Edit Mode SetControls (True) End Sub

Trong bi ti ta s hc thm v cch coding dng Recordset trong k thut DAO.

You might also like