Project 5: Accessing Databases
Objectives
After completing this lab, you will be able to:
Use the Data Form Wizard to create forms.w
Create an SQL statement.w
Work with the Data control.
Exercises
Exercise 1: Creating a Customer Information Form with the Data Form Wizard
In this exercise, you will use the Data Form Wizard to create a form that will display customer information.
Exercise 2: Creating an Order Information Form with the Data Form Wizard
In this exercise, you will use the Data Form Wizard to design a form that will display order information. Then, you will add a command button to the frmCustomers form to show the FrmOrders form.
Exercise 3: Creating an SQL Statement
In this exercise, you will link the two forms together so that only the orders for a selected customer are displayed in the FrmOrders form.
Exercise 4: Reviewing Code Created by the Data Form Wizard
In this exercise, you will review the code created by the Data Form Wizard.
Exercise 1: Creating a Customer Information Form with the Data Form Wizard
In this exercise, you will use the Data Form Wizard to create a form that will display customer information.
Ø Start a new projectStart a new Visual Basic project.
If the Data Form Wizard is not a menu item on the Add-Ins menu, use the Add-In Manager to load the Data Form Wizard add-in:
Ø Install the Data Form Wizard add-in1. On the Add-Ins menu, click Add-In Manager.
2. Select Data Form Wizard in the Available Add-Ins list., and click OK.
Ø Build the customer information form
1. On the Add-Ins menu, click Data Form Wizard.
2. Click Next on the Data Form Wizard introduction screen.
(if asked to load a profile for your settings, select (none) and click next).
3. Select Access from the list of database types, and click Next.
4. Browse for the Nwind.mdb database (most likely in c:\Program Files\Microsoft Visual Studio\Vb98) and click Next.
(if you can't find Nwind.mdb in the default directory, use the 'Find' command from Windows Explorer).
5. Name the form: frmCustomers. Select Single record and make sure ADO Data Control is selected, click Next.
6. In the Record Source drop-down list box, select Customers.
7. Copy the Customer ID and the Company Name fields from the Available Fields list to the Selected Fields list, and click Next.
8. Click Select All and then click Next.
9. Leave the default profile name of None, and click Finish.
10. Click OK on the Data Form Created dialog box.
In this application, we will not be using the default form that Visual Basic created.
Ø Remove the default form and set the startup form1. In the Project Explorer window, select Form1.
2. On the Project menu, click Remove Form1.
3. On the Project menu, click Project1 Properties.
4. Set the Startup Object to the frmCustomers form you just created, and then click OK.
Ø Save and test your work
1. Save the customer form by selecting Save frmCustomers As on the File menu.
2. Save the project by selecting Save Project As on the File menu.
3. Test your application. Use the forward and backward buttons on the Data control to cycle through some records in the database. Change some fields of the database and click the Update button. Click Close to end the application.
Exercise 2: Creating an Order Information Form with the Data Form Wizard
In this exercise, you will use the Data Form Wizard to design a form that will display order information. Then, you will add a command button to the frmCustomers form to show the frmOrders form.
Ø Build the order information form1. Use the Data Form Wizard to access the Nwind.mdb database table 'Orders'.
2. Name the form: frmOrders. Instead of using the 'Single Record' form layout, choose Grid (Datasheet) for this form.
3. Copy the Customer ID, Order ID, Order Date, and Required Date fields from the Available Fields list to the Selected Fields list, and click Next.
4. Leave the default profile name of None, and click Finish.
Ø Display the frmOrders form from the frmCustomers form
1. Widen the frmCustomers form and add an Orders command button:

2. In the Click event of the Orders command button, display the frmOrders form as modal:
frmOrders.Show vbModal
Ø Save and test your work
1. Save the frmOrders form by clicking Save frmOrders As on the File menu.
2. Run the application. Click the Orders button, and scroll through some of the records in the Orders table and try resizing the form. Click Close on the FrmOrders form to return to the frmCustomers form.
3. Close the running application.
Exercise 3: Creating a SQL Statement
In this exercise, you will link the two forms together so that only the orders for a selected customer are displayed in the frmOrders form.
Ø Customize the frmOrders form1. Open the Code Editor window for frmOrders.
2. In the General Declarations section, create a Public String variable, gstrCustomerID.
This variable will be set from the frmCustomers form.
3. In the Form_Load event procedure for frmOrders:
a. Create an SQL string that selects all fields from the Orders table, where CustomerID is equal to the gstrCustomerID variable.
Dim strSQL As String
strSQL = "SELECT * FROM Orders " & _
"WHERE CustomerID = '" & gstrCustomerID & "'"
b. Set the RecordSource property of the Data control to the SQL string.
datPrimaryRS.RecordSource = strSQL
c. Refresh the Data control.
datPrimaryRS.Refresh
Ø Customize the frmCustomers form
1. View the frmCustomers form.
2. Edit the Click event procedure for the Orders button.
3. Set the Public variable in frmOrders, gstrCustomerID, equal to the current text in the Customer ID field before showing the frmOrders form:
frmOrders.gstrCustomerID = txtFields(0).Text
Note
The Data Form Wizard assigns names to the controls it creates. Therefore, the names on your forms may be different from the names in the solution.Ø Save and test your work
1. Save your project.
2. Run your application. Cycle through a few of the companies, clicking Orders. Are all of the records displayed in the frmOrders form for the selected company? Click Close on the frmOrders form to return to the frmCustomers form.
3. Close the running application.
Exercise 4: Reviewing Code Created by the Data Form Wizard
The Data Form Wizard adds code as well as controls to the form. In this exercise, you will review the code created by the Data Form Wizard.
Ø Review the code that is generated by the Data Form Wizard1. Look at the code for the Add, Delete, Refresh, Update, and Close buttons.
2. Look at the code written for the Data control event procedures: Error, Reposition and Validate.
Ø Add a Find command to the frmCustomers form
1. Add a Find command button to frmCustomer:

2. In the Click event of the Find command button:
a. Declare two string variables: one to store the customer name, the second to store a bookmark.
Use the InputBox function to ask the user for a customer name to locate (this should be stored in the customer name variable) .
The bookmark variable should contain code that resembles the following:
strBookmark = datPrimaryRS.Recordset.Bookmark
b. Call the Findmethod of the Data control.
datPrimaryRS.Recordset.Find
"CompanyName Like " & _
Chr(39) & strCustomerName
& "*" & Chr(39)
c. If the Find method fails, reset the Data control to the record it was positioned on before executing the method.
If datPrimaryRS.Recordset.NoMatch Then
datPrimaryRS.Recordset.Bookmark = strBookmark
MsgBox strCustomerName & " not found."
End If