sas

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
Dim db As Database
Dim td As TableDef

Private Sub Command1_Click()
'program for creating a database
Dim i As Integer
Dim flds(2) As Field
Set ws = DBEngine.Workspaces(0)
Set db = ws.CreateDatabase("c:\vbproj\publisher", dbLangGeneral, dbVersion25)
Set td = db.CreateTableDef("author")
Set flds(0) = td.CreateField("auid", dbLong)
Set flds(1) = td.CreateField("aname", dbText)
flds(1).Size = 10
td.Fields.Append flds(0)
td.Fields.Append flds(1)
db.TableDefs.Append td
MsgBox ("database created")
End Sub

Private Sub Command2_Click()
'program for inserting data
Set db = OpenDatabase("c:\vbproj\publisher")
Set rs = db.OpenRecordset("author", dbOpenTable)
db.Execute SQLTEXT, dbFailOnError
End Sub
Private Sub Command3_Click()
Set db = OpenDatabase("c:\vbproj\publisher")
Set rs = db.OpenRecordset("author", dbOpenTable)
Do While Not rs.EOF
Debug.Print rs(0), rs(1)
rs.MoveNext
Loop
rs.Close
End Sub

 Figure 1a

2

                                  Figure 1

table
              Figure 2

table
      Figure3

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
Dim rs As Recordset
Private Sub Command1_Click()
Set db = OpenDatabase("d:\rr2\college")
Set rs = db.OpenRecordset("author", dbOpenTable)
rs.AddNew
rs("authorid") = Text1.Text
rs("authorname") = Text2.Text
rs.Update
rs.Close
End Sub

Private Sub Command2_Click()
Set db = OpenDatabase("d:\rr2\college")
Set rs = db.OpenRecordset("author", dbOpenTable)
Do While Not rs.EOF
Debug.Print rs(0), rs(1)
rs.MoveNext
Loop
rs.Close
End Sub

Private Sub Command3_Click()
Set db = OpenDatabase("d:\rr2\college")
Set rs = db.OpenRecordset("author", dbOpenTable)
rs.MoveLast
rs.Delete
rs.Close
End Sub

Private Sub Command4_Click()
Data1.Refresh
End Sub

Figure 4a

table

                                     Figure 4

table
                     Figure 5

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()
 Dim qdfTemp As QueryDef
  Set dbspayroll = OpenDatabase("c:\vbproj\payroll.mdb")
 With dbspayroll
 Set qdfTemp = .CreateQueryDef("", "select * from Employee where salary > 7000")
 GetrstTemp qdfTemp
 .Close
 End With
 End Sub
 
 Function GetrstTemp(qdfTemp As QueryDef)
 Dim rsttemp As Recordset
 With qdfTemp
 Debug.Print .Name
 Debug.Print " " & .SQL
 Set rsttemp = .OpenRecordset(dbOpenSnapshot)
 Do While Not rsttemp.EOF
 Debug.Print rsttemp(0), rsttemp(1), rsttemp(2), rsttemp(3)
 rsttemp.MoveNext
 Loop
 With rsttemp
  .MoveLast
 Debug.Print "number of records = " & .RecordCount
 .Close
 End With

 End Function

Private Sub Command1_Click()
Call CreateQueryDefX
End Sub

Figure 6a

table
                    Figure 6

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()
 Dim qdfTemp As QueryDef
  Set dbscollege = OpenDatabase("d:\rr2\college.mdb")
 With dbscollege
 Set qdfTemp = .CreateQueryDef("", "select student.sno, student.sname, student.major, result.cno, result.grade from student, result where student.sno = result.sno order by student.sno")
  GetrstTemp qdfTemp
  .Close
 End With
 End Sub
 
 Function GetrstTemp(qdfTemp As QueryDef)
 Dim rsttemp As Recordset
 With qdfTemp
 Debug.Print .Name
 Debug.Print " " & .SQL
 Set rsttemp = .OpenRecordset(dbOpenDynaset)
   Do While Not rsttemp.EOF
 Debug.Print rsttemp(0), rsttemp(1), rsttemp(2), rsttemp(3), rsttemp(4)
 rsttemp.MoveNext
 Loop
 With rsttemp
  .MoveLast
 Debug.Print "number of records = " & .RecordCount
 .Close
 End With
 End With
 End Function

Private Sub Command1_Click()
Call CreateQueryDefX
End Sub

Figure 7a

v
                  Figure 8

table
                    Figure 9

.
Data Access through ODBC:

      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()
Dim wRKodbc As Workspace
Dim conpubs As Connection
Dim D As Database
Dim r As Recordset
Set wRKodbc = CreateWorkspace("NewODBCWorkspace1", _
        "admin", "", dbUseODBC)
           
'Set conpubs = wRKodbc.OpenConnection("Connection1", dbDriverPrompt)
 Set conpubs = wRKodbc.OpenConnection("Connection2", , , _
        "ODBC;DATABASE=PAYROLL;UID=ADMIN;PWD=SYSTEM;DSN=D1")
Debug.Print ""
Set r = conpubs.OpenRecordset("SELECT * FROM EMPLOYEE")
Do While Not r.EOF
Debug.Print r(0), r(1), r(2), r(3)
r.MoveNext
Loop

End Sub

Figure 10a

table
      Figure10

tabletable
                          Figure11

Prev

 

 

 

 

 

 

 

 

 


footer back link


 

Error in my_thread_global_end(): 1 threads didn't exit