Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mask on a key

Status
Not open for further replies.

virginie

Programmer
Jul 24, 2002
53
0
0
IL
hi!
i have a unique key in a table
that is an index like this :

yyyy-9999
this has to year+"-"+number id as 4 digits

how can i force the field to be like this??

and how it will be in the forms?

how can i add it as automatic number
2003-0001
2003-0002
...
can someone help??
 
Hiya,

Use the 'DatePart' function to extract the year from the system date to get the Year portion of your id.

As for the numeric part, I'd have a new table with one field: type integer. Also make it the key. (Call the table tblAutoNum, and the field LastID (for future reference)).

When you need a new ID, use an 'hidden' form that automatically assembles your ID from the DatePart function and the value + 1 from the tblAutoNum.

E.g. The form will display the tblAutoNum.LastID value.
Add 1 to it and save it (for next time).
Use a global (public) variable to hold the new id:

strMyID = DatePart("yyyy",[OrderDate]) & "_" & str(LastID))

(Define or DIM strMyID in a module).

Close the form.

In your current form with the field you wish the id to be in:

myfield = strMyID

Done.

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience." darrylles@totalise.co.uk
 
this works, actually!

but the little thing left is that i get this following result":

2002-1
2002-2
...

how can i set the number to be -0001 ??
in your sentence :
strMyID = DatePart("yyyy",[OrderDate]) & "_" & str(LastID))

it works omly if i delete the str before lastID
strMyID = DatePart("yyyy",[OrderDate]) & "_" & (LastID))
otherwise it is error...
why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top