在相同的记录中,我似乎遇到了一个以前从未遇到过的问题。我需要设置一个基于5个其他日期字段的最新日期的日期字段值。我想在查询级别这样做,这样我就可以分配最近的日期并将其存储在一个字段中,但我不确定这是否可行。如果我必须在表单中完成,那么我不确定如何写出Case语句。如有任何帮助,我们不胜感激!
我现在要做的就是写一个表达式,大致如下所示:
ESA Exp Date: IIf([1-Record Search Date]>[2-Site Reconnaissance Date] And [1-Record Search Date]>[3-Owner Interview Date] And [1-Record Search Date]>[4-Lien/AUL Search Date] And [1-Record Search Date]>[5-User Questionnaire Date],[1-Record Search Date],IIf([2-Site Reconnaissance Date]>[1-Record Search Date] 以此类推。但是,表达式有字符限制,所以有人在一个无关的问题中说要写一个函数。不过,我不确定这在我的情况下是如何工作的。
再次感谢Rob
发布于 2014-03-04 12:39:09
这看起来如此困难的原因是因为您的表结构对于关系查询并不是最优的。假设您的当前表(date_values)具有列d1到d5,所有日期。你需要像这样规范化表格(注意,这不会改变你的实际表格,它只是在内存中把它‘重新打包’成一个更容易查询的形式):
select entity_id, 'type 1' as date_type, d1 as date_value
from date_values
where d1 is not null
union all
select entity_id, 'type 2' as date_type, d2 as date_value
from date_values
where d2 is not null
union all
select entity_id, 'type 3' as date_type, d3 as date_value
from date_values
where d3 is not null
union all
select entity_id, 'type 4' as date_type, d4 as date_value
from date_values
where d4 is not null
union all
select entity_id, 'type 5' as date_type, d5 as date_value
from date_values
where d5 is not null请注意,entity_id是唯一标识表中的行的任何字段。一旦你有了规范化形式的数据,查询就很容易了:*
update date_values as d
inner join (
select ssq.entity_id, max(ssq.date_value) as max_date
from (
... the query I show above ...
) as ssq
group by ssq.entity_id
) as sq
on d.entity_id = sq.entity_id
set d.[ESA Exp Date] = sq.max_date
where entity_id = [... the entity you wish to update ...]上面的最后一个子句where子句将修改限制为您可以指定的单个行。您可以在Access提示时手动输入,也可以让它引用表单中的某个字段。有很多选择。
好的,easi_er_,*。
发布于 2014-03-04 13:26:30
如果不能更改表格结构,那么可以使用Allen Browne http://allenbrowne.com/func-09.html创建的函数,网页上有更完整的说明。
在查询中这样调用它:(我可以建议不要在不需要的时候放空格,这将有助于在VBA中进一步深入)
ESAExpDate: MaxOfList([1-Record Search Date],_
[2-Site Reconnaissance Date],[3-Owner Interview Date],[4-Lien/AUL Search Date], _
[5-User Questionnaire Date])我已经粘贴在下面,以防链接死掉。
Function MaxOfList(ParamArray varValues()) As Variant
Dim i As Integer 'Loop controller.
Dim varMax As Variant 'Largest value found so far.
varMax = Null 'Initialize to null
For i = LBound(varValues) To UBound(varValues)
If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
If varMax >= varValues(i) Then
'do nothing
Else
varMax = varValues(i)
End If
End If
Next
MaxOfList = varMax
End Functionhttps://stackoverflow.com/questions/22161488
复制相似问题