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!

Setting default value = to last record entered 1

Status
Not open for further replies.

crawblood

Technical User
Jul 10, 2005
3
MX
Hi

How can I set the default value equal to the last record entered in a table?

Thanks
 
Short answer: You cannot. This is because Access does not give you the ability to trigger data events on tables. It must occur in an event driven class like a form.

Long answer:
Firstly, define for yourself what "last record entered" means, by some criteria that Access will understand. For example, if your table has an autonumber field called ID, you could say that the last record entered is the one with the Max([ID]). Now you have the record, you probably only want one of the fields since you are unlikely to want to cram an entire record into one fiel'd default value?

Now you learn the DMax function. For an autonumber field named ID in a table named "Table1", DMax("[ID]","Table1") will give you the value of the ID field for the last record added.

So although you cannot specify the default value in the table definition as you may wish, you could mak sure that on your data entry form, the default value of the Text Box that you have bound to the target field, has the DMAX expression entered. It's almost as good. The down side is that if another record is added to Table1 after your user opens your data entry form, the value will not update.

 
Yeah, there are some versions of doing such, for instance using the default value property of form controls, either on each control, or through looping them using relevant form events. Here are some faqs faq702-4852, faq702-5010 (section 6). Calling such in either the After Update or After Insert event of a form, would hopefully determine whether you've got the values from the last altered or the last added record as default for the new record (this version shouldn't be dependent on an incrementing surrogate key).

Roy-Vidar
 
Add a field to your table DateChanged type date.
Add a textbox to your page with the control source DateChanged. Make this invisible. Name it txtDateChanged.

On the form afterupdate add something like txtDateChanged.Value = Now()

Everytime the form is changed the [DateChanged] is updated (bit clunky, but it is a small piece of code) ~ now simply sort the form on DateChanged (decending) to have everything shown in reverse last changed order.
Regards Paul
 
DMax("[ID]","Table1") "

Thank you for this fellas

You have helped me a lot.
Best,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top