Thought Sharing

Tuesday, November 23, 2010

Database connectivity using ADO (ActiveX data Object)

Database connectivity using ADO (ActiveX data Object)
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:
1.) Open a new VB project and from the Components window (by clicking Project -> Components menu), select Microsoft Activex Data Control 6.0 (OLEDB) and click OK. ADO data control will appear in the toolbox.
2.) From toolbox, Drag & Drop Activex Data object data control (ADODC1) on your form.
3.) Click on the ADO control on the form and it will open the properties window.
4.) Click on the General tab and check the ConnectionString property

When the dialog box appears, select the Use Connection String's Option. Next, click build command button and at the Data Link Properties window, double-Click the option Microsoft Jet 3.51 OLE DB provider.

After that, click the Next button to select the database name BIBLO.MDB. You can click on Test Connection button to ensure proper connection of the database file. A messagebox will appear, Click OK to finish the connection.

5.) Finally, click on the RecordSource property and set the command type to adCmd Table and Table name to Customers. Now you are ready to use the database file.
6.) Drag & Drop a Textbox control (Text1) on your form, and set the properties as follows:
DataSourceName: Select ADO data control name: ADODC1
DataField: Select field of table to be displayed: CustomerID
7.) Now repeat the above step for all other text boxes on the form to set their DataSource, and DataField properties.
8.) Run the form by pressing F5 key.

Database connectivity using RDO (Remote data Object)

Database connectivity using RDO (Remote data Object)

RDO uses ODBC (Open Data Base Connectivity), an ODBC Data Source, which must be set up through the Windows Control Panel. Follow the steps below to set up an ODBC Data Source (this process is also called "setting up a DSN", where "DSN" stands for "Data Source Name").The following steps is used to setup system DSN as:
1.) Go to Start -> Windows Control Panel; double-click on Administrative Tools, then Data Sources (ODBC). The ODBC Data Source Administrator screen is displayed. Click on the System DSN tab.
2.) Click the Add button. The Create New Data Source dialog box will appear. Select Microsoft Access Driver (*.mdb) from the list and click the Finish button.
3.) The ODBC Microsoft Access Setup dialog box will appear. For Data Source Name, type Biblio. If desired, you can type an entry for Description, but this is not required.
4.) Click the Select button. The Select Database dialog box appears. On a default installation of VB6 or Visual Studio 6, the BIBLIO.MDB sample database should reside in the folder C:\Program Files\Microsoft Visual Studio\VB98. Navigate to that folder, select BIBLIO.MDB from the file list, and click OK.
5.) When you are returned to the ODBC Microsoft Access Setup screen, the database you selected should be reflected as shown below. Click OK to close this screen.
6.) When you are returned to the ODBC Data Source Administrator screen, the new DSN should appear as shown below. Click OK to close this screen.
Connecting VB application to MS Access database using the RDO Data Control (RDODC):
1.) Start a new VB project, and from the Components dialog box (invoked from the Project -> Components menu), select Microsoft Remote Data Control 6.0 (SP3) as shown below and click OK.
2.) Drag & Drop a remote data control (RDC) on your form, and set the properties as follows
DataSourceName: Select the DSN name as created above. Eg. Biblio
SQL: select * from Customers
3.) Now put text boxe on the form, and set DataSource, and DataField properties as follows:
DataSource: MSRDC1
DataField: CustomerName
Repeat this step for all other textboxes on the form.
4.) Save and run the program. It works just like the other data control.
Code for Add, delete, edit, update, movefirst, movenext, first, last operations on the click event of command button:
Private Sub Command1_Click()
MSRDC1.Resultset.AddNew
End Sub
======================
Private Sub Command2_Click()
MSRDC1.Resultset.Delete
End Sub
=====================
Private Sub Command3_Click()
MSRDC1.Resultset.MoveFirst
End Sub
======================
Private Sub Command4_Click()
MSRDC1.Resultset.Update
End Sub
======================
Private Sub Command5_Click()
MSRDC1.Resultset.Edit
End Sub
======================
Private Sub Command6_Click()
MSRDC1.Resultset.MovePrevious
End Sub
======================
Private Sub Command7_Click()
MSRDC1.Resultset.MoveFirst
End Sub
======================
Private Sub Command8_Click()
MSRDC1.Resultset.MoveLast
End Sub

Database connectivity using DAO (Data Access Object) i.e. Data Control (Data1)

DAO Data Control uses Microsoft Jet Engine to connect VB application to MS Access database file for example Nwind.mdb. The steps of connectivity are as follows:
1.) Open visual basic application. Drag & drop a data control (Data1) and textboxes on the form.
2.) For Data control (Data1) specify two properties :
i.) DatabaseName: specify the name of database. Eg. Nwind.mdb
ii.) RecordSource: specify the name of table you want to connect to. Eg. Customers
3.) Now for displaying data in textboxes, we need to specify two properties:
i.) DataSourceName: specify the name of data control (Data1)
ii.) Datfield: specify the name of the field of table whose data is to be displayed in this textbox. For eg. Customer Name.
4.) Repeat the above step for all the data bound controls i.e. textboxes on the form.
5.) Now run the form by pressing F5 key, to show the data of Customer Table.

List of Programs for Visual Basic Practical

List of Programs for Visual Basic Practical

1. Login page
2. Registration form
3. Calculator
4. Comparison b/w two no, Comparison b/w 3 no
5. Display pattern of counting
6. Display the table of 2.
7. Display the table of any number
8. Pattern of alphabets in serial order and in reverse order (eeeee,dddd,ccc,bb,a)
9. Pattern of alphabets in serial and reverse order (a,ab,abc,abcd,abcde)
10. Pattern of stars (*,**,***,****,*****): in serial and reverse order
11. To check where a number is Prime or Not.
12. Check no is even or odd
13. Check the year is leap year or not
14. Calculate compound interest
15. Calculate simple interest
16. Factorial of a number
17. To check a string is palindrome or not
18. Traffic light signal using Timer control
19. Storing and displaying 10 numbers in an array
20. Sorting 10 numbers stored in an array
21. Fibonacci series
22. Do while, loop while, while wend, Do until, loop until
23. Conversion of Celsius to Fahrenheit and vice versa
24. Swapping 2 numbers
25. Sum and Average of 10 numbers stored in a array
26. Creating an MDI application
27. Database connectivity using DAO
28. Database connectivity using RDO
29. Database connectivity using ADO