访问年龄范围查询未返回小于1的年龄

时间:2019-05-10 09:36:10

标签: ms-access

我在Access数据库中有一个查询,该查询需要2个用户输入的年龄,并返回这些输入之间的数据库条目。当您输入一个最小值>> = 1的范围时,它会很好地工作,但是如果最小值是<1,那么即使有条目存在,它也不会返回任何内容。

最初,我认为这可能与我计算年龄有关(我正在查询中即时进行计算),所以我尝试使用和不使用datediff函数: (Date()-[DOB])/ 365.25 和 年龄:(DateDiff(“ y”,[DOB],Date()))/ 365.25 但这些都不会对结果产生任何影响。它与年龄计算有关吗,还是在...和运算符之间有什么不起作用?

此部分查询的完整SQL代码为:

DateDiff("y",[DOB],Date()))/365.25) Between [Forms]![AgeRangeQueryForm]![Youngest Age in Range] And [Forms]![AgeRangeQueryForm]![Oldest Age In Range]

整个查询的代码是(包括Gustav建议的修改):

PARAMETERS [Forms]![AgeRangeQueryForm]![Youngest Age in Range] Short, [Forms]![AgeRangeQueryForm]![Oldest Age in Range] Short, [Forms]![AgeRangeQueryForm]![GenderOptionGroup] Short, [Forms]![AgeRangeQueryForm]![PostcodeSearch] Short;
SELECT [Carer Contact Details_1].[Parents Name], [Carer Contact Details_1].[Parents Surname], [Carer Contact Details_1].[Email Address], [Carer Contact Details_1].[Contact Number], [Carer Contact Details_1].Postcode, [Child Details1].[First Name], UCase(Left(Nz([Child Details1].[Gender],""),1)) AS Gender, Round((DateDiff("d",[DOB],Date())/365.25),2) AS Age, [Child Details1].DOB, [Child Details1].[Date of Last Test]
FROM [Carer Contact Details_1] INNER JOIN [Child Details1] ON [Carer Contact Details_1].[Parent ID] = [Child Details1].[Parent ID]
WHERE (((UCase(Left(Nz([Child Details1].[Gender],""),1))) Like Choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F","[MF]")) AND ((Round((DateDiff("d",[DOB],Date())/365.25),2)) Between [Forms]![AgeRangeQueryForm]![Youngest Age in Range] And [Forms]![AgeRangeQueryForm]![Oldest Age In Range]) AND ((Left([Carer Contact Details_1]![Postcode],Len(Nz([Forms]![AgeRangeQueryForm]![PostcodeSearch]))))=Nz([Forms]![AgeRangeQueryForm]![PostcodeSearch])));

很明显,我的预期结果是它能够处理小于1的值,即可以搜索并找到0.1-0.9的值。当前,它返回空白。

我对Access和SQL还是很陌生,所以如果这有一个我找不到的明显解决方案,我深表歉意。

谢谢。

编辑:我以为我会对此进行更新,因为我仍然对此有疑问。我尝试了下面列出的两个建议(由Gustav和SunKnight0提出),但都没有起作用。它仍然不会返回小于1年的任何内容(有趣的是,如果我将查询的Age部分设置为以天为单位返回年龄,则它不会返回少于525天的任何内容)。我认为这可能是由于我的搜索表单的“性别”字段中缺少值所致,因为它没有返回该字段中没有条目的任何条目(无论年龄)。考虑到这一点,我添加了这一行:

((UCase(Left(Nz([Child Details1].[Gender],""),1)))

在查询部分,我选择了指定的性别。我已经更新了上面的整个查询,因此它反映了我正在努力的最新尝试。

我现在完全为为什么它返回的内容感到困惑。与我用来计算年龄的函数有关吗? DateDiff会自动进行四舍五入吗?

2 个答案:

答案 0 :(得分:0)

它是“ yyyy”,而不是“ y”(一年中的某天),但是使用一个可以正确计算年龄的函数,如下所示:

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

然后您的查询将是:

Parameters 
    [Forms]![AgeRangeQueryForm]![Youngest Age in Range] Short,
    [Forms]![AgeRangeQueryForm]![Oldest Age In Range] Short;
Select
    *,
    AgeSimple([DOB])
From
    YourTable
Where
    AgeSimple([DOB]) Between [Forms]![AgeRangeQueryForm]![Youngest Age in Range] And [Forms]![AgeRangeQueryForm]![Oldest Age In Range]

答案 1 :(得分:0)

要获取以天为单位的年龄(然后将其除以365.25,就可以将年纪作为小数年),请使用

DateDiff("d",[DOB],Date())

不是

DateDiff("y",[DOB],Date())