0% found this document useful (0 votes)
126 views18 pages

Steps To Connect Visual Basic & Oracle: A Tutorial by B.Bhuvaneswaran

1. The document provides steps to connect Visual Basic to Oracle by using ADO to access an Oracle database and perform CRUD operations. 2. It describes creating controls on a form, connecting to Oracle using a connection string, and writing code to add, view, edit, and delete records using ADO and SQL. 3. Example code is provided to open a connection, retrieve and update records using ADO, and display messages to confirm operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views18 pages

Steps To Connect Visual Basic & Oracle: A Tutorial by B.Bhuvaneswaran

1. The document provides steps to connect Visual Basic to Oracle by using ADO to access an Oracle database and perform CRUD operations. 2. It describes creating controls on a form, connecting to Oracle using a connection string, and writing code to add, view, edit, and delete records using ADO and SQL. 3. Example code is provided to open a connection, retrieve and update records using ADO, and display messages to confirm operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Steps to Connect Visual Basic & Oracle

A tutorial by B.BHUVANESWARAN
Lecturer / CSE Rajalakshmi Engineering College Thandalam, Chennai 602 105

Mail your suggestion to


[email protected], [email protected], [email protected]

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).

Visit www.bhuvangates.com for more tutorials

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

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

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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

Visit www.bhuvangates.com for more tutorials

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

Visit www.bhuvangates.com for more tutorials

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

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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.

Visit www.bhuvangates.com for more tutorials

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

Visit www.bhuvangates.com for more tutorials

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

Visit www.bhuvangates.com for more tutorials

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

Visit www.bhuvangates.com for more tutorials

You might also like