【发布时间】:2019-12-19 16:46:12
【问题描述】:
我有以下查询“qryE”。
Equipment E1_45J E2_45J E3_45J E4_45J
400 2019-03-25 2019-10-07
401 2019-04-01 2019-10-23
402 2019-02-14 2019-08-25 2019-11-11
403 2019-02-11 2019-09-05 2019-11-25
我做了一个函数来返回这 4 列之间的最新值:
Function getmaxdate(dteDate1 As Date, dteDate2 As Date, dteDate3 As Date, dteDate4 As Date) As Date
Dim dteMaxDate As Date
dteMaxDate = dteDate1
Debug.Print dteDate2
If IsDate(dteDate2) Then
If dteDate2 > dteMaxDate Then dteMaxDate = dteDate2
If IsDate(dteDate3) Then
If dteDate3 > dteMaxDate Then dteMaxDate = dteDate3
If IsDate(dteDate4) Then
If dteDate4 > dteMaxDate Then dteMaxDate = dteDate4
getmaxdate = dteMaxDate
End Function
我想要实现的是:
Equipment 45J
400 2019-10-07
401 2019-10-23
402 2019-11-11
403 2019-11-25
但是,当我使用 45J 运行查询时:
getmaxdate([E1_45J],[E2_45J],[E3_45J],[E4_45J])
我得到 标准表达式中的数据类型不匹配。
我尝试使用以下命令强制我的查询值的格式:
45J: getmaxdate(Format([E1_45J], "yyyy/mm/dd"),Format([E2_45J], "yyyy/mm/dd"),Format([E3_45J], "yyyy/mm/dd"),Format([E4_45J], "yyyy/mm/dd"))
但是,我仍然遇到同样的错误。
我做错了什么?
【问题讨论】:
-
有空白值,访问是数据类型的坚持者,并且根据我的经验,空日期是有问题的。您需要检查每个空格,然后跳过它。
-
理想情况下,您需要一列作为日期,但如果您无法更改表设计,则可以使用 union all 来取消透视数据,然后在外部查询中使用然后使用 max 函数。 stackoverflow.com/questions/7255423/…
-
除了上述建议的 SQL 答案 - 对于潜在的纯 VBA 解决方案 - allenbrowne.com/func-09.html