Dennis said:
Concerning your comment :
“you can use the trick I mentioned earlier to overlay a text box on the
combo box. I'll describe it in more detail if you let me know you want to
follow that approach.â€
I would like to know about this approach because I would like to use it in
another form on which I’m working.
To use this technique, you base the form on a query that returns both the
foreign key field you want to store, and the text field you want to display.
Let's call them "FKField" and "DisplayField". FKField is stored in the
table that the form is primarily concerned with, while DisplayField comes
from different table, a lookup table. In the lookup table, the key field
that corresponds to FKField is usually the primary key field, so I'll call
that field "PKField". In real life, that field may well have the same name
as the foreign key field in the other table. (In your original table
arrangement, "UnitNo" would be the foreign key, and "AptNo" would be the
display field -- you wanted to store UnitNo in the Leases table, but display
AptNo on the form.)
On the form, you have a combo box bound to FKField, but drawing its
rowsource from the lookup table, including both PKField and DisplayField,
filtered according to the current record on the form. So it has properties
like these:
Control Source: FKField
Row Source:
SELECT PKField, DisplayField FROM LookupTable
WHERE SomeOtherField = Forms!YourForm!SomeOtherField;
Bound Column: 1
Column Widths: 0; 1 (or whatever width is required)
Limit To List: Yes
Because Limit To List is set to Yes/True, this combo box will be blank
whenever its (requeried) rowsource doesn't include the PKField matching the
form's FKField.
Also on the form, you have a text box bound to DisplayField (from the form's
recordsource query). So this text box will always display the text
corresponding to that record's FKField, regardless of what the combo box
shows or doesn't show. Set these properties of the text box:
Locked: Yes
Tab Stop: No
That's so that the DisplayField value in the lookup table can't accidentally
be changed by the user, and the user can't reach the text box by tabbing
through the form.
Now, here's where the trick comes in. You size the text box so that it
exactly matches the text area of the combo box -- all but the drop-down
arrow. Then you position the text box so that it exactly covers up the text
area of the combo box. Use the Format -> Bring To Front menu item to ensure
that the text box is in front of the combo box.
Finally, create two event procedures: one for the text box's GotFocus event
(to send the focus to the combo box) and one for the combo box's GotFocus
event (to requery the combo box). The event procedures might look like
this:
'------ start of example code ------
Private Sub txtDisplayField_GotFocus()
Me.cboFKField.SetFocus
End Sub
Private Sub cboFKField_GotFocus()
Me.cboFKField.Requery
End Sub
'------ end of example code ------
In the above, "txtDisplayField" is the name of the text box, and
"cboFKField" is the name of the combo box.
Unless I've overlooked something (which is possible, since I'm writing this
out freehand), that should do it. All records on the continuous form should
*appear* to show a combo box containing the correct DisplayField text, but
what they are really showing, except when the combo box has the focus, is
the text box instead. But any attempt to enter data in the combo box will
cause the combo box to get the focus and be requeried to show just the
appropriate items in its list.