Access VBA Macro Programming

(Joao Candeias) #1
Creating a Simple Form

To create a form, click the Create tab in the ribbon and then click the Form Design icon in
the Forms group of the ribbon. This will give you a blank form. You can resize the form by
placing the mouse on the right-hand or bottom border and dragging the border to where you
need it.
To give the form a name, click the Save icon in the toolbar or pressCTRL+S. Give the form
a name such asfrmMyForm.
You now need to link the form to a table or query as a data source. To do this, right-click
anywhere on the form and select Properties in the pop-up. At the top of the Properties
window is a drop-down showing Selection Type. Select Form in this drop-down.
Choose the All tab on the Properties window and go to the first property shown, called
Record Source. The drop-down on this property will display a list of all tables and queries in
the Access database. You can select one of these as the source for the form, or you can enter
in a SQL query string such asselect * from orders. If you click the button with the three
dots next to the drop-down, you can use the query builder window to create a query for your
form.
For the purposes of this example, select Orders as the record source. This form will be
referred to later in this chapter as the Orders form.
Now that you have a data source for the form, you can add controls and bind them to that
data source. Click the Design tab in the ribbon if it is not already selected and then select a
control from the Controls group of the ribbon. The text box control is one of the most widely
used, so try this one initially. Drag it on to the form. You will notice that it automatically adds
in a label box so you can enter a heading to describe the text box. Your form should now look
like Figure 9-1.
If you want to delete either the label or the text box, simply select it with the mouse and
press theDELETEkey. You can also drag the text box to a new position (the accompanying
label box will follow as well), and you can resize both the controls by selecting the handles
on a particular control and dragging the borders to the size you want.
Once you have the text box as you want it, you can bind it to a field within the record
source. To do this, right-click the control and click Properties in the pop-up. In the Properties
window, make sure the All tab is selected, and then select Control Source in the properties
list. Click the drop-down and a list of fields will be displayed according to what was entered
as the Record Source property for the form.
Select the field you want to display in the text box and this will bind the field to that
text box. Select a field that will display as text, not an ID number relating to another table.
Choose fields like Ship Name or Ship City, not Employee ID or Customer ID since these are
relational fields.
Repeat this process for some other fields so you have a form that has a reasonable population.

98 Microsoft Access 2010 VBA Macro Programming

Free download pdf