最簡單的方法是查看錶格中的值,而不是另一種形式的值。 DLookup()可讓您查看另一個表中的值。
就你而言,如果你的表名爲「Site location」,那麼DLookup()本身可能看起來像這樣。
DLookup("[Date Site Found]", "[Site Location]", "Your WHERE clause goes here")
您可以通過指定表,列和鍵來訪問SQL數據庫中的數據。您的WHERE子句提供密鑰。從描述中無法看出應該是什麼樣子,但我會猜測。
'Using a literal number. Only useful for testing.
DLookup("[Date Site Found]", "[Site Location]", "[Site ID] = 32")
'Referring to another control on the same form.
DLookup("[Date Site Found]", "[Site Location]", "[Site ID] = Form![Site ID"])
鏈接的文章顯示了WHERE子句的幾個變體,每個變體都返回相同的結果。 這種WHERE子句不包括字其中。這讓很多人絆倒了。
使用使用DLookup
你的事件代碼應該是這個樣子。 (我認爲這應該在[工作開始日期]控件的BeforeUpdate事件中。)
Dim date_found as Date
date_found = DLookup("[Date Site Found]", "[Site Location]", "[Site ID] = Form![Site ID"])
If ([work date start] >= date_found) Then
'Good. Do nothing.
Else
'Bad [work start date]. Throw the user a dialog box, and cancel this event.
'Left as an exercise for the OP.
End If
謝謝Dlookup的工作原理,將代碼添加到Private Sub BeforeUpdate事件中。修改並修改了上述示例代碼以適應我的要求。這種方法要比擁有一個非常長的驗證規則要好得多,這個規則要求兩個表單都是開放的。現在我可以根據自己的喜好格式化日期和日期輸入。乾杯 – user1655231