问题
I have a form TForm
based on table T
, set up as a datasheet. My goal is to add a filterable column to the datasheet where the column's value is calculated from a query using another column's value.
I tried to do this by adding a text box currentBox
to T
. The control source for currentBox
is:
=DLookUp("name","currentStatus","itemID=" & [ID])
where [ID]
is a field in T
and currentStatus
is an aggregate query on a table that T
is related to.
I can filter on all the fields in TForm
that are in T
. But I can't filter on currentBox
, even though it also appears as a column in the form; clicking on the column header doesn't do anything.
I'm guessing the problem is that currentBox
is not bound to a field in T
; is there a way to work around this?
回答1:
Here's a VBA solution:
Add a combo box (aka drop-down) object to your form header. This drop-down's source will be an independent query that displays all the values your Dlookup() currently pulls (names?) and stores the itemID. Let's call it
ObjPickName
in this example.Add an
AfterUpdate
event toObjPickName
that will filter your form for you (your form will still be based onT
). The code will be something like:Private Sub Combo_ObjPickName_AfterUpdate()
Me.Form.Filter="[itemID]='" & Me.Combo_ObjPickName.Value & "'"
Me.Form.Filteron=True
End Sub
回答2:
The way that I ended up solving this was to add a field to T
, and have that field updated during the AfterUpdate()
event with the value from the DLookup()
call. Because the field is now no longer query-based, it can be used to filter the form.
来源:https://stackoverflow.com/questions/17576613/how-to-enable-filtering-on-a-field-defined-by-dlookup