Increment Field

S

Sangeetha

I have a table for employment contracts. I have field for Autonumber. This
field gives the autonumber for the number of contracts. The fields are CID
(Autonumber),ENO (Employee number, lookup wizard), C_NO(Contract number).
One employee can have more than one contract as a new contract will be
issued to him/her at the expiry of the contract. In such case, I would like
to C_NO, increment by itself and it should be automated. I know it can be
done, by writing a macro. Can anyone help on this please.
 
J

Jeff Boyce

Aha!

So, Ken, you admit that the 'lookup data type' IS good for something ...
causing confusion!

<G!>

Jeff B.

KenSheridan via AccessMonster.com said:
Firstly do not rely on an autonumber to give you the number of rows in a
table. All it guarantees is a unique value for each row. If a user
begins
to insert a record and then abandons it for instance there will be break
in
the number sequence. To get the total number of contracts, i.e. rows in
the
table, you just need to include a text box control on your data entry form
or
in a report with a ControlSource of:

=DCount("*","Contracts")

where Contracts is the table name.

If you wish to store the contract number for each employee in the table,
starting at 1 for each employee then put the following code in the
AfterUpdate event procedure of the ENO control on your data entry form:

If Me.NewRecord Then
Me.C_NO = Nz(DMax("C_NO","Contracts","ENO = " & [ENO]),0)+1
End If

When adding a new record this will look up the highest existing C_NO value
for the current employee and add 1. The Nz function returns a zero if
there
are no existing records for the employee in question, so adding 1 to that
gives 1.

However, if you also have a column in the table such as ContractDate you
don't really need to store the contract numbers for each employee at all.
They can be computed and shown in an unbound text box in your data entry
form
or in a report with a ControlSource of:

=DCount("*","Contracts", "ENO = " & [ENO] & " And ContractDate <= #" &
Format
([ContractDate],"yyyy-mm-dd") & "#")

BTW I see that you've used the 'lookup wizard'. I'd strongly recommend
that
you avoid this feature in future. For reasons why see:

http://www.mvps.org/access/lookupfields.htm

You can achieve the same functionality by using a combo box bound to the
ENO
field on your data entry form. Data should only ever be entered or edited
in
a form, never in the raw datasheet view of a table, so using the 'lookup
wizard' in table design serves no purpose, other than to cause confusion
as
to the real values in the column.

Ken Sheridan
Stafford, England
I have a table for employment contracts. I have field for Autonumber. This
field gives the autonumber for the number of contracts. The fields are
CID
(Autonumber),ENO (Employee number, lookup wizard), C_NO(Contract number).
One employee can have more than one contract as a new contract will be
issued to him/her at the expiry of the contract. In such case, I would
like
to C_NO, increment by itself and it should be automated. I know it can be
done, by writing a macro. Can anyone help on this please.
 
J

James A. Fortune

Yeah, Jeff, I recommend it to those I don't like.

Sometimes I despair, though. One of my neighbours took an Access course at a
local college. She asked me for some pointers with one of her first
assignments, so I showed her the right way to do things. Was her tutor happy?
Nah, she should have used all the built in wizardry, including the lookup
wizard. Its enough to make you lose the will to live!

Ken Sheridan
Stafford, England

Fortune's not that outrageous :)! I code option button groups so I
don't even remember how to create one manually. I can imagine taking
an Access test and having the instructor say, "Hmm.., he can't even
create an option group. There's not much of a future for him in
Access :)." I shudder at the idea of using only the built-in
features of Access to create a database.

James A. Fortune
(e-mail address removed)

I read my first horror novel last year. It was by Stephen King. It
gives me nightmares to this day. Spoiler ahead ----------------- It
had a description of someone in the U.S. making a dollar an hour --
shudder :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top