Vous êtes sur la page 1sur 19

Steps to Connect

Visual Basic & Oracle

A tutorial by

B.BHUVANESWARAN
Lecturer / CSE
Rajalakshmi Engineering College
Thandalam, Chennai – 602 105

Mail your suggestion to

bhuvangates@yahoo.com, bhuvangates@gmail.com, admin@bhuvangates.com

Visit my site

www.bhuvangates.com
Steps to Connect Visual Basic & Oracle

1. Select SQL Plus from the Start menu.

2. Enter the User Name, Password and Host String.

3. Create the table (eg. Employee).


4. Select the Microsoft Visual Basic 6.0 from the Start menu.

5. Select the Project type as Standard EXE and click Open.


6. Create the form with relevant controls.

Control Name

Text Box txtEmpCode


Text Box txtEname
Option Button optMale
Option Button optFemale
DTPicker dtpDob
Combo Box cboDesig
Combo Box cboDept
Text Box txtSalary
Command Button cmdAdd
Command Button cmdView
Command Button cmdEdit
Command Button cmdDelete
Command Button cmdClear
Command Button cmdExit

7. For placing the DTPicker control, go to Project Æ Components (or) press Ctrl+T and
select the Microsoft Windows Common Controls-2.6.0 (SP3) from the Components and
click OK to place in the Toolbox.
8. Now the form look like as follows.

9. Change the form name as frmEmployee and project name as prjEmployee.

10. Place the ADO Data Control by selection the Project Æ Components (or) by pressing
Ctrl+T.
11. From the list of available components check the Microsoft ADO Data Control 6.0 (SP3)
(OLEDB).

12. From the Toolbox double click the Adodc control to place in the form.

13. Select the Adodc and right click. Then select ADODC Properties.
14. In the Property Pages dialog box click the Use Connection String radio button and click
the Build button.

15. It opens the Data Link Properties dialog box. In that select the Microsoft OLE DB
Provider for Oracle and click Next.
16. Enter the Server Name, User Name and Password correctly. Make the Allow Saving
Password box as checked and click Test Connection.

17. If it displays the “Test Connection Succeeded” message, then click OK.

18. Copy the text available in the Use Connection String text box.
19. Double click the form and place the cursor at the top of the code window and press enter.
Now we get General Æ Declaration section. Declare the following variables.

Dim connEmp As ADODB.Connection


Dim rsEmp As ADODB.Recordset

20. Double click the form, it loads the Form Æ Load event and enter the following code.
Private Sub Form_Load()
Set connEmp = New ADODB.Connection
connEmp.Open "Provider=MSDAORA.1;Password=baba;User ID=baba;Data
Source=IBMREC;Persist Security Info=True"
connEmp.CursorLocation = adUseClient
MsgBox "Connection Established..."
cmdClear_Click
End Sub

21. Your code is look like as following.

22. Run the program by clicking the Start button (or) by pressing F5 button. The program
will display the following message box.

23. Double click the Clear button and enter the following code.
Private Sub cmdClear_Click()
txtEmpCode.Text = ""
txtEname.Text = ""
optMale.Value = False
optFemale.Value = False
cboDesig.Text = ""
cboDept.Text = ""
txtSalary.Text = ""
End Sub
24. Double click the Add button and enter the following code.

Private Sub cmdAdd_Click()


Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset,
adLockReadOnly, adCmdText
If rsEmp.RecordCount <> 0 Then
MsgBox "Employee Code Already Exists..."
rsEmp.Close
Set rsEmp = Nothing
Exit Sub
Else
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee", connEmp, adOpenKeyset,
adLockPessimistic, adCmdText
rsEmp.AddNew
rsEmp!Empcode = Trim(txtEmpCode.Text)
rsEmp!Ename = Trim(txtEname.Text)
If optMale.Value = True Then
rsEmp!Gender = "Male"
ElseIf optFemale.Value = True Then
rsEmp!Gender = "Female"
End If
rsEmp!Dob = dtpDob.Value
rsEmp!Desig = Trim(cboDesig.Text)
rsEmp!Dept = Trim(cboDept.Text)
rsEmp!Salary = Val(Trim(txtSalary.Text))
rsEmp.Update
connEmp.Execute "commit"
rsEmp.Close
Set rsEmp = Nothing
MsgBox "Added Succesfully..."
cmdClear_Click
End If
End Sub

