如何在 "Table 1" 上基于 "a field value" & "a field name" 在 MS Access "Table 2" 中查找值

How to find a Value in MS Access "Table 2" based on "a field value" & "a field name" on "Table 1"

我有2个Table,如附件示例所示 我想根据两条记录从 table 2 中检索数据,"size" 作为第一个字段值,"category" 作为字段名称。 在 Excel 中,使用 Vlookup 和匹配函数很容易,但我希望它在 Access 中。 例如:从 Table 1 的第 3 条记录,"size"=3 & "category"=D,然后 "item 3" 值应从 Table 2 中检索,其中两个条件:"Size" = 3 & 字段名="D" i. e. “30”。 谢谢。

Example; Actual Data

以下函数解决了我的问题,即根据行和列名称 中的值在 table 上查找值 我认为可能需要对代码进行一些调整才能更快。

Function MatchColumn(row As Single, column As String) As Single
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim Pos As Integer ' coulmn position
Set db = CurrentDb
Set tdf = db.TableDefs("Table 2")
Set rs = tdf.OpenRecordset

For Each fld In tdf.Fields
 If fld.Name <> column Then Else Exit For
Next
Pos = fld.OrdinalPosition

If Not (rs.EOF And rs.BOF) Then
 Do While Not rs.Fields(0).Value = row
 rs.MoveNext
Loop
MatchColumn = rs.Fields(Pos).Value

rs.Close
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End If
End Function