How to compare two date fields in an Access Form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stress999
    New Member
    • Oct 2014
    • 12

    How to compare two date fields in an Access Form?

    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.
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3664

    #2
    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:

    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
    Normally I don't write the whole thing out, but I had a few minutes and this should get you started.

    Hope this hepps!

    Comment

    • stress999
      New Member
      • Oct 2014
      • 12

      #3
      Thanks Twinnyfo - this is syntax I was looking for. Great stuff!

      Comment

      • stress999
        New Member
        • Oct 2014
        • 12

        #4
        Hi Twinnyfo,
        I tried to insert the code above... for some reason it errors out. To test the exact code, I created 2 text boxes with the field names you used in the code.
        The code was inserted from the "General" option.

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3664

          #5
          stress999,

          First, I don't know what "General" option you are referring to.

          Second, at what point of the code is it producing an error? This will help us to trouble shoot.

          Comment

          • stress999
            New Member
            • Oct 2014
            • 12

            #6
            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
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3664

              #7
              This code should be in the VBA module for the form that has the two text controls on it.

              Comment

              • stress999
                New Member
                • Oct 2014
                • 12

                #8
                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 Sub
                Last edited by Rabbit; Nov 3 '14, 05:36 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

                Comment

                • twinnyfo
                  Recognized Expert Moderator Specialist
                  • Nov 2011
                  • 3664

                  #9
                  First, please use Code tags when posting Code.

                  What is the error that you are receiving? This will tell us how to troubleshoot.

                  Comment

                  • stress999
                    New Member
                    • Oct 2014
                    • 12

                    #10
                    The error occurred at line 10 and 15 in the original code you posted.
                    Line 15 kicks out compile error: "Method or data member not found" when click text box that represents "textDate2

                    Comment

                    • twinnyfo
                      Recognized Expert Moderator Specialist
                      • Nov 2011
                      • 3664

                      #11
                      What are the names of the controls on your form. You must either 1) replace the control names in the code with the names of your controls or 2) rename your controls to match what is in the code.

                      Comment

                      • stress999
                        New Member
                        • Oct 2014
                        • 12

                        #12
                        Twinnyfo, the controls on the form are the same as the code above...txtDate 1 and txtDate2.
                        I switched the control names. Now the compile error on LINES 14 states: Method or data member not found

                        Comment

                        • twinnyfo
                          Recognized Expert Moderator Specialist
                          • Nov 2011
                          • 3664

                          #13
                          I can only imagine that you don't have a text box named txtWarning.....

                          Comment

                          • stress999
                            New Member
                            • Oct 2014
                            • 12

                            #14
                            Thx twinnyfo got it...

                            Comment

                            • twinnyfo
                              Recognized Expert Moderator Specialist
                              • Nov 2011
                              • 3664

                              #15
                              Great to hear it! Let us know if you need anything else.

                              Comment

                              Working...