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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prepopulating a field in a form

Status
Not open for further replies.

moles

Technical User
Jul 29, 2002
17
US
Once submitted my form creates a record in a table, one of the fields in the table is Auto Number( generates random nuber, identifier field). On the second form I have a text box that I would like to be populated with the auto number from the last record created from submitting the first form. How can I accomplish this - should I set the default value of the text box to that record's auto number using Dlast or should I create a piece of code that populates that text box when the form opens.
 
That all depends on some fine detail within your design.

Will form1 still be open ( and the last record current ) at the point when you open form2?
If so you can access the value in the control on form1 directly using:-
Forms!FormName!ControlName

Is form2 opened via a button on form1?
If so you can pass the value of the AutoNumber in the OpenArgs parameter of the DoCmd.OpenForm command

If not then using DLast in form2's On_Load event will work quite well.

It is more important that it works reliably regardless of whatever the user does to it - rather than there being a 'right way' in something like this.

Another option - could form2 be a subForm and form1 the main form.
That way you can get the foreign key populated automatically.



'ope-that-'elps.

G LS



 
How can I pass the auto number in the OpenArgs parameter???
 
If the table is tblTheName then the AutoNumber field is TheNameId

Then have a control on the form also called TheNameId, bound to the field.
( IF you don't want the users to see the AutoNumber then make the control .Visible = No )

Then, to open Form2 and pass the Id value use :-

DoCmd.OpenForm "Form2", , , , , , TheNameId



QED.

G LS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top