DefaultValue

  • Thread starter Thread starter LucB
  • Start date Start date
L

LucB

Ms-Access Help: <If the DefaultValue setting for a field is an expression,
it can't contain [...] references to forms, or other field objects.>

I want to set the Default Value of a field in a subform to the value in a
field on the main form. Is there a way to work around the above restriction?
 
LucB said:
Ms-Access Help: <If the DefaultValue setting for a field is an expression,
it can't contain [...] references to forms, or other field objects.>

I want to set the Default Value of a field in a subform to the value in a
field on the main form. Is there a way to work around the above restriction?


You may be able to set the property using code in the main
form:

Me.subformcontrol.Form.subformtextbox.DefaultValue = _
Me.mainformtextbox

It's not clear where you may want to put that line of code,
possible in the main form's Current event procedure,
possible in the mainformtextbox's AfterUpdate event, most
likely in both places.
 
I tried your solution but it results in the subform field displaying
'#Name?'. Would that be linked to the restriction mentioned in Ms-Access
Help or am I doing something wrong?

Marshall Barton said:
LucB said:
Ms-Access Help: <If the DefaultValue setting for a field is an expression,
it can't contain [...] references to forms, or other field objects.>

I want to set the Default Value of a field in a subform to the value in a
field on the main form. Is there a way to work around the above
restriction?


You may be able to set the property using code in the main
form:

Me.subformcontrol.Form.subformtextbox.DefaultValue = _
Me.mainformtextbox

It's not clear where you may want to put that line of code,
possible in the main form's Current event procedure,
possible in the mainformtextbox's AfterUpdate event, most
likely in both places.
 
I tried your solution but it results in the subform field displaying
'#Name?'. Would that be linked to the restriction mentioned in Ms-Access
Help or am I doing something wrong?

The Help message that you reference is a different issue: you're not
setting the Default property of a table field at all, which is the
subject of the help message.

I suspect you have been tripped up by the peculiar syntax of subform
references:

here, "subformcontrol" needs to be the Name property of the Subform
control on the main form - this might or might not be the same as the
name of the Form within that control. subformtextbox and
mainformtextbox need to be replaced by the names of those controls
from your own form, of course.

You *might* need to include some quotemarks: the DefaultValue property
is a string. It shouldn't give you the #Name? error, but try changing
this to

Me.subformcontrol.Form.subformtextbox.DefaultValue = _
& Chr(34) & Me.mainformtextbox & Chr(34)

where Chr(34) is the " character.
 
Thanks very much John! The quotes were indeed the problem, it was them that
fired the '#Name?' error. Both Chr(34) & Textbox & Chr(34) and the more
awkward """" & Textbox & """"" did the trick.
 
Back
Top