3 Forms
Up

Create a form with autoform

Now that you've created a table with all the information you need, you're ready to create a form. The easiest way to do this is to highlight tblLeads, and then click the New Object > AutoForm, as shown in Figure 4-1.

Figure 4-1: AutoForm.
Figure 4-1: AutoForm.

In Figure 4-1, you also see a list of the New Objects available to you. They are as follows:

bulletAutoForm
bulletAutoReport
bulletTable
bulletQuery
bulletForm
bulletReport
bulletPage
bulletMacro
bulletModule
bulletClass Module

AutoForm automatically creates a form, always in the same from top to bottom format. Form Wizard (found on the Forms tab of your database window) can do a slightly better job, but in general, forms are always time consuming because of the careful layout needed.

AutoReport automatically creates a report, always in the same from top to bottom format. Another option is to use Report Wizard, which is covered in Lesson 5.

The next five choices, Table, Query, Form, Report, and Page, bring up a choice of how to create a new table, query, form, report, or page. The same results can be achieved by clicking New on the corresponding tab of your database window.

These other three choices take you right into a new macro, module, or class module. The same results can be achieved by clicking New on the Macros or Modules tab of your database window.

If you get stuck in any of the areas while experimenting, just close the object you've created and answer no if Microsoft Access asks you to save the object.

Form view

Now, granted, AutoForm produces a fairly ugly form. But it's just a starting point. Click the Save button, or choose File > Save from the menu. Access defaults to naming the form the same as the table it was created from, but you're going to call this form frmLeads.

Notice the frm indicates this is a form just as the tbl in tblLeads indicates it's a table.

Figure 4-2 shows the plain form and at the top of the form, you see the name, tblLeads, which Access used as the source. This and most other items discussed here can be edited in Design view.


Figure 4-2: Form view.

Notice that above the form are also some standard buttons you're familiar with. Remember, just hold your mouse over the button to get its name. In this view, there are two a new buttons: Properties and Database Window. The Properties button is a toggle button to show or hide the Properties window. (The Properties window is covered in detail in the section on Design view.) The Database Window button brings the database window to the front.

Notice that when you click the Database Window button, this toolbar disappears. It's only visible in Form view. Each toolbar is customized for usefulness.

The toolbar directly above that one (yours may be reversed) is the Formatting toolbar. All of these buttons should be familiar to you and if not, try them to see what they do. Note that you can only format the entire field in Access, not one field at a time. And formatting affects the field, not the data in the field. Therefore, if you bold the phone number field, everyone's phone number is bold.

Looking at the form itself, the first field listed is the Autonumber field. As you tab through all the field names from the table you entered, you see that Autonumber changes to the next number. It's 1 if you haven't entered any data and some higher number if you have. AutoForm has arranged the fields from top to bottom, but you can move them around in Design view.

The tab key allows you to move from field to field. That's a handy feature, but if you move the fields around, the tab order won't change unless you change it. At the end of the form, the tab key goes to the next record.

TIP
Pressing the Shift+Tab keys allows you to move through the tab fields in reverse.

The scroll bars on the right allow you to move up and down. In Form view, you only see one record at a time, whereas in Datasheet view you see all of the records, as if looking at a Microsoft Excel file.

The Navigation buttons at the bottom work just as they do in the Datasheet view. They allow you to move back and forth through the records and create new records by clicking the arrow and asterisk button.

To clean up the form, you use Design view. This is a large topic and is covered in the following section, but before moving on, a few words about the other auto objects available to you in Access.

Notice that you have three views available to you in the Forms area. At the top left, same place as in the tables, there's a Design View, a Datasheet View, and a Form View. The Datasheet view just reemphasizes that the form is just a face for the data. All data resides in the table. If data is changed in the form, it changes in the underlying table. Click Datasheet View to see a view that looks just like Datasheet view in the tables.

When you're finished experimenting, click Design View to move on.

Forms in design view

There's a lot to see in the Design view, so take it one step at a time. First, if you'll look at Figure 4-3, you'll see a few other windows floating around on the screen. On the bottom right is a field list. This lists all the fields available to you for use in this form. On the top right is the properties box mentioned in Form view. To the left, you'll see the Toolbox; this has a variety of options for you to add objects to the form.


Figure 4-3: Design view of a form.

In Design view, you'll find that you can move any of the fields around anywhere you like.

TIP
As you start to experiment, keep in mind that you can always close the form without saving your work if you make a mistake.

Toolbar

As mentioned earlier, the toolbars are different, depending on where you are in Access. In the Design view of a form, you have the same Formatting toolbar as you did in the Form view.

The other toolbar is also the same as the Form view with the exception of a few buttons. Move your mouse over the items near the middle of the bar to find the Field List and the Toolbox buttons. These are toggles that turn the windows on and off.

