Sie sind auf Seite 1von 6

MS ACCESS – FORMS

EXERCISE – 2A

CREATION OF FORMS

For any Business Application the first step is to determine the Master tables and Transaction
tables. Master tables contain relatively permanent data while Transaction tables capture data
regarding business events – typically interactions between elements of the Master tables. For e.g.
in a University context, Master tables are maintained for students, faculty, departments, courses,
subjects, etc., while Transaction tables will be for admissions, course registration, marks etc. In a
Retail Store, Master tables will be for customers, suppliers, employees, products, etc., while
Transaction tables are for purchase, sales, hiring, etc. In a Library, Master tables are for books,
magazines, members, assets, etc., while Transactions are for borrowing, returning, loss of book,
purchases, etc.

EXAMPLE OF A LIBRARY DATABASE

Create the following Master tables:

MEMBERS BOOKS

Field Type Field Type


Memberno Text Accno Text
Name Text Title Text
Class Text Author Text
Department Text Classification Text

And the following Transaction table

Field Type
Transactno Auto number
Memberno Text
Accno Text
Issue date Date
Due date Date
Return date Date

Though data can be entered directly into the tables, you should keep in mind, that especially for
Transact tables, the sheer volume of data entry necessitates the utilisation of all the ways and
means to:

 Reduce the monotony,


 Avoid entering data that have already been entered somewhere else,
 Also give ample scope for cross verification to avoid error and
 In fact, reduce typing to a minimum

We will start with entering data directly into the Master tables; a few rows in each. (That’s your
homework – get going).

Now we will create a form for transact table and see how we can make data entry a breeze……

1
Go to forms → new → design view and from the drop down, select the Transact table (so that
the form is now based on the Transact table; in other words the data from the form goes to the
Transact table).

As the form opens u will see a box with all the fields of the Transact table listed there. (If not
visible go to ‘view’ and click on ‘field list’; if still not seen, then it means u had not selected the
table in the previous step. So go back and start all over again!!)

Now immediately click on the save button (floppy symbol). Respond to the dialog box by typing
the name of the form.

Now select all the fields of the field list and drag it on to the form. Click on the form view and
see how the form looks. U will see that there is not much difference between this and directly
entering into the table.

Come back to the design view. First we will see how to avoid typing the accno value. We will
make use of the fact that the data for all accno is already present in the Books table. The combo
box control comes in handy here.

Right click on the ‘accno’ field and click on the ‘change to item’ in the context menu that pops
up and further click on the ‘combo box’. This will convert the text box into a combo box. Now
we will have to tell the combo box where to get the data from; i.e. the combo box should get the
data from the Books table which is the master for all books.

Now right click the combo box and select ‘properties’. Here go to the ‘data tab’ and go to ‘row
source’ field. There in the dropdown, select the ‘Books’ table. We have now linked the combo
box to the Books table.

Go to the form view and see for yourselves that the accno field has metamorphosised to a combo
box and on drop down, shows all the accnos from the Books table. All we got to do is to click!!

Go back to the design view and do the same to the member no field, linking it to the Members
table. Go to the form view and check again.

The issue date has got to be today. Why should we enter it in that field? Can we avoid it?
Certainly. Go back to the design view. Right click the ‘issue date’ field; go to ‘data tab’ and go
to the ‘default value’. Here click on the ellipsis at the end of the box (three dots). This throws up
the expression builder.

Go to the ‘common expressions’…the middle pane will show the ‘current date’. Double click it.
Click OK and close the dialog box. Now see the effect in the form view. Remember the effect
will take place for new records that u enter and not on the records u have already entered. So u
have to click on the new record button at the bottom of the window.

U can repeat the same for the due date field …here the default value will be ‘current date +7’.

2
MS ACCESS – FORMS

EXERCISE – 2B

AUTOMATING THE FORMS

Usually in a library, this is not the way u enter accno or member no as there will be 1000s of
them and a combo is not the exact way to handle them.

So now we will modify the form to behave in a diff way. We want the form to show the title of
the book when I enter the accno by manual typing. (similarly for member no it should show the
member name). But there is a problem. The form has included fields only from the Transact
table which do not contain the fields like title and name.

For that, we create a query which will include all fields from the Transact table and also from the
Books table and the Members table. Go ahead and create the query. Select all fields from the
Transact table and only the required fields from the Books and Members tables. Now recreate the
old form. This time the form will be based on the query and not on the Transact table.

Go to the form view. Go to new record. Enter the member no … there comes the name of the
member because we had linked the tables in the query and so it knows!!

One more thing – if u want to see the data entry as the old datasheet way, this is still possible by
going to the ‘form properties’ (for ‘form properties’ right click at the top left corner black box of
the form) and under the ‘format’ tab select ‘datasheet view’ from the dropdown. And if u feel
irritated that the form shows all the records every time it opens and u want that it directly goes to
the last record so that u can straight away start entering new data, a small tweak of the form
properties will achieve this. U have to set the ‘data entry’ field under ‘data’ tab of the form
properties to ‘yes’.

To make the form look even better we shall create a sub-form in a form??? Wondering what’s
this. Relax and read ahead.

Suppose a student comes with four books to issue. We have to enter the member no and accno
four times even though the member no is same for all the four. It is better if we can put the entry
of the member no in a separate form and the entry of the accno in another form with the second
form automatically taking member no from the first form.

Creating the main form:

Create a form not based on any table or query. Put two text boxes on the form. Save the form
under the name u want it to be known as. Change the label of the first text box to member
number and that of the second text box to member name.

