我想只在MS访问的文本框中显示5到12之间的年龄。年龄字段不是表格而是计算的。这是代码
= Int((Now() - [出生日期])/ 365.25)
现在验证无效。这是代码:
= 5且< = 12
当年龄> 12时,验证文字没有出现?我该如何解决?
由于
答案 0 :(得分:1)
首先,你需要一个适当的函数来计算年龄 - 像这样:
Public Function AgeSimple( _
ByVal datDateOfBirth As Date) _
As Integer
' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.
Dim datToday As Date
Dim intAge As Integer
Dim intYears As Integer
datToday = Date
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDateOfBirth, datToday)
If intYears > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of datDateOfBirth.
intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
End If
AgeSimple = intAge
End Function
然后验证应该是:
>=5 And <=12
但验证仅适用于输入,这不会发生,因为控件绑定到表达式。当你考虑它时,可以采取什么行动,并且&#34;无效&#34;从表达式输出?
答案 1 :(得分:0)
拳头,为了在几天之间获得可中继的时间间隔,请使用DateDiff功能。您的年龄函数应如下所示:Int(DateDiff("d",[Date Of Birth],Now()/365.25)
第二,您需要条件显示,使用IIf功能。这应该是你的控制权:
=IIf(Int(DateDiff("d",[Date Of Birth],Now())/365.25)>=5 and Int(DateDiff("d",[Date Of Birth],Now())/365.25)<=12,Int(DateDiff("d",[Date Of Birth],Now())/365.25),Null)
正如古斯塔夫指出的那样,验证文本在这种情况下并不相关。