【问题标题】:Access - Get latest date if column contains value from another one访问 - 如果列包含来自另一列的值,则获取最新日期
【发布时间】:2019-12-17 15:00:23
【问题描述】:

我在 Access 中的查询的 VBA 函数存在问题。
我有一个表“tbldata

Equipment   Last Inspection
420-413     2019-06-15
440-433     2019-06-15
402-483     2019-06-29
420-413     2019-12-12

还有一个查询“qryunpair

UnpairEquipment
420 
413 
440 
433 
402 
483 

我想要实现的是:

Equipment Latest Date
420       2019-12-12
413       2019-12-12
440       2019-06-15
433       2019-06-15
402       2019-06-29
483       2019-06-29

我编写了以下代码,但是当我运行它时,它没有返回任何值。 有解决办法吗?

Function typeinspection(Source As String) As String
Dim Rst As Recordset
Dim Rst2 As Recordset
Dim s As String
    s = ""
    Set Rst = CurrentDb.OpenRecordset("tbldata")
    Set Rst2 = CurrentDb.OpenRecordset("qryunpair")
    While Not Rst.EOF

        If InStr(Source, Rst2.Fields("UnpairEquipment") > 0) Then _
            s = Rst.Fields("Last Inspection")
        Rst.MoveNext
    Wend

    Set Rst = Nothing
    Set Rst2 = Nothing
    typeinspection = s

End Function

【问题讨论】:

    标签: sql vba ms-access syntax


    【解决方案1】:

    如果您想要一个返回预期结果的查询,您可以使用运算符LIKE 连接表和查询:

    SELECT q.UnpairEquipment AS Equipment, MAX([Last Inspection]) AS [Latest Date]
    FROM qryunpair AS q INNER JOIN tbldata AS t
    ON '-' + t.Equipment + '-' LIKE '*-' +  q.UnpairEquipment + '-*'
    GROUP BY q.UnpairEquipment
    

    结果:

    Equipment   Latest Date
    402         2019-06-29
    413         2019-12-12
    420         2019-12-12
    433         2019-06-15
    440         2019-06-15
    483         2019-06-29
    

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 2022-11-12
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      相关资源
      最近更新 更多