The next button, Autoformat, gives you a choice of a few designs that you can apply to your form. If you want it to apply to the entire form, make sure the black dot is visible in the top-left corner of the form window (this is the intersection of the vertical and horizontal rulers). This black dot indicates that the entire form is selected. Otherwise, Autoformat only applies to selected objects.

The next button is the Code button. It opens a separate window. If you click this, you won't see code at this point. We cover code in the following section.

The next button is the Toggle for the Properties window. Next is the Build button. This is an advanced feature for using Macros, Code, or Expressions. The last three are the same as in the Form view, Database Window, new object, and help.

The form

Click any field on the form. When you click any field, you see that it's identified as selected by little black squares, also call handles, around it. Figure 4-4 shows multiple selections. You can make multiple selections by clicking an area without a field, and then dragging the mouse across the area you want to select.

Figure 4-4: Multiple objects selected.
Figure 4-4: Multiple objects selected.

Notice in Figure 4-4, that there are handles completely around both the fields and the labels to the left of each field from the top, down through Contact First Name, but below that, only the field has handles and the label does not.

After selecting these items, click Bold on the Formatting toolbar. Notice that the formatting only applies to the objects that have handles all the way around them.

Now that you have a selection, move your mouse over the selected fields. The mouse pointer changes to the shape of a hand. If you click and drag your mouse when the pointer is shaped like a hand, you can move selected objects.

Notice that the labels that are not selected still move with the group. That's because labels associated with fields, always move with the field, unless you move your mouse to the top-left corner of the field or label. When you do this, you should notice a slightly larger handle -- even on the labels not selected.

When you place your mouse over this handle, the mouse pointer changes to a hand with just one finger sticking up. This is your indication that clicking and dragging results in just that object moving, whether it's a label or a field.

Try selecting and moving objects around on the form. You can use undo to step back, or close the form without saving if you just want to start over.

Field list

The field list is easy to use. Simply click and drag the field you want to add to the form to the location on the form you want it. Although this allows you to put multiple fields of the same name on the form, you can tell when there's an existing field of the same name because the accompanying label is something generic, such as Text74, rather than the name of the field as it normally is.

TIP
Instead of using AutoForm, you can create a form from scratch. This is often preferable when there are only a few fields on the form. In the final section of this lesson, you'll create a form from scratch.

Toolbox

The toolbox is a collection of tools that is just the tip of the iceberg. More advanced features are available when you later need them. You'll use a few when creating a form from scratch. The toolbox is actually just another toolbar. It can be docked or float around, as all toolbars can. If you move your mouse over the buttons, you will see the button names.

Properties

This is the most important area of the form. All of your properties can be set here and it changes as you click different items, so keep an eye on where you are.

Again, be sure the black dot is visible in the top-left corner. If it is, the properties box should say Form.

In the Properties window, there are five tabs across the top:

bulletFormat: This tab is similar to the Format toolbar, but includes additional properties that are unique to the selected object. Most are self-explanatory. If not, simply change one, and then view the form in Form view to see the change.
bulletData: This tab controls what data feeds the object and how it can be adjusted or controlled. More advanced features include calculated fields, using formulas similar to Microsoft Excel, as well as form level validation rules. The label field does not have any properties on this tab, as it's not data controlled. Only the form and the text fields have properties on this tab.
bulletEvent: This tab includes the most powerful feature of forms. For each object, there is a collection of events. Examples include, Click, Double-Click, On Change, On Enter, On Exit, On Update, and others. You can use any of these events, to cause an action to occur. This can include checking data, updating data in this or other fields, or anything you can think of.
bulletOther: This tab is a catchall for certain items that don't fall under the preceding categories, including tab stops. You can see what order the field will be tabbed to and whether the cursor will stop during tabbing on that field.

TIP
Another place to view all tab stops is under View > Tab Order.

bulletAll: As it says, all the fields are listed in one place, but because they're not alphabetical, you may not find the tab any easier to use than the others.

As you click each one, you see a list of different properties relating to whatever is selected at the moment. Each object on the form has properties, as well as the form itself. Be sure to click different objects and then view the tabs.

TIP
A quick way to get the Properties window up is to double-click any object.

As you click each property, you'll see a description in the lower-left corner of the screen. Don't be afraid to experiment. You can't break Access and if you make a mistake, undo. If you make too many mistakes, just close without saving.

Add a button - look at code

In the previous section, you created an AutoForm. Now you're going to create a form from scratch. This is going to be your menu form. You can add all your features here and activate them as you finish that part of the database.

To create the form, follow these steps:

  1. If you haven't already, close frmLeads. The database window should now be visible.
  2. Select Forms and you should see your lone form: frmLeads.
  3. Click New > Design View, as shown in Figure 4-5, and then click OK. You'll see a blank empty form.


Figure 4-5: New Form, Design View.

  1. Click Save, and then name the form frmMenu.

