MS Access在两个日期之间选择?

编程入门 行业动态 更新时间:2024-10-24 02:00:15
本文介绍了MS Access在两个日期之间选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经搜索过,但是所有结果都无法帮助我理解.

I have searched, but all results didn't help me to understand.

我需要选择18-23岁的人的名字.所以我的尝试是:

I need to select names of people who are 18-23 years old. So my try was:

WHERE ((People.Birth) Between (Now()-Year(18)) And (Now()-Year(23)))

我做错了什么?解决方案为#some_date#是个坏主意!

What I'm doing wrong? Solution as #some_date# is a bad idea!

推荐答案

要获得真正的解决方案,您需要使用 DateAdd 和类似的函数:

For a true solution, you need to use DateAdd and a function like this:

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

然后您的查询将具有以下 where 子句:

Then your query will have this where clause:

WHERE AgeSimple(People.Birth) Between 18 And 23

更多推荐

MS Access在两个日期之间选择?

本文发布于:2023-10-23 22:43:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   日期   MS   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!