Steps To Connect Visual Basic & Oracle: A Tutorial by B.Bhuvaneswaran
Steps To Connect Visual Basic & Oracle: A Tutorial by B.Bhuvaneswaran
A tutorial by B.BHUVANESWARAN
Lecturer / CSE Rajalakshmi Engineering College Thandalam, Chennai 602 105
Visit my site
www.bhuvangates.com
4. Select the Microsoft Visual Basic 6.0 from the Start menu.
6. Create the form with relevant controls. Control Text Box Text Box Option Button Option Button DTPicker Combo Box Combo Box Text Box Command Button Command Button Command Button Command Button Command Button Command Button Name txtEmpCode txtEname optMale optFemale dtpDob cboDesig cboDept txtSalary cmdAdd cmdView cmdEdit cmdDelete cmdClear 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.
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
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 where empcode = '" & txtEmpCode.Text & "'", 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
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.
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 where empcode = '" & txtEmpCode.Text & "'", 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 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