| Topic: Creating a Database and a Table In this lesson we will consider some projects using DAOs, ODBC and ADOs.
1. Creating a Database and a Table:The dbEngine object is used to create the workspace for the database file. The TableDef object is used to create the table. Drag three command buttons in the form, caption them, size them and position them as shown in the figure 1. Open the code window and enter the codes as shown in the figure 1a. Data are inserted in the table by the sql command ‘insert’. The syntax of the Insert statement is:INSERT INTO AUTHOR VALUES(AUID, ANAME) VALUES(24, ‘KKKK’)Any number of records can be added, but each time the enter data button is pressed. Save and run the project and see how the three commands work. You will get an error message if you try to create a database which is already present. You can see the database table in the Immediate window by pressing the display button. In order to see the database table in the DBGrid, open another form. Place a data control and a DBGrid control box. Bind the data control to the database ‘publisher’ and the Record Source to ‘author’. DBGrid is bound to ‘Data1’.. and The form2 is shown in the figure 2 and the output is shown in the figure 3. Notice the two forms are not related in the program and so is separately run. To run the form2, open the properties dialog table by pressing the properties item from the project menu and select form2 for the startup object from the dropdown list.Dim ws As Workspace Private Sub Command1_Click() Private Sub Command2_Click() Figure 1a
2. Project using DAO object TableDef: In this project data are added to the table using text boxes and the Addnew method. The input form appears as shown in the figure 4. Enter the code in the code window as shown in the figure 4a. Save and run the project. The output window will look as in the figure 5. Clicking the display button will update the debug window and clicking the refresh button will update the DBGrid window.Dim db As Database Private Sub Command2_Click() Private Sub Command3_Click() Private Sub Command4_Click() Figure 4a
3. Project using QueryDef Object: In this project we make use of the QueryDef object to execute a stored sql query. The input form contains only a command button. Enter the code as shown in the figure 6a. The output window appears as shown in the figure 6.Dim dbspayroll As Database Sub CreateQueryDefX() End Function Private Sub Command1_Click() Figure 6a
Project for querying from multiple tables using Querydef: Suppose you have two tables named student and course in a database college as shown in the figure 7 and 8. You want to make a query involving data from both tables and print them in the sorted order of the student numbers. Enter the code as shown in the figure 7a. The output of the query appears as shown in the figure 9. Dim dbscollege As Database Sub CreateQueryDefX() Private Sub Command1_Click() Figure 7a
. A Data Access Object can be connected by a ODBC driver and data objects can be accessed and printed in the Debug window. First you set up a form with a single command button as shown in the figure 10.. In the code window of the command button enter the code as shown in the figure 10a. Save and run the project. You will get the output for all the SQL queries entered in the program. You can modify the program so as to enter the queries from an input text box at the time of execution. A typical output is shown in the figure 11. Private Sub Command1_Click() End Sub Figure 10a
|