In Lesson 22 and Lesson 23, we have learned how to build VB database applications using data control. However, data control is not a very flexible tool as it could only work with limited kinds of data and must work strictly in the Visual Basic environment.
To overcome these limitations, we can use a much more powerful data control in Visual Basic, known as ADO control. ADO stands for ActiveX data objects. As ADO is ActiveX-based, it can work in different platforms and different programming languages. Besides, it can access many different kinds of data such as data displayed in the Internet browsers, email text and even graphics other than the usual relational and non-relational database information.To be able to use ADO data control, you need to insert it into the toolbox. To do this, simply press Ctrl+T to open the components dialog box and select Microsoft ActiveX Data Control 6. After this, you can proceed to build your ADO-based VB database applications.
The following example will illustrate how to build a relatively powerful database application using ADO data control. It is, in fact, a library application. First of all, name the new form as frmBookTitle and change its caption to Book Titles- ADO Application. Secondly, insert the ADO data control and name it as adoBooks and change its caption to book. Next, insert the necessary labels, text boxes and command buttons. The runtime interface of this program is shown in Figure 25.1 below, it allows adding and deletion as well as updating and browsing of data.
The property settings of all the controls are listed as in Table 25.1 below:
|Book Titles -ADOApplication
|Label 2 Name
|Command Button1 Name
|Command Button1 Caption
|Command Button2 Name
|Command Button2 Caption
|Command Button3 Name
|Command Button3 Caption
|Command Button4 Name
|Command Button4 Caption
|Command Button5 Name
|Command Button5 Caption
|Command Button6 Name
|Command Button6 Caption
|Command Button7 Name
|Command Button7 Caption
To be able to access and manage a database, you need to connect the ADO data control to a database file. We are going to use BIBLIO.MDB that comes with VB6. To connect ADO to this database file , follow the steps below:
a) Click on the ADO control on the form and open up the properties window.
b) Click on the ConnectionString property, the Property Pages dialog box will appear, as shown in Figure 25.2.
When the dialog box appear, select the Use Connection String's Option. Next, click build and at the Data Link dialog box, double-Click the option labeled Microsoft Jet 3.51 OLE DB provider.
After that, click the Next button to select the file BIBLO.MDB. You can click on Text Connection to ensure proper connection of the database file. Click OK to finish the connection.
Finally, click on the RecordSource property and set the command type to adCmd Table and Table name to Titles. Now you are ready to use the database file.
Now, you need to write code for all the command buttons. After which, you can make the ADO control invisible.
For the Save button, the program codes are as follow:
Private Sub cmdSave_Click() adoBooks.Recordset.Fields("Title") = txtTitle.Text adoBooks.Recordset.Fields("Year Published") = txtPub.Text adoBooks.Recordset.Fields("ISBN") = txtISBN.Text adoBooks.Recordset.Fields("PubID") = txtPubID.Text adoBooks.Recordset.Fields("Subject") = txtSubject.Text adoBooks.Recordset.Update End Sub
For the Add button, the program codes are as follow:
Private Sub cmdAdd_Click() adoBooks.Recordset.AddNew End Sub
For the Delete button, the program codes are as follow:
Private Sub cmdDelete_Click() Confirm = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation") If Confirm = vbYes Then adoBooks.Recordset.Delete MsgBox"Record Deleted!", , "Message" Else
MsgBox "Record Not Deleted!", ,"Message" End If End Sub
For the Cancel button, the program codes are as follow:
Private Sub cmdCancel_Click() txtTitle.Text = "" txtPub.Text = "" txtPubID.Text ="" txtISBN.Text = "" txtSubject.Text = "" End Sub
For the Previous (<) button, the program codes are
Private Sub cmdPrev_Click() If Not adoBooks.Recordset.BOF Then adoBooks.Recordset.MovePrevious If adoBooks.Recordset.BOF Then adoBooks.Recordset.MoveNext End If End If End Sub
For the Next(>) button, the code is
Private Sub cmdNext_Click() If Not adoBooks.Recordset.EOF Then adoBooks.Recordset.MoveNext If adoBooks.Recordset.EOF Then adoBooks.Recordset.MovePrevious End If End If End Sub
Click here to view a related sample program
You might also want to check out our Inventory Management System created using ADO data control.
Katika somo la 22 na 23, tulijifunza jinsi ya kujenga programu za database za VB kwa kutumia udhibiti wa data. Hata hivyo, udhibiti wa data si chombo rahisi sana kama inaweza tu kazi na aina ndogo ya data na lazima kazi kabisa katika mazingira ya Visual Basic. Ili kuondokana na mapungufu haya, tunaweza kutumia udhibiti wa data wenye nguvu zaidi katika Visual Basic, unaojulikana kama udhibiti wa ADO. ADO ni kifupi cha ActiveX data vitu. Kwa kuwa ADO inategemea ActiveX, inaweza kufanya kazi katika majukwaa tofauti na lugha tofauti za programu. Kwa kuongezea, inaweza kupata aina nyingi tofauti za data kama vile data iliyoonyeshwa katika vivinjari vya mtandao, maandishi ya barua pepe na hata picha tofauti na habari ya kawaida ya hifadhidata ya uhusiano na isiyo ya uhusiano. Ili kufanya hivyo, bonyeza Ctrl + T kufungua sehemu dialog box na kuchagua Microsoft ActiveX Data Control. Baada ya hapo, unaweza kuendelea kujenga maombi yako ya database ya VB ya ADO. Mfano ufuatao unaonyesha jinsi ya kujenga programu ya database yenye nguvu kwa kutumia ADO data control. Kwa kweli, ni programu ya maktaba. Kwanza kabisa, jina fomu mpya kama frmBookTitle na kubadilisha caption yake kwa Kitabu Vichwa-ADO maombi. "Kisha, weka ""ADO Data Control"" na jina lake kama ""Adobooks"" na kubadilisha caption yake kwa ""Book.""" Ifuatayo, kuingiza viashiria muhimu, masanduku ya maandishi na vifungo amri. Programu ya runtime inaonyesha katika Picha 25.1 hapa chini, inaruhusu kuongeza na kufuta, pamoja na kusasisha na kuvinjari data. "Kama ilivyoelezwa katika kifungu cha ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass) na ""Mafuniko ya Usimamizi"" (Compass)." Kwa hivyo, kwa sababu ya kuwa na BIBLIO.MDB, tunahitaji kuunda faili ya BIBLIO.MDB. Ili kuunganisha ADO na faili hii ya hifadhidata, fuata hatua zifuatazo: a) Bonyeza ADO kwenye fomu na ufunguo wa mali. "Kama ilivyoonyeshwa kwenye picha ya chini, ""Kupitia"" ni sehemu ya ukurasa wa mazungumzo ya ""Kupitia"" ambayo itaonekana kwenye picha ya chini." Wakati sanduku la mazungumzo kuonekana, kuchagua Matumizi ya String ya uhusiano Option. "Kisha, bonyeza ""Kujenga"" na katika sanduku la mazungumzo ""Kiungo cha data,"" bonyeza chaguo lililoitwa ""Mtoa huduma wa OLE DB wa Microsoft Jet 3.51.""" Baada ya hapo, bonyeza kitufe cha pili kuchagua faili BIBLO.MDB. Unaweza bonyeza juu ya Nakala Connection kuhakikisha muunganisho sahihi ya faili ya database. Bonyeza OK kumaliza uhusiano. Hatimaye, bonyeza juu ya RecordSource mali na kuweka aina ya amri kwa adCmd Jedwali na Jina la Jedwali kwa Vichwa. Sasa wewe ni tayari kutumia faili ya database. Sasa, unahitaji kuandika kificho kwa ajili ya vifungo vyote amri. Baada ya hapo, unaweza kufanya ADO kudhibiti asiyeonekana. "Kama ilivyoelezwa hapo juu, ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha"" na ""Safisha"" ni kifungu cha ""Safisha""." ", vbYesNo, "Uhakikisho wa Ufutaji") Kama kuthibitisha = vbYes Kisha adoBooks.Recordset.Delete MsgBox"Record Deleted! ", "Ujumbe" Else MsgBox "Record Si kuondolewa! "Kama ni ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza"" au ""Kupunguza."