To be able to edit the value, the ActivityName control would have to be bound
to a field in the underlying table. I know I said don't have such a field,
but the fact that you want to be able to change it from the value in the
referenced row in the tblEquipment table changes the model, and means that it
does not now introduce redundancy to have such a field. In the jargon the
ActivityName fields in the two tables are each said to be 'functionally
dependent' solely on the whole of the key of their own table, which means,
other things being equal, that the table is normalized to Third Normal Form
(3NF). Normalization is the process of eliminating redundancy by
'decomposing' tables into related tables so that the normalization rules are
satisfied. There are normal forms from 1 to 5, along with an extra one
(Boyce Codd Normal Form or BCNF) which was inserted when it was found that
the original set didn't cater for one particular situation. There are normal
forms beyond 5, but they are somewhat bizarre and distinct from the basic 5.
So, the ControlSource would now be the name of the ActivityName field in the
form's table, and to set its value you'd 'push' in the value looked up from
the tblEquipment table by putting the following code in the AfterUpdate event
procedure of the EquipmentName control on the form:
Dim strCriteria As String
strCriteria = "EquipmentName = """ & Me.EquipmentName & """"
Me.ActivityName = DLookup("ActivityName", "tblEquipment", strCriteria)
To do this select the control in form design view and open its properties
sheet if its not already open. Then select the AfterUpdate event property in
the properties sheet. Click on the 'build' button; that's the one on the
right with 3 dots. Select 'Code Builder' in the dialogue, and click OK. The
VBA window will open at the event procedure with the first and last lines
already in place. Enter the lines of code between these two existing lines.
Ken Sheridan
Stafford, England
PPCO said:
Cut and pasted it and it works perfectly. Don't know what I did, but it
works
. Out of curiosity, is there a way to do something like this and still
also be able to manually fill in the activity field if needed?
PPCO said:
Looks like the first one would be the one--not totally sure if I follow it,
but I'll give it a try. I will always be manually filling in a value in the
Equipment Name field, I just want the activity to auto fill. Thanks for all
your help.
Ken Sheridan said:
You can do it in various ways e.g.
1. Call the DLookup function in an expression as the ControlSource of the
Activity control:
=DLookup("ActivityName", "tblEquipment", "EquipmentName = """ &
[EquipmentName] & """")
This assumes that the tblEquipment table includes a column EquipmentName
which is referenced by the EquipmentName column in the form's underlying
table.
2. Base the form on a query which joins its current underlying table(s) to
the tblEquipment table on EquipmentName and bind a text box control to the
ActivityName column from the tblEquipment table. Note that the control
would be updatable in this case, so you might want to set its locked property
to True and its Enabled property to False to prevent it being edited.
3. For the EquipmentName control on the form use a combo box with a
RowSource property of:
SELECT EquipmentName, ActivityName FROM tblEquipment ORDER BY EquipmentName;
Set its ColumnCount property to 2. For the ControlSource of the Activity
control reference the unseen second column of the combo box with:
=EquipmentName.Column(1)
The column property is zero-based, so Column(1) is the second column.
What you should not do is include an ActivityName column in the forms
underlying table, i.e. the table which references the tblEquipment table via
the EquipmentName foreign key column. That would introduce redundancy and
the risk of inconstant data. The activity for the equipment in question is
known via the relationship so can be shown whenever necessary using the above
methods.
Ken Sheridan
Stafford, England
:
Here's the expression I tried: =IIf([equipmentname] Is Not
Null,[tblEquipment]![ActivityName],Null).
On the form, it shows #Name? in the activity field.
:
I have two fields on a form: Equipment Name and Activity. I want the field
Activity to auto fill from a list based on what's in Equipment Name. I've
tried a couple of expressions in the expression builder, but to no avail.
Thanks for any help