vba - 在表中添加新记录时如何检查时间范围?
问题描述
我有一个名为“Reservations”的访问表和一个名为“Reservations Form”的访问表。
预约表
员工姓名 | 服务名称 | 服务时间 | 开始日期时间 | End_DateTime |
---|---|---|---|---|
约翰 | 头部按摩 | 2小时 | 20 年 12 月 31 日下午 3:00:00 | 20 年 12 月 31 日下午 5:00:00 |
鲍勃 | 足部按摩 | 1小时 | 20 年 12 月 31 日下午 3:00:00 | 20 年 12 月 31 日下午 4:00:00 |
表格结构
Employee_Name : (Input Field)
Service_Name : (Input Field)
Service_Hours : (Input Field)
Start_DateTime : (Input Date Time Field)
End_DateTime : (Input Date Time Field)
表格中的样本数据
Employee_Name : John
Service_Name : Head Massage
Service_Hours : 2 Hours
Start_DateTime : 31-Dec-20 4:00:00 PM
End_DateTime : 31-Dec-20 5:00:00 PM
我们不能接受同一员工在同一天同一时间的两次预订。
从 20 年 12 月 31 日下午 3:00:00 到 12 月 20 日 31 日下午 5:00:00 为约翰预订。
在此期间不接受其他预订。
如果通过预订表格提交了新的预订,我们需要检查日期和时间以及员工姓名。
John 已在 20 年 12 月 31 日下午 3:00:00 至 12 月 20 日 31 日下午 5:00:00 期间预订。
他无法进行新预订(从 20 年 12 月 31 日下午 4:00:00 到 12 月 20 日 31 日下午 5:00:00)。
VBA 代码
Private Sub Command11_Click()
Dim strWhere As String
Dim dtRequeestStartDate As Date
Dim dtRequestEndDate As Date
dtRequestStartDate = Me![Text1].Value
dtRequestEndDate = Me![Text2].Value
If (DCount("Employee_Name", "Reservations", "Employee_Name=""" & [Employee_Name].Value & """ AND #" & Format(dtRequestStartDate, "dd/mm/yyyy hh:mm:ss AM/PM") & "# <= EndDate" & " and #" & Format(dtRequestEndDate, "dd/mm/yyyy hh:mm:ss AM/PM") & "# >= StartDate") <> 0) Then
MsgBox "Can't book a reservation."
End If
我收到一个错误
您作为查询参数输入的表达式产生了这个错误:'EndDate'
解决方案
好的,这开始看起来很不错。
你拥有的“strWhere”的想法(我看到你没有使用它)只是为了帮助编写代码(我想在需要填充墙的房间里节省一些时间)。
你怎么吃大象?答:一次一口。
所以,这里的想法是把它分解成越来越小的部分——小到可以在公园里散步。
所以,我们首先需要(并且想要)设置一个日期/范围测试/标准。
所以,这应该可以解决问题:
Dim strWhere As String
Dim dtRequeestStartDate As Date
Dim dtRequestEndDate As Date
dtRequestStartDate = Me![Text1].Value
dtRequestEndDate = Me![Text2].Value
ftime = "mm/dd/yyyy HH:MM"
strWhere = "#" & Format(dtRequestStartDate, ftime) & "# <= End_DateTime" & _
" and #" & Format(dtRequestEndDate, ftime) & "# >= Start_DateTime"
所以,这会照顾我们的日期范围。但是我们还需要在上面添加人员。
因此,我们只需将其添加到上面。
strWhere = strWhere & " AND "Employee_Name = '" & [Employee_Name].Value & "'"
所以,看看为什么我们有 strWhere。我们不必这样做,但现在我们可以将其分解,并在 where 子句中添加一个甚至 5 个以上的条件。那么,既然我们有了带有日期范围和工作人员的 strWhere 呢?
然后我们可以使用 dcount()
dcount() 是
dcount("some field name", "some table name", "some where clause")
这就是 dcount() 的语法。事实上,dlookup() 和 dcount() 大体相同。唯一真正的区别是 lookup() 返回一个值,而 dcount() 返回一个计数。
在这种情况下,我们只关心请求的计数是否为 1、20 或其他(任何大于 0 的都是冲突)。
那么,现在我们将标准加载到 where 子句中了吗?
然后我们可以像这样使用 dcount() :
If DCount("*", "Reservations", strWhere) > 0 then
MsgBox "Can't book a reservation."
End If
所以你可以看到为什么建议将标准加载到 strWhere 中。它只是允许更轻松地阅读代码。请注意,我使用“*”作为 dcount() 中的第一个值。您可以输入“id”或您想要的任何列 - 但我们真的不在乎,因为我们只是在计算行数 - 我们只关心我们的结果是否 > 0。
所以,总而言之,我们有很多这样的代码:
Dim strWhere As String
Dim dtRequeestStartDate As Date
Dim dtRequestEndDate As Date
Dim ftime As String
ftime = "mm/dd/yyyy HH:MM"
dtRequestStartDate = Me![Text1].Value
dtRequestEndDate = Me![Text2].Value
' date range critera
strWhere = "#" & Format(dtRequestStartDate, ftime) & "# <= End_DateTime" & _
" and #" & Format(dtRequestEndDate, ftime) & "# >= Start_DateTime"
' employee criteria
strWhere = strWhere & " AND Employee_Name = '" & [Employee_Name].Value & "'"
' doctors exam room criteria
' ok - we don't' have any!!!, but we can add many more criteria.
' now data pull - check for existing -
If DCount("*", "Reservations", strWhere) > 0 then
MsgBox "Can't book a reservation."
else
' ok to book - call routines to add this record to reservations
End If
推荐阅读
- python - 当我说“深红色”时,谷歌语音识别触发“开启”
- postgresql - 通过 AWS Glue 将数据从 RDS Aurora Postgres 推送到 SQS
- c# - 当我打开另一个桌面应用程序时,如何触发 WinForms 应用程序?
- sql - ORDER BY 表结构而不是数字
- javascript - 将具有自定义字体的 SVG 元素下载到 PNG
- php - 来自另一个类的 Wordpress add_action
- azure-devops - 目标分支识别不正确 - SystemPullRequestTargetBranch。解决方法是什么?
- c++ - 如何使用 Boost.Beast 库
- c++ - 减轻头文件中的长限定
- javascript - 为什么 React 原生视图中不能垂直放置多个项目?