You are on page 1of 20

Steps to Connect Visual Basic & Oracle 1.

Select SQL Plus from the Start Programs Oracle-OraClient10g_home1 Application Development SQL Plus.

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 Programs Microsoft Visual Studio 6.0 Microsoft Visual Basic 6.0.

5. From the list of Projects select the Project type as Standard EXE and click Open.

6. Create the form with the following controls and assign the given for Naming Convention. 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.

8. Now the form look like as below:

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 it 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 (based on your institution configuration). Make the Allow Saving Password box as checked and click Test Connection.

17. If everything is correct, 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 below:

22. Run the program by clicking the Start button (or) by pressing F5 button. If your code is correct, then 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 get stored in the database or not, enter into the SQL, and type select * from employee ;, the SQL displays the inserted record values.

31. To edit any record values, 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 modify any value, chagne it and click Edit button, then it will display the following confirmation Message box.

33. Click Yes to change or No to retain the 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 or No to retain the record. If the selection is Yes, it displays the following Message box.

37. Save the Form with the name frmEmployee.

38. Save the Project with the name prjEmployee.

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.

Complete 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 adOpenUnspecified adOpenForwardOnly Value -1 0 Description Does not specify the type of cursor. 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. 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. 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. 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 adLockUnspecified adLockReadOnly adLockPessimistic Value -1 1 2 Description Unspecified type of lock. Clones inherits lock type from the original Recordset. Read-only records Pessimistic locking, record by record. The provider lock records immediately after editing Optimistic locking, record by record. The provider lock records only when calling update Optimistic batch updates. Required for batch update mode

adOpenKeyset

adOpenDynamic

adOpenStatic

adLockOptimistic

adLockBatchOptimistic