来自:微软 翻译:周芳
【译文】如何从一个表中指定一个控件的值
您可以使用DLookup函数来显示字段值不在记录来源表或报告。例如,假设您有一个基于订单详细信息的表。表显示了OrderID、ProductID,UnitPrice、Quantity和Discount字段。 然而,UnitPrice字段是在另一个产品表中。当用户选择一个产品,你可以使用DLookup函数在相同的表计算控制显示UnitPrice。
下面的示例把当前选定的ProductID组合框中产品的价格填入到UnitPrice文本框。
VBA代码
Private Sub ProductID_AfterUpdate()
’在传递给DLookup函数前评估过滤。
strFilter = "ProductID = " & Me!ProductID
’查找产品的单价将它赋值给UnitPrice控件。
Me!UnitPrice = DLookup("UnitPrice", "Products", strFilter)
End Sub
这个DLookup函数有三个参数。第一个参数指定要查找的字段(UnitPrice);第二个指定表(产品);第三个指定找到的值(与ProductID一样的当前记录在订单子表的ProductID值)。
原文:How to: Assign a Control a Value From a Table
You can use the DLookup function to display the value of a field that is not in the record source for your form or report. For example, suppose you have a form based on an order Details table. The form displays the orderID, ProductID, UnitPrice, Quantity, and Discount fields. However, the UnitPrice field is in another table: Products. You could use the DLookup function in a calculated control to display the UnitPrice on the same form when the user selects a product.
The following example populates the UnitPrice text box with the price of the product currently selected in the ProductID combo box.
VBA
Private Sub ProductID_AfterUpdate()
' Evaluate filter before it is passed to DLookup function.
strFilter = "ProductID = " & Me!ProductID
' Look up product's unit price and assign it to the UnitPrice control.
Me!UnitPrice = DLookup("UnitPrice", "Products", strFilter)
End Sub
The DLookup function has three arguments. The first specifies the field you are looking up (UnitPrice); the second specifies the table (Products); and the third specifies which value to find (the value for the record where the ProductID is the same as the ProductID on the current record in the orders subform).