[英]How to find a Value in MS Access “Table 2” based on “a field value” & “a field name” on “Table 1”
I've 2 Tables, as shown in the attached example I want to retrieve the data from table 2 based on two records, "size" as 1st field value & "category" as a field name. 我有2个表,如所附示例中所示,我想基于两个记录从表2中检索数据,“大小”作为第一个字段值,“类别”作为字段名。 In Excel, it is easy by using Vlookup & match functions, but I want it In Access. 在Excel中,使用Vlookup和match函数很容易,但是我希望在Access中使用它。 As an example: From Table 1 at 3rd record , "size"=3 & "category"=D, then "item 3" value shall be retrieved from Table 2 with two conditions: "Size" = 3 & field name ="D" ie "30". 例如:从第三条记录的表1中,“ size” = 3&“ category” = D,则必须在两个条件下从表2中检索“ item 3”值:“ Size” = 3&字段名称=“ D ”即“ 30”。 Thank you. 谢谢。
Example ; 例子 ; Actual Data 实际数据
The following function solved my problem to find a value on a table based on a value in a row & column name I think it may require some tuning on the code to be faster. 以下函数解决了我的问题,该问题基于行和列名称中的值在表上查找值,我认为可能需要对代码进行一些调整才能更快。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.