简体   繁体   English

如何基于“表1”上的“字段值”和“字段名称”在MS Access“表2”中查找值

[英]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.

相关问题 MS Access:根据表的字段值隐藏对象 - MS Access: Hide object based on Table's field value MS Access-Dlookup根据另一个表的内容设置字段的值 - MS Access - Dlookup to set value of field based on content of another table 如何基于MS Access 2013 Web应用程序中另一个表的输入来更改一个表中的字段的值 - How to change the value of a field in one table based on input from another table in MS Access 2013 web app MS Access:如何根据另一个字段的值使用查找表自动填充? - MS Access: How to auto fill using lookup table based on the value of another field? 如何从表字段值中默认表单字段值? (MS 访问) - How to Default a Form Field Value from a Table Field Value? (MS Access) Microsoft Access在具有已知值的表中查找字段 - Microsoft Access find field in table with known value 如何根据另一个字段的值过滤 MS Access 字段 - How to filter MS Access field based on the value of another field MS Access查找并突出显示与长文本字段中的任何表列列表值匹配的多个子字符串 - MS Access Find & Highlight Multiple Substrings that Match Any Table Column List Value in Long Text Field MS Access-通过字段中的值重复表中的记录 - MS Access - Repeat Records from a Table by a value in a field 从 MS Access 中的表中获取自动编号字段值 - To get Auto Number field value from table in MS Access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM