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

default value in subform taken from main form

Status
Not open for further replies.

ph2

IS-IT--Management
Oct 8, 2001
27
GB
Please can anyone help?

I have a form (Sales Header) with a subform (sales items). It is possible to have 100+ sales items within an order, so to save input time I need to be able to add a value in the header form that will be the default value for a field in the subform for each new item added. The user will then have the option to accept this default value or enter a new one. If this is possible, I will need to use it on several fields. (The value in the sales header will be different for each order)
 
On your main form place a control for the default value. Let's call it txtDefault.

For the default value property of the control in your subform, just reference txtDefault on the main form. (Click on the ... to let Access build the reference).
 
BSman,

Many thanks for your prompt reply. I have already tried your suggestion using DLookup but I have not been sucessful. Is there any chance you could help me with the coding? (sorry, I'm fairly new to Access and I don't fully understand these areas at the moment. I have spent a lot of time reading help files and books and hopefully will get there soon)
 
Click on the ... for the Default Value property of the field on the subform.
In the Expression Builder dialog form, click =
Then double click Forms in the left hand box.
Find the Main Form (either under Loaded Forms or All Forms)and double click it
In the middle box, find the control that will contain the default value for the field.
Double click the name of the control containing the default value
Click OK to return to the properties sheet.

The default value will contain something like
Forms![Main Form]![txtDefault]
 
maybe you can try this:

First, create a text box on your main form. Then, in the OnOpen or OnLoad event of your main form, set the value of the text box to the same value as your primary key. That will just ensure uniqueness.

Me.MyTextBox = "Primary Key Value"




Then, in your subform, set the default value of the text box you want to use to equal that text box.

In the Default Value property, put this:

=[Forms]![frmMainForm]![MyTextBox]


As long as the field is not part of the primary key, the user can edit it, but it will default to the primary key value of the main form.
 
BSman,
Thanks once again for your help.

I have actually tried what you stated above
= Forms![CTL]![ctlquoteno]which saves OK, but when I run my main form in add mode I get an error saying that Access needs to rebuild/repair itself.

Am I doing something wrong?
 
Don't know why you get the error message, but if the field that is entered in the subform (and defaults to the unbound control in the main form) is required (null not allowed), then there may be a problem if the user selected value (the control in the main form) is null.

Be sure the field in Main Form is unbound.

Also try making a copy of the Main Form, delete the subform from it, and try opening it in both add and edit modes to see if you get an error then. If you rename the full Main Form and name the new version (without the subform) Main Form, you can keep it open and try separately opening the subform to see if any error occurs then.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top