The idea is that when you enter a member no in the first text box the second text box should
fetch the corresponding name and display it. For this we will use the function called ‘dlookup’
(found in ‘expression builder’). Now the display should happen as soon as the member no. is
entered in the first text box. So we use the ‘after update’ event associated with the first text box
to drive the ‘dlookup’ function and find the name corresponding to the member no.

So here goes …. go to the ‘properties’ page of the first text box by right clicking on it. In that,
select the ‘event’ tab and go to the ‘after update’ field. Click on the ellipsis (three dots) to invoke

3
a dialog box from which u will select the macro builder and after giving a name to the macro,
select the ‘set value’ from the choices.

Set value poses to u two questions – the ‘item’ and the ‘expression’. The idea of the set value is
to set the value of something to something. The first something is the ‘item’ and the second
something is the ‘expression’.

Here we want the value of the second text box to be equal to the member name. So ‘item’ is the
second text box and ‘expression’ is something, which will give the member name.

In the item field invoke the expression builder, go to ‘forms’ and in ‘loaded forms’ select the
correct text box name from the middle pane.

In the expression box again, invoke the expression builder and this time select ‘functions’ and in
it select ‘built in’ functions. Now in the middle pane select the ‘domain aggregate’ functions
class. Now in the third pane select the ‘dlookup’ function.

Now the expression window will show like below


Dlookup (expr , domain, criteria)

Expr is what u want the dlookup to give back. In this case it is the member name. Go to ‘tables’
in the left pane. In that select the members table and from the middle pane select the ‘member
name’ field.

Domain is a table or query where u can find the expr. Here it is the members table.

Criteria is that which will help us to ferret out the correct name from the whole lot in the domain
– members in this case. So how do we go about doing it? The member name we want is the one
corresponding to the member no that is present in the first text box. So u enter the criteria
something like this
members!memberno = Forms!form3!text0, it may be diff depending on ur table.
(member number equals that present in text box).

One very important thing …please enclose each of the three components within inverted
commas…that too without leaving any gaps!!

Now close out the dialog boxes one by one. Go back to ur form view and enjoy the experience
with the form, with the correct name comes out corresponding to the number u supply in the first
text box.

A small exercise next. Try putting in one more text box that should show the class of the student.
(This is your homework)

4
MS ACCESS – FORMS

EXERCISE – 2C

MAIN AND SUB FORMS

Go back to the design view and position the form in such a way (reduce the form) that u can drag
the previous form and drop it bang on target, over this form…. Now onwards the outer form will
be referred to as the main form while the inner form will be referred to as the sub form.

Few things… The default view of the sub form should be datasheet view and its data entry
should be set to yes (so that the sub form does not show the old – already entered data and shows
up only a blank single row, ready to accept new entries).

Now we have to see that when we enter a new number in the first text box of the main form it
should be copied into the sub form’s ‘member no’ field. This is a two-step process.

First set the default value of the ‘member no’ field in the sub form to the text box in the main
form.

That is not enough. U got to add one more task in the after update macro associated with the
member no text box in the main form. That is ‘requery’ the sub form (u have to refer to the sub
form by name and type it). That is it! Your form is ready and you can issue to your heart’s
content.

But, every issue should result in another transaction – ‘return’. So we now launch into another
project … a form for the transaction called return.

U will have to create the two forms – main and sub. Main will be a replica of the old main form
(Do it again. No copying business. It is because so many elements in that old form refer to the
form by name and if u simply copy the form then these elements will cry and object).

The major difference comes in the sub form. Here the underlying query has to change. As this is
for return, u may have to include the fields pertaining to that such as member no, accno, title, due
date, return date.

Create the main form and the sub form and do the mixing act. Here there is one thing that u got
to keep in mind. The sub form should show all the old records. Because we will have to enter the
return date against the records already entered. This means for the sub form, u got to have the
‘data entry’ field under the ‘data’ tab in the properties turned to ‘no’.

But with this comes another problem. With data entry turned to ‘no’, the sub form by default,
will show all the records. U have asked one too many. No problem, u can sort it out. U can
restrict the records in the sub form, to the ones pertaining to the number in the main form, by
tweaking the underlying query.

Go to the query and under the member no field in the criteria row, u give the text box in the main
form corresponding to member no. Here u can invoke the expression builder by right clicking on
the criteria field and selecting ‘build'. Here select forms all forms (if u have closed the main
form).

Now go back and run the form. Enter the number in the main form. The sub form shows only the
records pertaining to that no. Now enter today’s date against the books that are being returned.

5
One small correction can go a long way here. The sub form, if it can show only the books that
should be returned, instead of all the books of the member, it would be better. Again we tweak
the query. Under the return date column criteria enter ‘null’ (signifying that I want only records
that have return date as blank (quite obvious it seems now).

Can we make it simpler? Yes. Why should we enter the return date ourselves? No, u need not. U
introduce a new field called ‘returned’ in the design of the transaction table itself, with data type
yes/no (means it can take only two values, yes or no, true or false).

Include this in the query of the sub form and subsequently in the sub form also. Open the form
and u will see a new entity in the sub form with a box. Now this is the ticking box. If u click on
the box a tick will come there. Now go to the design view and right click on the ticking box (and
not on the label) and choose properties. Then go to ‘on click’ option corresponding to ‘event’
tab. There create a macro that will set the value of the return date to today’s date (common
expressions – current date).

One Thing in the ‘Item’ field of the Set Value Dialog Box. U should refer to the return date as
FormsLoaded FormsMain FormSubformReturn Date. Now, your return form is ready.

Das könnte Ihnen auch gefallen