我是 VBA 的新手,但有一些编码经验,所以我正在慢慢开始学习。我的 Access 数据库中有两个表,每个表都有一个相同的字段。一个表是可能值(“TOSITEXREF”)的不同列表,这些值可能会显示在另一个表(“Trans_Earned”)的同一字段中。我试图创建的这个函数将在宏中运行,以确定通过查询附加的数据是否在 ToLocn 字段中有一个不在可能值列表中的实例。这是我到目前为止所拥有的,但没有用:
Function TESTING()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("TransEarned")
Dim rs2 As DAO.Recordset
Set rs2 = CurrentDb.OpenRecordset("TOSITEXREF")
Dim cond1A As Boolean
cond1A = (rs.Fields("ToLocn") = rs2.Fields("ToLoc"))
If cond1A Then
DoCmd.OpenQuery "Earns", acViewNormal, acEdit
Else
DoCmd.CancelEvent
MsgBox "Unknown ToLocation, Please Update TOSITEXREF File to acccount for new location", vbOKOnly, "NEW LOCATION"
End If
Set rs = Nothing
Set rs2 = Nothing
End Function
最佳答案
最终的 VBA 示例将在表 1 的 Loc 字段中返回一个值,但该值不会出现在表 2 的 Loc 字段中:
Function Validate()
Dim sql As String
Dim rs As DAO.Recordset
sql = "SELECT Table1.Loc FROM Table1 LEFT JOIN Table2 ON Table1.Loc = Table2.Loc WHERE (((Table2.Loc) Is Null))"
Set rs = CurrentDb.OpenRecordset(sql)
If (rs.RecordCount = 0) Then
MsgBox "WORKS", vbkokonly, "WORKS"
Else
MsgBox "DOES NOT WORK", vbkokonly, "DOES NOT WORK"
Set rs = Nothing
End If
End Function
关于sql - VBA 检查来自 2 个字段的不同值是否匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30782524/