Documente Academic
Documente Profesional
Documente Cultură
Application
Development-Tutorials
You would definitely get complete details of various issues related to the
design of a customized application development. The only requirement to
understand this chapter is know the basics of VB 6.0. This chapter starts with few
small tutorial examples and then it takes you to Software Requirement
Specification (SRS) of Company database and explains the procedures to create
forms, command buttons, VB code, and many more. By practicing this case study,
you can develop any other application package with out any additional efforts.
The entire chapter is divided into four parts each one considers a different
backend tool.
PART - I
11.1 TUTORIALS USING ORACLE9i & VB6.0
We shall begin with tutorials that use Oracle9i DBMS as back-end and VB6.0 as
front-end tool. It is assumed that the reader is aware of VB6.0 already so that
understanding the tutorials become easier.
Application Development: Tutorials 427
11.1.1 Tutorial - I
The aim of this tutorial is to:
Create a simple table 'Test' in Oracle9i with two attributes 'RegNo' and 'Name'
and develop an application in VB6.0 to add new records, browse, modify, and
delete the records.
We shall assume the following hardware and software:
Step-1: Go to the SQL window. Click on Start > All programs > Oracle-OraHome90
> Application Development > SQL Plus.
Step-3: Click Start > Control Panel > Administrative tools > Data Sources(ODBC).
Step-4: Press Add button and enter a name for the DSN (Data Source Name). Leave the rest
blank. In this example, it is named as “Elective”.
The screenshots of Step-3 and Step-4 are shown below:
428 Chapter 11
Step-5: Click Start > All Programs > Microsoft Visual Studio 6.0 > Microsoft Visual Basic 6.0. In
the “New Project” Window, select “Standard EXE” icon and then click the “Open”
button. You will see a new Project window and a form window with the default names
“Project1” and “Form1”.
Step-6: Change Name property of the form to “Demo1” and the caption property to “Demo1”.
Step-7: Draw the following controls and assign the names as given below:
After creating these controls, the form should look like as shown in Figure
11.1.
Step-9: Enter the code for the form load, and other controls as given in the next page.
Global Section
Option Explicit
Dim DB As Connection
Dim rstStudent As Recordset
Cancel Button
Private Sub cmdCancel_Click()
rstStudent.CancelUpdate
rstStudent.Requery
End Sub
Delete Button
Private Sub cmdDelete_Click()
On Error Resume Next
Dim intChoice
If Not rstStudent.EOF Then
intChoice = MsgBox("Are sure you want to delete?", vbYesNo + vbCritical)
If intChoice = vbYes Then
rstStudent.Delete
rstStudent.MoveNext
If rstStudent.EOF Then
Application Development: Tutorials 431
rstStudent.MoveLast
End If
End If
End If
End Sub
New Button
Private Sub cmdNew_Click()
rstStudent.AddNew 'Create a new record and clear
txtRegNo.Text = ""
txtName.Text = ""
txtRegNo.SetFocus
End Sub
432 Chapter 11
Save Button
Private Sub cmdSave_Click()
On Error Resume Next
rstStudent!RegNo = txtRegNo.Text
rstStudent!Name = txtName.Text
rstStudent.Update
cmdFirst.Enabled = True
cmdPrev.Enabled = True
cmdNext.Enabled = True
cmdLast.Enabled = True
rstStudent.Requery
End Sub
Exit Button
Private Sub cmdExit_Click()
End
End Sub
Application Development: Tutorials 433
Step-10: Save the project with appropriate name (in this example it is “Demo1”). You can set
the name of the project by selecting Project > Project1 properties and type
“DatabaseDemo” in the Project Name text box and then click “OK” button (See Figure
11.3).
Exercise
Create a new table called “Projects” with attributes ”PNo”, “PName”, and “RegNo”. Here RegNo
is a foreign key, which references “RegNo” of “Student” table. Design a list box and a combo box.
The combo box should be populated with student register numbers (or name). When a register
number or name is selected from the combo box, the list box must be populated with all the
projects the student is working on.
The next tutorial solvs this exercise and is named as Tutorial-II.
11.1.2 Tutorial - II
This tutorial is an extension of Tutorial-I and in fact the solution of the exercise
given in the previous section. However, we need to create a new table called
‚Projects‛ as shown below:
The other controls are same as the previous Tutorial. However, some new GUI
components have been added to this project. The first one is a menu and the
second one is a tool bar. We shall explain the steps involved in designing these
controls.
Step-4: To create a menu, click Tools > Menu Editor (ctrl + E). Add main menu items
and sub menu items as shown in Figure 11.4.
Step-5: Creating a tool bar is little tricky. Click Add-ins > Add-in Manager… > Application
Wizard and then check the Loaded/Unloaded and Load on Startup boxes (see Figure
(11.5). This step brings two items for the Add-ins called “Application Wizard” and
“Toolbar Wizard”.
Application Development: Tutorials 435
Step-6: Click on Add-Ins > Toolbar Wizard… and add the appropriate icons as shown in Figure
11.6.
Step-8: Type the code for various controls as follows: (Recall that to add the code for each
button/menu item/toolbar icon, simply double click or single click on the appropriate
control)
Option Explicit
Dim DB As Connection
Dim rstStudent As Recordset
Dim rstProject As Recordset
rstStudent.MoveLast
End If
End If
End If
End Sub
Toolbar1.Buttons.Item(2).Enabled = False
Case "Undo"
rstStudent.CancelUpdate
rstStudent.Requery
cmdSave.Enabled = False
mnuSave.Enabled = False
Toolbar1.Buttons.Item(2).Enabled = False
Case "Delete"
On Error Resume Next
Dim intChoice
If Not rstStudent.EOF Then
intChoice = MsgBox("Are sure you want to delete?", vbYesNo + vbCritical)
If intChoice = vbYes Then
rstStudent.Delete
rstStudent.MoveNext
If rstStudent.EOF Then
rstStudent.MoveLast
End If
End If
End If
Case "Help"
'ToDo: Add 'Help' button code.
MsgBox "Under construction."
End Select
End Sub
rstStudent.MoveFirst
cmdPrev.Enabled = False
cmdNext.Enabled = True
End Sub
End Sub
Toolbar1.Buttons.Item(2).Enabled = False
End Sub
Private Sub mnuExit_Click()
End
End Sub
Step-9: Finally, save the form and the project. In our example - “Demo2” is the form name and
“DataBaseDemo” is the name of the project.
Step-10: Run the application and test it with some sample data.
Exercise
Create a third table "Course" and add appropriate foreign keys. Modify the design presented in
this section to incorporate this table too.
Another advantage of this application is that any database application can use
this package for security purposes. The steps needed to develop this application
are shown in the following steps. Since we have already shown detailed steps in
creating form(s), menus, and tool bar, this tutorial does not give the steps in detail.
Step-2: Create a new form and give the “frmEMC” and its caption property as “Enterprise
Manager Console”. Save the project by giving the name as “Security”.
Step-4: Let us see what the main menus are and sub menu items that are required for this
application.
Step-5: The next task which needs to be done is the creation of tool bar as shown in Figure
11.8 (this figure shows the complete form also).
Step-6: Type the following code for various controls of this form.
Option Explicit
Dim DB As Connection
Dim rstUser As Recordset
DataGrid1.AllowAddNew = False
DataGrid1.AllowDelete = False
DataGrid1.AllowUpdate = False
Disable_fields
cmdShow.SetFocus
End Sub
444 Chapter 11
Step-7: Create a second form with name “frmLogin” and caption “Login”. Save the form as
“frmLogin” (See Figure 11.9).
Application Development: Tutorials 447
Dim DB As Connection
Dim rstId As Recordset
Public LoginSucceeded As Integer
PART - II
11.2 TUTORIAL USING MS-Access & VB6.0
All the examples shown in Part-I used Oracle9i as back-end. Let us show an
example with MS-Access as back-end in this section. However, we shall retain the
front-end as VB6.0 only.
The design using MS-Access is not much different from Tutorial-I of Part-I. All
that we need to do is to change the code in the FormLoad section as given below:
Private Sub Form_Load()
Application Development: Tutorials 449
The database file Demo.mdb with the table name 'Test' should be created in MS-
Acces with the same set of attributes as shown earlier. The above code (bold letters)
is a partial one and it is assumed to be in the folder: D:\DBMS\Tutorial-I. The rest
of the code is same as Tutorial-I. You don't need to change anything else at all.
PART - III
11.3 TUTORIAL USING SQL Server & VB6.0
The Tutorials given in the earlier sections did not cover a very important control
called "FlexGrid". The purpose of this section is to include the FlexGrid control and
demonstrate its usage in a typical application.
Let us adopt the same procedure as we did for the other tutorials too.
However, one major change that we introduce in this example is use of SQL Server
2000 as the backend database instead of Oracle 9i.
Step-1: Create a DSN called "T1" for SQL Server. The procedure is same as that of the
example shown in Section 11.4.4. However, you must select an appropriate "Data
Source" i.e. SQL Server) from the window "Create New Data Source".
Step-2: Create a table called "Product" in SQL Server (see figure 11.10).
Step-3: The next task is to create a form in VB with various controls as shown in Figure 11.11.
Fig. 11. 11 Data entry form for Product table with a FlexGrid Control
Notice that the form has three text boxes and a combo box. In addition, we have a flexgrid called
"Grid1" where multiple data records be added. You can notice also that every time a
Application Development: Tutorials 451
new record is added, by pressing "Add" button, the partial sum is shown in a label box
against "Total".
Step-4: After creating the layout with various controls, type the code for each button and Form
Load section as given below:
Option Explicit
Dim cn As ADODB.Connection
Dim rstemp As New ADODB.Recordset
Dim rsgrn As New ADODB.Recordset
Dim Sum As Double 'to compute partial sum
databaseerror:
cn.RollbackTrans
rsgrn.Close
MsgBox "The following error has occured while saving data : " & Chr(13) +
Chr(10) & " No : " & Err.Number & " Description : " & Err.Description
End Sub
For i = 1 To Grid1.Rows - 2
rsgrn.AddNew
rsgrn.Fields("grnno") = Val(Grid1.TextMatrix(i, 1))
rsgrn.Fields("pcode") = Grid1.TextMatrix(i, 2)
rsgrn.Fields("value") = Val(Grid1.TextMatrix(i, 3))
rsgrn.Fields("quantity") = Val(Grid1.TextMatrix(i, 4))
rsgrn.Update
Next i
'commit transaction to save changes
cn.CommitTrans
MsgBox "The data has been saved successfully", vbInformation, "FMC United"
rsgrn.Close
Grid1.Clear
Call initgrid
'txtgrn.Text = Val(txtgrn.Text) + 1
cmbCode.SetFocus
Sum = 0
lblTotal.Caption = Sum
Exit Sub
databaseerror:
cn.RollbackTrans
rsgrn.Close
MsgBox "The following error has occured while saving data : " & Chr(13) +
Chr(10) & " No : " & Err.Number & " Description : " & Err.Description
End Sub
PART - IV
11.4 COMPANY DATABASE
Though there are many problems for automation, we have selected a very simple
problem called the Company Database. This problem has been discussed already
in Chapter 1; but now we shall describe its implementation issues. Following is a
simple abstract of the problem domain for the Company database.
454 Chapter 11
In addition, Figure 4.2 showed the table structure for the entire application
after normalization. Remember, to solve any new problem, you are expected to
start with an ER diagram, translate into tables, and normalize these tables at least
up to BCNF level. In our example, all these steps have already been done and so
we can proceed to create these tables in Oracle9i. The next section does this.
Step-1: In the SQL prompt, type the following programs: (alternatively, you can enter these
programs in a notepad and copy and paste in the SQL prompt).
Application Development: Tutorials 455
COMMIT;
Step-1: Start > Control Panel. Double click on Administrative Tools and double click on Data
Sources (ODBC). You will see the screen similar to the one shown in Figure 11.12.
Step-2: Press Add button to create a new DSN source. After pressing Add button, you should
see a screen as shown in Figure 11.13.
Step-3: Select Microsoft ODBC for Oracle and then click on Finish button. Then another
small window appears as shown in Figure 11.14.
458 Chapter 11
Step-4: Enter "empl" in Data Source Name and then press OK. There is no need to enter
other details in this window.
With this step, you have successfully created a new data source name called
"empl" for Oracle ODBC.
application with some assigned privileges. In case if it does not match the login
screen, the login window should appear again. With this requirement in mind, we
shall show how to create the login form. The necessary steps are shown below:
Step-1: Create a new Login form with name as "frmLogin" and caption property set to "login".
The controls are as shown in Figure 11.15.
Step-2: Double click on "OK" button and "Cancel" button and type the appropriate code as
shown below:
Option Explicit
Public LoginSucceeded As Integer
Exit Ctrl+Q Department Details Work Info Employee Wage Details About
Project Details
Step-1: Create the above menu by invoking the menu editor and type the following details:
&File DbAcc
&Login Dblogin Ctrl+N
E&xit DbExit Ctrl+Q
&Divisions mnucmp
&Employee Details mnuemp
E&mployees mnuepl Ctrl+M
&Work Info mnuwrks Ctrl+W
D&epartment Details mnuDep
Departments… mnuDept Ctrl+E
Department &Locations mnudeploc Ctrl+L
&Projects mnuProj
&Reports mnurep
Employee Details mnuREmp
Employee Wage Details mnuRDept
Project Details mnuRProject
&Help AppHelp
&Technical Support Appulse F2
About ApplAbout
Application Development: Tutorials 461
Figure 11.17 shows the "frmMainForm" and its Picture property is set with the
bitmap image.
Step-2: Type the following code by double clicking the appropriate menu item.
Dim db As Connection
Private Sub ApplAbout_Click()
frmAbout.Show vbModal
End Sub
frmEmployee.Show vbModal
End Sub
can add, delete, modify, search, add dependent details, and many more. These
tasks can be achieved either by clicking the drop down menus or the tool bar icons.
Below are the steps needed to create the Employee form.
Step-1: Click Project > Add Form. Draw various controls as indicated in Figure 11.18
Option Explicit
Dim db As Connection
Dim rstEmployee As Recordset
Dim rstEmpFind As Recordset
Public strSSN As String 'to acces in frmModifyEmp form
choice = DataGrid1.Columns(1)
DataEnvironment1.cmdDependents choice
464 Chapter 11
Load rptDependent
rptDependent.Show vbModal
End Sub
End Sub
Step-1: Create a new form and name it as "frmAddEmployee" and save it with the name.
The Figure 11.19 shows the form with these controls and the labels.
Option Explicit
Dim db As Connection
Dim rstEmployee As Recordset
Dim rstDept As Recordset
Dim rstSuperSSN As Recordset
Private Sub cmdAdd_Click()
On Error GoTo err1
Dim strDate As String
Dim UsrDate As Date
Dim CurrentDate As Date
strDate = cboDay.Text + "/" + cboMonth.Text + "/" + cboYear.Text
UsrDate = Format(strDate, "dd/mmm/yyyy") 'convert string to date
CurrentDate = Format(Now, "dd/mmm/yyyy") 'convert string to date
If Not UsrDate > CurrentDate Then
rstEmployee!SSN = txtSSN.Text
rstEmployee!Name = txtName.Text
rstEmployee!BDate = strDate
rstEmployee!Address = txtAddr.Text
rstEmployee!Sex = cboSex.Text
rstEmployee!Salary = Val(txtSalary.Text)
rstEmployee!SuperSSN = cboSSSN.Text
rstEmployee!DNo = Val(cboDNo.Text)
rstEmployee.Update
Unload Me
Else
468 Chapter 11
MsgBox "Date of birth exceeds the current date", vbCritical, "Error in Birth date"
cboDay.SetFocus
strDate = cboDay.Text + "/" + cboMonth.Text + "/" + cboYear.Text
End If
Exit Sub
err1:
MsgBox Err.Description + Chr(13) + Chr(10) + "One or more fields may be empty",
vbCritical, "Error"
End Sub
'Populate Month
cboMonth.AddItem "Jan"
cboMonth.AddItem "Feb"
cboMonth.AddItem "Mar"
cboMonth.AddItem "Apr"
cboMonth.AddItem "May"
cboMonth.AddItem "Jun"
cboMonth.AddItem "Jul"
cboMonth.AddItem "Aug"
cboMonth.AddItem "Sep"
cboMonth.AddItem "Oct"
cboMonth.AddItem "Nov"
cboMonth.AddItem "Dec"
'Populate Year
Dim intYear As Integer
Dim strYear As String
Dim intY As Integer
intYear = 1947
strYear = Right(Format(Date, "dd/mm/yyyy"), 4)
intY = Val(strYear)
While intY >= intYear
cboYear.AddItem intY
intY = intY - 1
Wend
End Sub
Private Sub txtAddr_Validate(Cancel As Boolean)
txtAddr.Text = StrConv(txtAddr.Text, vbUpperCase)
If txtAddr.Text = "" Then
MsgBox "Incomplete or blank field not allowed!", vbCritical + vbOKOnly,
"ERROR"
Cancel = True
End If
End Sub
Step-4: Similar to "frmAddEmployee", create a new form for the Dependents and name it as
"frmDepen". The structure of the form is shown Figure 11.20.
Option Explicit
Dim db As Connection
Dim rstEmployee As Recordset
Dim rstDepend As Recordset
Dim rstBrowse As Recordset
cboSSN
cmdAdd
txtName
txtBDate
cboSex
cboRelation cmdCancel
Image1
cboEmpSSN
DataGrid
1
cmdDelete
cmdClose
Fig. 11.20 Dependent Form (Arrows indicate the name of the controls)
Frame2.Enabled = True
Unload Me
Exit Sub
err1:
MsgBox Err.Description
Frame2.Enabled = True
End Sub
rstDepend.AddNew
txtName.Text = ""
txtBdate.Text = ""
Image1.ToolTipText = "Browse"
cboSex.Text = "M"
Frame2.Enabled = False
End Sub
Step-6: One more work is remaining before we conclude this section. That is nothing but to
create yet another form for modifying the employee data. Hence, simply create a new
form just similar to "frmAddEmployee" form. However, the code will be different for this
form. You must give "frmModifyEmp" for the name property and save the form with this
name. The layout is shown in Figure 11.21.
Option Explicit
Dim db As Connection
Dim rstEmployee As Recordset
Dim rstDate As Recordset
Dim rstDept As Recordset
Dim rstSuperSSN As Recordset
Application Development: Tutorials 475
txtSSN.Text = rstEmployee!SSN
txtName.Text = rstEmployee!Name
txtAddr.Text = rstEmployee!Address
cboSex.Text = rstEmployee!Sex
txtSalary.Text = rstEmployee!Salary
cboSSSN.Text = rstEmployee!SuperSSN
cboDNo.Text = rstEmployee!DNo
'Populate Day
Dim intDay As Integer
intDay = 1
While intDay <> 32
cboDay.AddItem intDay
intDay = intDay + 1
Wend
'Populate Month
cboMonth.AddItem "Jan"
cboMonth.AddItem "Feb"
cboMonth.AddItem "Mar"
cboMonth.AddItem "Apr"
cboMonth.AddItem "May"
cboMonth.AddItem "Jun"
cboMonth.AddItem "Jul"
cboMonth.AddItem "Aug"
cboMonth.AddItem "Sep"
cboMonth.AddItem "Oct"
cboMonth.AddItem "Nov"
cboMonth.AddItem "Dec"
Application Development: Tutorials 477
'Populate Year
Dim intYear As Integer
Dim strYear As String
intYear = 1900
strYear = Right(Format(Date, "dd/mm/yyyy"), 4)
txtDNo cmdCanc
el
txtDName
cboMSSN
txtMDate
cmdClose
Give the name as "frmDept" and save the form with the same name. Now to
create the tool bar, you can follow the below steps:
Step-1: Draw an Imagelist from the tool box (left side of your main VB window). The default
name given to it will be "ImageList1".
Step-2: Right lick on the ImageList1 and select properties and select the Images tab (see
Figure 11.23).
Step-3: By clicking the "Insert Picure" button you can add any image (.ico, or .jpg, or .bmp
files). For each button, an index value is attached. This index value can be used to
reference appropriate image at the time coding. The figure shows after adding the
required five buttons for application.
Step-4: From the toolbox, select "Toolbar", and draw it on the form. The tool bar will look
empty. Right click on the tool bar and select properties. From the "General" tab, set the
ImageList combo box to "ImageList1". Then, click on the "Buttons" tab. To add a
toolbar icon, click "Insert Button" button and enter 1 in the "Image" text box (refers to
the first image of the ImageList1). Similarly you can add more icons, but ensure that
you fill up the Image text box with the corresponding image index (see Figure 11.24).
Application Development: Tutorials 479
Option Explicit
Dim db As Connection
Dim rstEmployee As Recordset
Dim rstDept As Recordset
'Populate Day
Dim intDay As Integer
intDay = 1
While intDay <> 32
cboDay.AddItem intDay
intDay = intDay + 1
Wend
'Populate Month
cboMonth.AddItem "Jan"
cboMonth.AddItem "Feb"
cboMonth.AddItem "Mar"
cboMonth.AddItem "Apr"
cboMonth.AddItem "May"
cboMonth.AddItem "Jun"
cboMonth.AddItem "Jul"
cboMonth.AddItem "Aug"
cboMonth.AddItem "Sep"
cboMonth.AddItem "Oct"
cboMonth.AddItem "Nov"
cboMonth.AddItem "Dec"
'Populate Year
Dim intYear As Integer
intYear = 2005
While intYear >= 1947
cboYear.AddItem intYear
intYear = intYear - 1
Wend
If rstDept.RecordCount = 0 Then
MsgBox "Department Table is Empty", vbCritical, "Error"
cmdFirst.Enabled = False
cmdPrev.Enabled = False
cmdNext.Enabled = False
cmdLast.Enabled = False
End If
End Sub
Case "New"
rstDept.AddNew 'Create a new record and clear
cmdFirst.Enabled = False
cmdPrev.Enabled = False
cmdNext.Enabled = False
cmdLast.Enabled = False
txtMDate.Visible = False
cboDay.Visible = True
cboMonth.Visible = True
cboYear.Visible = True
txtDNo.Text = ""
txtDName.Text = ""
cboMSSN.Text = ""
cboDay.Text = ""
cboMonth.Text = ""
cboYear.Text = ""
txtDNo.SetFocus
Case "Save"
'On Error GoTo err1
Dim strDate As String
rstDept!DNumber = txtDNo.Text
rstDept!DName = txtDName.Text
rstDept!MgrSSN = Null
Else
rstDept!MgrSSN = cboMSSN.Text
End If
strDate = cboDay.Text + "/" + cboMonth.Text + "/" + cboYear.Text
rstDept!MgrStartDate = strDate
rstDept.Update
cmdFirst.Enabled = True
cmdPrev.Enabled = True
cmdNext.Enabled = True
cmdLast.Enabled = True
cboDay.Visible = False
cboMonth.Visible = False
cboYear.Visible = False
txtMDate.Visible = True
rstDept.Requery
Case "Undo"
cmdCancel_Click
Case "Delete"
On Error Resume Next
Dim intChoice
If Not txtDNo = "" Then
If Not rstDept.EOF Then
intChoice = MsgBox("Are you sure that you want to delete?", vbYesNo +
vbCritical)
If intChoice = vbYes Then
rstDept.Delete
rstDept.MoveNext
If rstDept.EOF Then
Application Development: Tutorials 483
rstDept.MoveLast
End If
End If
End If
Else
MsgBox "No Record to Delete", vbExclamation
End If
End Select
Exit Sub
err1:
MsgBox Err.Description
End Sub
Option Explicit
Dim db As Connection
Dim rstProject As Recordset
Dim rstDept As Recordset
Case "Save"
On Error GoTo err1
rstProject!PNumber = Val(txtPNo.Text)
rstProject!PName = txtPName.Text
rstProject!PLocation = cboPLoc.Text
rstProject!DNum = cboDNum.Text
rstProject.Update
cmdFirst.Enabled = True
cmdPrev.Enabled = True
cmdNext.Enabled = True
cmdLast.Enabled = True
rstProject.Requery
Toolbar1.Buttons.Item(2).Enabled = True
Toolbar1.Buttons.Item(5).Enabled = True
Case "Undo"
cmdCancel_Click
Case "Delete"
On Error Resume Next
Dim intChoice
If Not rstProject.EOF Then
intChoice = MsgBox("Are you sure that you want to delete?", vbYesNo +
vbCritical)
If intChoice = vbYes Then
rstProject.Delete
rstProject.MoveNext
If rstProject.EOF Then
rstProject.MoveLast
End If
End If
End If
End Select
Exit Sub
err1:
MsgBox "The following Error has occured: " + Err.Description
End Sub
records, we shall use a FlexGrid to obtain all the records (maximum is say 3) and
save together in the database.
Step-1: Go to Project > Add Form > Form, then click Open.
To add the FlexGrid control to the toolbox, click Ctrl+T and select Microsoft FlexGrid Control
6.0) and press "OK".
Step-3: Type the following program, for each control shown in the above figure.
Option Explicit
Dim db As Connection
Dim rstTemp As Recordset
Dim rstDept As Recordset
For i = 1 To Grid1.Rows - 2
rstTemp.AddNew
rstTemp.Fields("DNumber") = Val(Grid1.TextMatrix(i, 1))
rstTemp.Fields("DLocation") = Grid1.TextMatrix(i, 2)
rstTemp.Update
Next i
'commit transaction to save changes
db.CommitTrans
Application Development: Tutorials 489
MsgBox "The data has been saved successfully", vbInformation, "FMC United"
Grid1.Clear
Call InitGrid
cboDeptNo.SetFocus
Exit Sub
DatabaseError:
db.RollbackTrans
MsgBox "The following error has occured while saving data : " & Chr(13) +
Chr(10) & " No : " & Err.Number & " Description : " & Err.Description
Grid1.Clear
Call InitGrid
cboDeptNo.SetFocus
End Sub
Step-2: Note that to modify the properties of the DataGrid, right click on the DataGrid and
select properties. Save the form as "frmWorks".
Option Explicit
Dim db As Connection
Dim rstWorks As Recordset
Dim rstEmployee As Recordset
Dim rstProject As Recordset
Dim rstTemp As Recordset
Private Sub cmdCancel_Click()
Unload Me
End Sub
Application Development: Tutorials 491
txtHours.Text = ""
rstWorks.AddNew
End Sub
rstTemp.CursorLocation = adUseClient
rstTemp.Open "Select * from Works_on order by ESSN", db, adOpenStatic,
adLockReadOnly
Set DataGrid1.DataSource = rstTemp
DataGrid1.Refresh
End Sub
Step-1: Add a new form with name "frmTechnical" and save the form with the same name.
Step-2: Draw a text box with name "txtName" and set its Multiline property to "true". You will
see a scroll bar attached automatically at design time and also at run time.
Step-3: Draw a Command Button called "cmdOK" with its caption property set to "OK".
"3. You can search for an employee by giving SSN or Name." + Chr(13) + Chr(10) + Chr(13) +
Chr(10) + _
"4. If you want to login as a different user, select File menu and click Login."
End Sub
Step-5: To design the "About" form, go to Project > Add Form and then select "About Dialog".
Step-7: You don't need to type any code for this form as VB automatically puts the code for
you. However, to customize for our application, modify the Form_Load section only as
shown below:
This completes the design of main forms. The next section would describe the
details of Report generation.
Application Development: Tutorials 495
Step-1: Go to Project > Add data Environment. You will see a default data environment by
name "Data Environment1" and a default connection "Connection1" (see Figure
11.30).
Step-2: Right click on "Connection1" and select "Add Command". By doing this you will get a
default command called "Command1". You can change the names of these controls by
going to the properties menu. For our application change the name of the command to
"cmdEmployee"
Step-3: Right click on the "cmdEmployee" and select the properties. You will see a new
window and check the SQL Statement. Type the SQL statement as shown in Figure
11.31 and click "OK" button.
Step-4: Similarly, create four more commands by right clicking the "Connection1". Give the
names as "cmdDependents", "cmdWages", "cmdProject", "cmdDept". Type the code
for each of these commands as shown below. See Figures 11.32.
Step-5: The next step is to design the layout for the reports. Click on Project > Add Data
Report. You will see a default data report called "Datareport1" created. Rename it as
"rptEmployee".
Step-6: Change the name property to "Employee Details". Firstly, select the data report
window. Next, click on the Data Source property and select "Data Environment1".
Finally, select the Data Member property and select the appropriate command. In this
case, click on "cmdEmployee" (See Figure 11.33).
498 Chapter 11
Step-7: Right Click on the Detail Section > Insert Control > Textbox. Select Data Member
property and select "cmdEmployee". Next, you must select Data member property and
click the appropriate field. Continue this process until you create textboxes for all the
fields. Create labels wherever required. Recall that the textboxes in the "Detail" section
will be filled by all the records of the table.
Step-8: Repeat Steps-5 to Step-7 for the remaining commands. The reports for Employee and
Department at run time are shown in Figure 11.34.
Application Development: Tutorials 499
This completes the entire design of the Company Database. You can now test
the application by entering sample data and generate few reports. To receive the
soft copy, please send an e-mail to the author (nand_gopalan@vsnl.net).
11.6ADDITIONAL EXAMPLES
This section discusses some more database problems and their solutions using SQL
statements. A number of exercise problems for designing VB forms and reports are
given at the end of this chapter. Readers are encouraged to solve these problems to
get better understanding of the application development.
Solution
(a)
create table Person(
Driverid# varchar2(5), Name varchar2(15) not Null, Address varchar2(20),
primary key (Driverid#));
(b)
Insert into Person values('1111', 'Ramu', 'K. S. Layout');
Insert into Person values('2222', 'Michel', 'Indiranagar');
Insert into Person values('3333', 'Priya', 'Rajajinagar');
Insert into Person values('4444', 'Gopal', 'Whitefield');
Insert into Person values('5555', 'Latha', 'Indiranagar');
(c)
update Participated
set damageamount = 1111
where RegNo = 'KA01P4028' and ReportNo between 12 and 25000;
(d)
SELECT COUNT(*) FROM ACCIDENT WHERE ACCDATE LIKE '__-___-02';
(e)
Select count(*) from Car C, Participated P
where C.RegNo = P.RegNo and Model = 'MARUTHI-DX';
Solution
(a)
Create table Customer(
Cust# integer, CName varchar2(15) not Null, City varchar2(15),
primary key (Cust#));
(b)
Insert into Customer values (1111, 'Gupta', 'Bangalore');
Insert into Customer values (2222, 'Deepali', 'Mangalore');
Insert into Customer values (3333, 'Kiran', 'Chennai');
Insert into Customer values (4444, 'John', 'Bangalore');
Insert into Customer values (5555, 'Abbas', 'Belgaum');
(c)
Select C.CName, count(CO.Order#), Avg(CO.OrdAmt)
from Customer C, CustOrder CO
where C.Cust# = CO.Cust#
group by C.CName, CO.Cust#;
(d)
Select Order#, Warehouse# from Shipment where warehouse# in
(select warehouse# from warehouse where City = 'Bangalore');
(e)
Delete from Item where Item# = 10 and
(update Order_Item set Item# = Null where Item# = 10);
Solution
(a)
create table Student(
RegNo varchar2(10), Name varchar2(20) not null,
Major varchar2(10) not null, BDate date,
primary key(RegNo));
504 Chapter 11
(b)
Insert into Student values('1BI00CS010', 'KIRAN', 'CSE', '02-JAN-1983');
Insert into Student values('1BI01ME075', 'DIVYA', 'MECH', '10-DEC-1984');
Insert into Student values('1BI02EC041', 'DIWAKAR', 'ELNS', '22-FEB-1984');
Insert into Student values('1BI00CS035', 'JOHN', 'CSE', '23-MAR-1982');
Insert into Student values('1BI03CS010', 'MUKUND', 'CSE', '15-AUG-1983');
(c)
Select C.Course#, T.ISBN, T.BookTitle
from Course C,Book_Adoption BA, Text T
where C.Course# = BA.Course# and BA.ISBN = T.ISBN and C.Dept = 'CSE'
group by C.Course#, T.ISBN, T.BookTitle;
Solution
(a)
Create table Author(
Authorid integer, AName varchar2(15), ACity varchar2(15), ACountry varchar2(15),
primary key (Authorid));
Create table Publisher(
Publisherid integer, PName varchar2(15), PCity varchar2(15), PCountry varchar2(15),
primary key (Publisherid));
(b)
Insert into Author values(1000, 'Nandagopalan', 'Bangalore', 'India');
Insert into Author values(2000, 'Tony', 'Haywood', 'USA');
Insert into Author values(3000, 'Holzner', 'New York', 'USA');
Insert into Author values(4000, 'Tennenbaum', 'London', 'UK');
Insert into Author values(5000, 'Balaguruswamy', 'Chennai', 'India');
(c)
select C.Authorid, A.AName
from Catalog C, Author A
where A.Authorid = C.Authorid and C.Year > 2000 and C.Price >
(Select Avg(Price) from Catalog)
group by C.Authorid, A.AName
having count(C.Authorid) >= 2;
(d)
Create View SalesDetails as (
Select OD.Bookid as Book#, C.Price as Cost, Sum(OD.Quantity) as Qty, Sum(OD.Quantity *
C.Price) as Sales
from Order_Details OD, Catalog C, Author A
where OD.Bookid = C.Bookid and C.Authorid = A.Authorid
group by OD.Bookid, C.Price);
(e)
update Catalog set Price = Price * 1.10
Application Development: Tutorials 507
In this application, the main menu items and sub menu items are arranged
differently compared to the other applications. For example, from the main menu
the users can navigate to open a Branch, or open a new Customer, or open a new
Loan, etc. They can also enquire about Branches, or Accounts, or Balance amount of
any account type. The Reports Menu is not filled with any specific details and is
left to the reader to design relevant reports. Keeping these requirements, let us
design the menu and is shown in Figure 11.38.
Now, the VB codes for all the above forms appear in the next page:
510 Chapter 11
End If
End Sub
This application has been tested with some sample data and found working at
all conditions.
learn how to specify the DSN, ODBC driver and record set statements in a Java
program. Notice that most of the task is almost similar to VB6.0.
The steps to build this databse example are very simple. The first step is to
create a table named as ‘Customer’ in Oracle9i with columns <CustID, CustName,
Balance>. Next, specifiy CustID as primary key. The second step is to add few
records and commit. Ensure that the ODBSC DSN for Oracle exists with the name
‘Elective’.
This example uses JBuilder2005 to create the Java program. Create a java
project named as ‘customerdb’ and name the java program as ‘Customer.java’.
For the complete program and output, see the below program:
package customerdb;
/**
* <p> JDBC - ODBC Demo (uses JBuilder 2005)
* </p>
* <p>Demo of how to connect to MS-Access Database from Java
* it also shows how to retrieve the data from a table and display it
* on the console
* </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: BIT </p>
* @author S. Nandagopalan
* @version 1.0
*/
import java.sql.*;
import sun.jdbc.odbc.*;
class Customer {
public Customer() {
}
514 Chapter 11
Notice that cid, cname, and cbal are the host variables and assuming the table
Customer has only three records.
11.7 SUMMARY
This Chapter discussed several important points with regard to the database
application development
Simple tutorials were given to get the basic idea of developing VB6.0 based
applications. These tutorials used Oracle9i as the database where small tables
were created
Next, using the same front-end tool of VB6.0 it was shown how to make use of
tables created using MS-Access
Part-IV gave a very detailed and enterprise level database application
development called Company database. Readers can apply this example for
developing more sophisticated applications
Finally, a number of examples were shown to develop SQL queries.
11.8 EXERCISES
11.1 Design and develop a complete database application using Oracle9i and
VB6.0 for the following problems:
a) Hospital Management System (in-patient and out-patient)
b) Library Management (reference section, lending section, and journals)
c) Cricket Database Management
d) CD/DVD Library Management
e) On-Line Examination System
f) Inventory Control and Maanagement
g) Car Sales and Service Management
All the above database applications require images to be maintained. Suggest
the best and efficient way to store and retrieve images to and from the
database.
11.2 Identify some meaningful reports and implement them using VB's Data
Environment Report facility for the problems in 11.1.
516 Chapter 11
11.3 Develop suitable front-end for all the four additional problems dicussed in
Section 11.6. Also design some useful reports for the databases.