To modify the form, follow these steps:

  1. Double-click anywhere in the grey background to open the Properties menu for the form detail section.
  2. Click the Format tab, and then click in the Back Color property.
  3. Click the Build button (...) to the right to open a palette of colors. Choose your color, and then click OK.
  4. On the Toolbox, click the label button (the button with the Aa near the top- left corner). Your cursor changes to crosshairs with a capital A. Click anywhere on your form, and then type Main Menu.
  5. Press Enter.

You now have a custom form. It doesn't do anything yet, but it's starting to look pretty. While your label (Main Menu) is still selected, format it as you wish, changing the font, size, color, and so on, as you would do in Microsoft Word. If the words become bigger than the label, double-click any of the black handles to automatically resize the label.

Now you're going to add a button to your form. Follow these steps:

  1. On the Toolbox, make sure the Control Wizards button at the top is selected. This bring up a wizard on certain types of tools.

Control Wizards button.

  1. Click the Command button on the middle right, and then click anywhere in the form. The Command Button Wizard appears, as shown in Figure 4-6.

Command button.

Figure 4-6: Command Button Wizard.
Figure 4-6: Command Button Wizard.

  1. From the Categories list, choose Form Operations, and then on the right side, choose Open Form. Click Next.
  2. A list of your forms appears. Click frmLeads, and then click Next.
  3. Leave Show all records selected, and then click Next.
  4. Click Text, rather than Picture and change the text from Open Form (not very descriptive) to Enter Leads. Click Next.
  5. Rather than Command1, rename the button cmdLeads. The cmd stands for Command button, so that you'll recognize what control it is when you have multiple controls.
  6. Click Finish.

Add a second Command button, but this time, just click Cancel when the wizard comes up. On the Other tab of the properties, change the name of this button from Command2 to cmdLeadReport.

Double-click the button, and then change the text there from Command2 to Lead Report. You can place the buttons anywhere you want on the form.

Look at the code

Now we're going to do something that some users might find a little scary. We're going to look at the code for this form. Click your Enter Leads button, and then click the Event tab of the Properties window. Notice that On Click Event has an [Event Procedure] associated with it.

If you now click your Lead Report button, you won't see anything on the Event tab. This is because the Command Button Wizard created code for you on the Enter Leads button.

Now you'll create the code for your Lead Report button:

  1. In the properties window for the Lead Report button, make sure the Events tab is selected.
  2. Click the On Click property, and then click the Build button ([. . . ]) to the right.
  3. Choose Code Builder, and then click OK.
  4. This brings up a Visual Basic window, shown in Figure 4-7. Access actually uses VBA (Visual Basic for Applications), which is quite similar, but not identical to VB (Visual Basic).


Figure 4-7: Visual Basic window.

  1. Looking at Figure 4-7, you see that Access created two lines of code for you and left your cursor in the middle. Since you want this button to open a report (which you haven't created yet), enter the following code:
DoCmd.OpenReport "rptLeadsReport", acViewPreview

Good programming technique requires that you tab in one time to indent the code for easier reading. Below your code, you can see the code the wizard created for your other button.

As you read through, you'll see a line similar to the one you just entered. It includes DoCmd.OpenForm, but there are three extra lines of code for options and six extra lines of code for error handling. This is just the way the Wizard creates code. If you learn a bit of code, you can streamline your programming.

Close this window, save frmMenu, and then view it in Form view and test your Enter Leads button. You'll get an error on your Lead Report Button because there's no Lead Report yet.

A few clean up things to make your form look better include modifying the form properties to turn off the Scroll Bars, Record Selectors, Navigation Buttons, and Dividing Lines. Figure 4-8 shows a finished menu.

Figure 4-8: A finished menu.
Figure 4-8: A finished menu.

Leads Program is set in the Caption property of the Form. Experiment and add buttons for future use. Practice makes perfect!

Moving on

In this lesson, you created a new form using AutoForm and then modified it to your needs and preferences. During this process, you also created a Main Menu form from scratch and viewed the code module associated with the form along with writing a line of code that is useful to forms. You can continue to build forms as you need to add functionality to your database.

In Lesson 5, you'll learn the power of queries and reports. Getting data out is usually the reason for having a database in the first place.

 

Assignment

Practice building a form

Create a new form using AutoForm, and then go to design mode and add a button to close the form. The code is:

me.close  

Practice modifying your existing forms.

Quiz

Question 1
True or False: Forms are required in Microsoft Access.

A) True
B) False

Question 2

True or False: In forms, Form view and Datasheet view are the same.

A) True
B) False

Question 3

Select multiple fields in form design view by pressing and holding the _______ as you click fields.

A) Ctrl key
B) mouse buttons
C) Shift key
D) Esc key

Question 4

True or False: Fields and their labels move together as one when you use the mouse cursor that looks like an open hand.

A) True
B) False

Question 5
True or False: Automatically generated code is more efficient than manually generated code.

A) True
B) False

Previous Up Next