I am having difficulty comparing two different date fields on my access form. Ex. If date two is less than date one... I want the system to display a msg... saying date two must be greater than date one.
How to compare two date fields in an Access Form?
Collapse
X
-
Hi stress999,
Assuming you have some controls named similarly as below, you would have VBA behind each of your Date fields in the AfterUpdate Event. Your code would look similar to this:
Normally I don't write the whole thing out, but I had a few minutes and this should get you started.Code:Option Compare Database Option Explicit Private Sub txtDate1_AfterUpdate() VerifyDates End Sub Private Sub txtDate2_AfterUpdate() VerifyDates End Sub Private Sub VerifyDates() If Not (IsNull(Me.txtDate1) Or IsNull(Me.txtDate2)) Then If Me.txtDate2 < Me.txtDate1 Then Me.txtWarning = "Date 2 must be after Date 1" Else Me.txtWarning = "" End If End If End Sub
Hope this hepps! -
Twinnyfo,
I placed the code in the "General" option from the the drop down option where are the named fields are stored. It errors out at "Private Sub verifyDates() and "txtWarning .
Where should I placed the code? I have identical named field on the form as you created in the code: "txtDate1 and "txtDate2. I am not sure what is going on...Comment
-
Twinnyfo,
The error occurs at "Private Sub VerifyDate()" in the code below
Code:Option Compare Database Option Explicit Private Sub txtDate1_AfterUpdate() VerifyDates End Sub Private Sub txtDate2_AfterUpdate() VerifyDates End Sub Private Sub VerifyDates() If Not (IsNull(Me.txtDate1) Or IsNull(Me.txtDate2)) Then If Me.txtDate2 < Me.txtDate1 Then Me.txtWarning = "Date 2 must be after Date 1" Else Me.txtWarning = "" End If End If End SubLast edited by Rabbit; Nov 3 '14, 05:36 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.Comment
Comment