如何检索 Date 字段在下一年内的所有记录的计数,并将其存储在变量中?
How to retrieve a count of all records where a Date field is within the next year, and store it in a variable?
我是 MS Access 的新手,但对数据库有一定的了解,对 SQL 有一定了解。
我正在 Access 中创建数据库。在用户首先看到的主窗体上,我需要显示 Case
table 中所有记录的计数,这些记录的 StatuteOfLimitation
日期在下一年内。
我的目标是创建一个描述信息的标签,标签下方有一个按钮。该按钮将打开所有记录的报告(这部分工作正常),我希望按钮的标题显示符合条件的记录总数。
我能想到的唯一方法是检索计数并将其存储到变量中。从那里,我应该能够将标题设置为变量值。
我见过几种检索计数并将其存储在变量中的方法,但我发现的所有方法都只存储了每条记录的计数,没有过滤日期范围。
这是我能想到的最好的方法,但它不起作用:
Private Sub Form_Load()
Dim oneYearFromToday As TempVars
SET TempVars!oneYearFromToday = (SELECT COUNT(StatuteOfLimitation) FROM Case
WHERE StatuteOfLimitation <= DateAdd("yyyy", 1, Date());
End Sub
我可能不会使用临时变量来存储您的变量。您可以使用 DAO 尝试类似下面的操作。
Private sub Form_Load()
dim rst as dao.recordset
dim strSQL as string
'Creates query string
strsql = "SELECT Count(StatueOfLimitation) as RecordCount " & _
"FROM Case " & _
"WHERE (((StatueOfLimitation) <= DateAdd('yyyy',1,date())));"
'Opens the query string into a recordset
set rst = currentdb.openrecordset(strsql)
'Change Labelnamehere isnto the name of the label control on your form
'Change what ever saying you want here to something you want the label to display
me.labelnamehere.caption = "What ever saying you want here " & rst![RecordCount] 'Don't need a variable storage is you can use the result here
rst.close 'closes recordset
set rst = nothing 'Clears memory
EndCode: 'Ensures clean up is successful
If not rst is nothing then
rst.close
set rst = nothing
end if
end sub
如果这对你不起作用,请告诉我,我会做更多的挖掘工作。
DCount()
为 "How to retrieve a count of all records where a Date field is within the next year"
提供了一种简单的方法
Dim lngCount As Long
lngCount = DCount("*", "Case", "[StatuteOfLimitation] <= DateAdd('yyyy', 1, Date())")
然后您可以在命令按钮的 Caption
属性 中使用该计数。假设按钮名为 cmdOpenReport
...
Me!cmdOpenReport.Caption = "Report " & lngCount & " cases"
如果您想要 TempVar
而不是常规 Long
变量中的计数,请将其声明为 As TempVar
(仅一个)而不是 As TempVars
(集合) .当你给它赋值时,不要使用 Set
.
Dim oneYearFromToday As TempVar
TempVars!oneYearFromToday = DCount("*", "Case", "[StatuteOfLimitation] <= DateAdd('yyyy', 1, Date())")
我无法对 HansUp 的回答发表评论,但要更改 VBA 中的标签标题,您需要在设计视图中打开表单。这不是我通常做的事情,我个人的偏好是使用标签不变的未绑定文本框,但我已经在一个数据库中完成了它,以更新 time/date 和用户发送的最后一封电子邮件。
代码如下所示:
DoCmd.OpenForm "yourformname", acDesign, , , , acHidden
Forms![yourformname]![yourlabelname].Caption = "There are " & TempVars!onYearFromToday & " cases to view."
DoCmd.Close acForm, "yourformname", acSaveYes
DoCmd.OpenForm "yourformname", acNormal
使用按钮来显示信息是个坏主意。这不是按钮的目的。
使用文本框并设置其 ControlSource:
=DCount("*","[Case]","[StatuteOfLimitation]<=DateAdd("yyyy",1,Date()))
会自己填充,随时打开报表的按钮是自由的
我是 MS Access 的新手,但对数据库有一定的了解,对 SQL 有一定了解。
我正在 Access 中创建数据库。在用户首先看到的主窗体上,我需要显示 Case
table 中所有记录的计数,这些记录的 StatuteOfLimitation
日期在下一年内。
我的目标是创建一个描述信息的标签,标签下方有一个按钮。该按钮将打开所有记录的报告(这部分工作正常),我希望按钮的标题显示符合条件的记录总数。
我能想到的唯一方法是检索计数并将其存储到变量中。从那里,我应该能够将标题设置为变量值。
我见过几种检索计数并将其存储在变量中的方法,但我发现的所有方法都只存储了每条记录的计数,没有过滤日期范围。
这是我能想到的最好的方法,但它不起作用:
Private Sub Form_Load()
Dim oneYearFromToday As TempVars
SET TempVars!oneYearFromToday = (SELECT COUNT(StatuteOfLimitation) FROM Case
WHERE StatuteOfLimitation <= DateAdd("yyyy", 1, Date());
End Sub
我可能不会使用临时变量来存储您的变量。您可以使用 DAO 尝试类似下面的操作。
Private sub Form_Load()
dim rst as dao.recordset
dim strSQL as string
'Creates query string
strsql = "SELECT Count(StatueOfLimitation) as RecordCount " & _
"FROM Case " & _
"WHERE (((StatueOfLimitation) <= DateAdd('yyyy',1,date())));"
'Opens the query string into a recordset
set rst = currentdb.openrecordset(strsql)
'Change Labelnamehere isnto the name of the label control on your form
'Change what ever saying you want here to something you want the label to display
me.labelnamehere.caption = "What ever saying you want here " & rst![RecordCount] 'Don't need a variable storage is you can use the result here
rst.close 'closes recordset
set rst = nothing 'Clears memory
EndCode: 'Ensures clean up is successful
If not rst is nothing then
rst.close
set rst = nothing
end if
end sub
如果这对你不起作用,请告诉我,我会做更多的挖掘工作。
DCount()
为 "How to retrieve a count of all records where a Date field is within the next year"
Dim lngCount As Long
lngCount = DCount("*", "Case", "[StatuteOfLimitation] <= DateAdd('yyyy', 1, Date())")
然后您可以在命令按钮的 Caption
属性 中使用该计数。假设按钮名为 cmdOpenReport
...
Me!cmdOpenReport.Caption = "Report " & lngCount & " cases"
如果您想要 TempVar
而不是常规 Long
变量中的计数,请将其声明为 As TempVar
(仅一个)而不是 As TempVars
(集合) .当你给它赋值时,不要使用 Set
.
Dim oneYearFromToday As TempVar
TempVars!oneYearFromToday = DCount("*", "Case", "[StatuteOfLimitation] <= DateAdd('yyyy', 1, Date())")
我无法对 HansUp 的回答发表评论,但要更改 VBA 中的标签标题,您需要在设计视图中打开表单。这不是我通常做的事情,我个人的偏好是使用标签不变的未绑定文本框,但我已经在一个数据库中完成了它,以更新 time/date 和用户发送的最后一封电子邮件。
代码如下所示:
DoCmd.OpenForm "yourformname", acDesign, , , , acHidden
Forms![yourformname]![yourlabelname].Caption = "There are " & TempVars!onYearFromToday & " cases to view."
DoCmd.Close acForm, "yourformname", acSaveYes
DoCmd.OpenForm "yourformname", acNormal
使用按钮来显示信息是个坏主意。这不是按钮的目的。
使用文本框并设置其 ControlSource:
=DCount("*","[Case]","[StatuteOfLimitation]<=DateAdd("yyyy",1,Date()))
会自己填充,随时打开报表的按钮是自由的