25. Double click the View button and enter the following code.
Private Sub cmdView_Click()
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset, adLockReadOnly,
adCmdText
If rsEmp.RecordCount <> 0 Then
txtEname.Text = Trim(rsEmp!Ename)
If Trim(rsEmp!Gender) = "Male" Then
optMale.Value = True
ElseIf Trim(rsEmp!Gender) = "Female" Then
optFemale.Value = True
End If
dtpDob.Value = rsEmp!Dob
cboDesig.Text = Trim(rsEmp!Desig)
cboDept.Text = Trim(rsEmp!Dept)
txtSalary.Text = Val(rsEmp!Salary)
MsgBox "Viewed Succesfully..."
Else
MsgBox "Employee Code Not Exists..."
End If
rsEmp.Close
Set rsEmp = Nothing
End Sub
26. Double click the Delete button and enter the following code.

Private Sub cmdDelete_Click()


If (MsgBox("Are you sure to delete...", vbYesNo) = vbYes) Then
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset, adLockPessimistic,
adCmdText
rsEmp.Delete
connEmp.Execute "commit"
rsEmp.Close
Set rsEmp = Nothing
MsgBox "Deleted Succesfully..."
cmdClear_Click
End If
End Sub

27. Double click the Edit button and enter the following code.

Private Sub cmdEdit_Click()


If (MsgBox("Are you sure to edit...", vbYesNo) = vbYes) Then
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset, adLockPessimistic,
adCmdText
rsEmp!Empcode = Trim(txtEmpCode.Text)
rsEmp!Ename = Trim(txtEname.Text)
If optMale.Value = True Then
rsEmp!Gender = "Male"
ElseIf optFemale.Value = True Then
rsEmp!Gender = "Female"
End If
rsEmp!Dob = dtpDob.Value
rsEmp!Desig = Trim(cboDesig.Text)
rsEmp!Dept = Trim(cboDept.Text)
rsEmp!Salary = Val(Trim(txtSalary.Text))
rsEmp.Update
connEmp.Execute "commit"
rsEmp.Close
Set rsEmp = Nothing
MsgBox "Edited Succesfully..."
cmdClear_Click
End If
End Sub
28. Run the program and enter the values.

29. Click the Add button, the program displays the following Message box.

30. To see whether the values are added in the database or not, view the rows by entering the
SQL, and type select * from employee ;, the SQL displays the added record values.
31. To edit any record, clear the values by clicking the clear button and enter the Employee
Code in the text box and click View. If the record is available in the database it will display
the following message box.

32. If you want to change any value, edit it and click Edit button, then it will display the
following confirmation Message box.

33. Click Yes to change and No to remain previous values. If the selection is Yes, it displays
the following Message box.

34. To delete any record, clear the values by clicking the clear button and enter the Employee
Code in the text box and click View. If the record is available in the database it will display
the following message box.

35. If you want to delete click Delete button, then it will display the following confirmation
Message box.
36. Click Yes to delete and No to retain the record. If the selection is Yes, it displays the
following Message box.

37. Save the Form.

38. Save the Project.


39. Select No for adding project to SourceSafe.

40. To create an Executable file for this Project, select File Æ Make prjEmployee.exe.

41. Select the path to store the Executable file and click OK.
Full Code

Dim connEmp As ADODB.Connection


Dim rsEmp As ADODB.Recordset

Private Sub cmdAdd_Click()


Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset,
adLockReadOnly, adCmdText
If rsEmp.RecordCount <> 0 Then
MsgBox "Employee Code Already Exists..."
rsEmp.Close
Set rsEmp = Nothing
Exit Sub
Else
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee", connEmp, adOpenKeyset,
adLockPessimistic, adCmdText
rsEmp.AddNew
rsEmp!Empcode = Trim(txtEmpCode.Text)
rsEmp!Ename = Trim(txtEname.Text)
If optMale.Value = True Then
rsEmp!Gender = "Male"
ElseIf optFemale.Value = True Then
rsEmp!Gender = "Female"
End If
rsEmp!Dob = dtpDob.Value
rsEmp!Desig = Trim(cboDesig.Text)
rsEmp!Dept = Trim(cboDept.Text)
rsEmp!Salary = Val(Trim(txtSalary.Text))
rsEmp.Update
connEmp.Execute "commit"
rsEmp.Close
Set rsEmp = Nothing
MsgBox "Added Succesfully..."
cmdClear_Click
End If
End Sub

