Chapter One: Building the Basic Search Form
©Conrad Muller 2005
Build a working form.
In this chapter you will build the basic form by following these steps:
- Make a copy of the database file memberdata.mdb and name it member.mdb.
- Create an SQL query in the new database file.
- Build and modify a form based on the query.
- In the next chapter we will add the search Text Box.
A convention is a set of rules that are used voluntarily. You should use these standardized naming conventions for programming in all Versions of Visual Basic. Using standard naming will make your code easier to write, maintain, and debug.
We will start with naming conventions for some database objects:
- No spaces in names.
- Only letters, numbers, $ and the underscore character _.
- Always begin the name with a letter.
- Tables, queries, forms, and controls should never have plural names.
Make a copy of an Access database file.
- Open the Windows Explorer.
- Find the Examples folder on the CDROM that came with this book, and drag the folder to a convenient location on your working drive. C:\My Documents\examples is a possibility.
- After dragging the folder to your working drive, all of the files will be Read-only. To remove the Read-only attribute, select the files you wish to change.
- Then select Files/Properties and uncheck Read-only in the File Properties dialog box. If you have selected more than one file, you will be changing all that you selected.
- In the folder Examples select memberdata.mdb. Hold down the Ctrl key and use the mouse to drag the file to an empty space in the folder's window.
- You should now have a new file named Copy of memberdata.mdb. Use a right mouse click context menu to rename the file member.mdb.
Inspect the new Access database file.
- Double-click on the filename member.mdb to open it in Microsoft Access.
- Look at the Tables, Queries, Forms, Reports, Macros, and Modules. You should find only one table, tbl_member, and no other objects.
- Open the table by double-clicking on it. Use the scroll bars to look around. Notice the record navigation buttons in the lower left of the form, and the record count.
- Now close the table, but not the Database Window.
Create an SQL query to retrieve the data for our form.
- Click on the Queries button of the Database Window.
- Double-click the entry Create Query in design view.
- Use the Show Table dialog box to add table member to the top pane of the Query Builder, and then close the Show Table dialog box.
- Add the columns we will need in our form to the query grid: Member_id, FirstName, LastName, AreaCode, Phone, Email, City, PaidUntil, and Board.
- Choose to Sort Ascending on the LastName column, as in column three of the Query Builder picture above.
- Save the query as qsel_search.
- Use the View button, all the way on the left of the Tool Bar, to see the each of the three views of the query: datasheet view, design view, and SQL view.
- The Design View is on the facing page. The Datasheet View and the SQL View are below.
- Close the Query when you are sure it works correctly.
This is the Datasheet View, showing the results of running the query.
This is the SQL view, showing the actual code that was written by the Query Builder and sent to the database server for execution.
Create a form based on the query.
- Click the Forms button in the database window.
- Double-click Create form by using wizard.
- Be certain that Query: qsel_search is selected in the first Form Wizard dialog box.
- Use the "fast forward" button to move all of the fields to the Selected Fields pane. Click Next.
- Select a tabular layout, and then click Next.
- Choose the Standard style, and then click Next.
- Name the form frm_search, and then click Finish.
- The resulting form is truly ugly, but we can fix it up.
Fix up the form generated by the Form Wizard.
- Right mouse-click on the blue bar at the top of the form. Choose Form Design from the top of the context menu that appears.
- Make the Form Header about ?" high.
- Move the labels to the very bottom of the header.
- Move the Text Boxes to the very top of the Detail section.
- Adjust all of the Text Boxes to have a height of 0.1771" and Scroll Bars = None.
- Move the bottom of the Detail Section up, so there is no space beneath the Text Boxes in the Detail Section.
- Make all of the Text Box Widths fit the data they have to display. You will have to switch between Design View and Form View as you adjust widths to fit. Do not leave space between Text Boxes. Use the picture below as a guide.
- Create a ?" Form Footer, and line up the Labels with the Text Boxes. Save your work, and take a look at your form in Form View.
Name all of the objects on the form.
- Put the form back into Design View.
- Open the Properties list dialog box, and click the Other tab.
- Click the member_id Text Box.
- Set properties for the member_id textbox control.
- Name: txt_member_id
- Tab Stop: No.
- Rename all of the Text boxes by putting "txt" at the beginning of each name, and set Tab Stop: No.
- Name the Board Check Box by adding chk to the beginning of the name, to make it chk_board, and set Tab Stop: No.
- Don't miss setting the Name property, and setting Tab Stop to No, on any detail section control.
- Now rename the Labels in the Form Header. They will look like member_id_Label. Remove the Label, and add lbl to the beginning of the name. For example lbl_member_id.
|Hint: Once the Properties List dialog box is open, click on any form object to display its properties. Just leave the Properties List open while you work on the form.|
Lock all of the Text Boxes on the form.
- Put the form into Design View again.
- Open the Properties list, and click the Data ?tab.
- Click the txt_member_id Text Box.
- Set Locked: Yes
- Lock all of the other Text Boxes and the Check Box.
- Save your work. Now, go to Form View and take a look.
- Notice the Title Bar at the top of the form. It would be nice if the Form Caption said something more interesting than frm_search. It would also be nice to remove the three small buttons on the right end of the blue Title Bar.
- We will also remove the navigation buttons and a scroll bar from the bottom of the form. We aren't going to need any of that.
- Before I forget, we will need some way to close the form after we remove the buttons on the top right. We'll add a Close button next.
- Change the form to Design View. Be sure the Wizard button on the Toolbox is depressed (turned on). The Wizard button icon is a magic wand.
- Click on the Command Button button. Draw a button on the far right of the Form Footer. The Command Button Wizard should start.
- In the left pane of the dialog box choose Form Operation. In the right pane of the dialog box choose Close Form. Click the Next button.
- Choose Text and make the Text simply say Close. Click Next.
- Name the control cmd_close and click Finish. Save your work.
- Change to Form View, and try your Close button.
Clean up the form by setting Form Properties.
- Change back to Design View. Open the Properties List.
- If there isn't a black dot in the button on the top left of the form window, click on the button.
- This causes the properties of the form object (overall form properties) to be displayed in the Properties List dialog box.
Set the Format properties for frm_search.
Select the Format tab of the properties sheet. Change these properties:
- Caption: Member Search Form
- Default View: Continuous Forms
- Scroll Bars: Vertical Only
- Record Selectors: No
- Navigation Buttons: No
- Auto Resize: No
- Border Style: Thin
- Control Box: No
- Min Max Buttons: None
- Close Button: No
Set the Data properties for frm_search.
Select the Data tab. Change this property: Allow Additions: No
Note: We have changed the Form Border to Thin, and Auto Resize to No so that the form will always appear in the size we have planned.
We have removed all of the default navigation and control buttons, because we are going to provide all of the control the user will need, and it would be distracting to give the user so many choices.
We have disabled editing on this form, because this form is used only for searching, and because some of the fields are going to be hyperlinks, and hyperlinks should not be edited.
This is our form, ready to add the Search Text Box in the next chapter.
Try to make your form look as much like this one as you can. We will be adding a lot of functionality to this form, and it should be as attractive as it is functional.
All of Conrad Muller's work on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 2.5 License.