首页 > 解决方案 > 访问 - 基于 ID 和日期的验证

问题描述

我正在为食品银行业务构建一个小型 Access 数据库,作为一个班级的项目。以下是我的一张表:

订单(订单 ID、家庭 ID、订单日期、交付日期)

我将如何设置仅允许家庭 (householdID) 每个日历月下一个订单 (orderDate) 的数据验证规则?这是否仅适用于该表中的验证规则?

在此先感谢您的帮助。

标签: validationms-access

解决方案


Will need to use VBA, either in BeforeUpdate event of orderDate textbox or BeforeUpdate event of form. Could use DLookup() expression. Something like:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not IsNull(DLookup("householdID", "Order", "Format([OrderDate], 'yyyymm')=" & Format(Me.tbxDate, "yyyymm"))) Then
    Cancel = True
    MsgBox "Household already has order for this month."
    DoCmd.RunCommand acCmdUndo
End If
End Sub

Might also be possible to use table BeforeChange data macro, however, I don't use macros of any type.


推荐阅读