Private Sub cmdClear_Click()


txtEmpCode.Text = ""
txtEname.Text = ""
optMale.Value = False
optFemale.Value = False
cboDesig.Text = ""
cboDept.Text = ""
txtSalary.Text = ""
End Sub
Private Sub cmdDelete_Click()
If (MsgBox("Are you sure to delete...", vbYesNo) = vbYes) Then
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset, adLockPessimistic,
adCmdText
rsEmp.Delete
connEmp.Execute "commit"
rsEmp.Close
Set rsEmp = Nothing
MsgBox "Deleted Succesfully..."
cmdClear_Click
End If
End Sub

Private Sub cmdEdit_Click()


If (MsgBox("Are you sure to edit...", vbYesNo) = vbYes) Then
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset, adLockPessimistic,
adCmdText
rsEmp!Empcode = Trim(txtEmpCode.Text)
rsEmp!Ename = Trim(txtEname.Text)
If optMale.Value = True Then
rsEmp!Gender = "Male"
ElseIf optFemale.Value = True Then
rsEmp!Gender = "Female"
End If
rsEmp!Dob = dtpDob.Value
rsEmp!Desig = Trim(cboDesig.Text)
rsEmp!Dept = Trim(cboDept.Text)
rsEmp!Salary = Val(Trim(txtSalary.Text))
rsEmp.Update
connEmp.Execute "commit"
rsEmp.Close
Set rsEmp = Nothing
MsgBox "Edited Succesfully..."
cmdClear_Click
End If
End Sub

Private Sub cmdExit_Click()


End
End Sub
Private Sub cmdView_Click()
Set rsEmp = New ADODB.Recordset
rsEmp.Open "select * from employee where empcode = '" &
txtEmpCode.Text & "'", connEmp, adOpenKeyset, adLockReadOnly,
adCmdText
If rsEmp.RecordCount <> 0 Then
txtEname.Text = Trim(rsEmp!Ename)
If Trim(rsEmp!Gender) = "Male" Then
optMale.Value = True
ElseIf Trim(rsEmp!Gender) = "Female" Then
optFemale.Value = True
End If
dtpDob.Value = rsEmp!Dob
cboDesig.Text = Trim(rsEmp!Desig)
cboDept.Text = Trim(rsEmp!Dept)
txtSalary.Text = Val(rsEmp!Salary)
MsgBox "Viewed Succesfully..."
Else
MsgBox "Employee Code Not Exists..."
End If
rsEmp.Close
Set rsEmp = Nothing
End Sub

Private Sub Form_Load()


Set connEmp = New ADODB.Connection
connEmp.Open "Provider=MSDAORA.1;Password=baba;User ID=baba;Data
Source=IBMREC;Persist Security Info=True"
connEmp.CursorLocation = adUseClient
MsgBox "Connection Established..."
cmdClear_Click
End Sub
More About Cursor Types

CursorTypeEnum Values

Constant Value Description


adOpenUnspecified -1 Does not specify the type of cursor.
adOpenForwardOnly 0 Default. Uses a forward-only cursor. Identical to a static
cursor, except that you can only scroll forward through
records. This improves performance when you need to make
only one pass through a Recordset.
adOpenKeyset 1 Uses a keyset cursor. Like a dynamic cursor, except that you
can't see records that other users add, although records that
other users delete are inaccessible from your Recordset. Data
changes by other users are still visible.
adOpenDynamic 2 Uses a dynamic cursor. Additions, changes, and deletions by
other users are visible, and all types of movement through the
Recordset are allowed, except for bookmarks, if the provider
doesn't support them.
adOpenStatic 3 Uses a static cursor. A static copy of a set of records that you
can use to find data or generate reports. Additions, changes,
or deletions by other users are not visible.

LockTypeEnum Values

Constant Value Description


adLockUnspecified -1 Unspecified type of lock. Clones inherits lock
type from the original Recordset.
adLockReadOnly 1 Read-only records
adLockPessimistic 2 Pessimistic locking, record by record. The
provider lock records immediately after editing
adLockOptimistic 3 Optimistic locking, record by record. The
provider lock records only when calling update
adLockBatchOptimistic 4 Optimistic batch updates. Required for batch
update mode

Vous aimerez peut-être aussi