Check for existing date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3653

    #46
    When are you getting this error? It is more helpful to us to tell us exactly when and where so we can focus troubel shooting.

    Comment

    • didacticone
      Contributor
      • Oct 2008
      • 266

      #47
      As soon as i open the form... sorry

      Comment

      • didacticone
        Contributor
        • Oct 2008
        • 266

        #48
        I added some sample data to the table and that removed the messaged but the buttons are not disabling here is my code:

        Code:
        Option Compare Database
        Option Explicit
        Dim fCheckIn  As Boolean
        Dim fCheckOut As Boolean
        Private Sub CheckStatus()
        On Error GoTo EH
         
            CheckEmployee 1
            CheckEmployee 2
            CheckEmployee 3
            CheckEmployee 4
           
            Exit Sub
        EH:
            MsgBox "There was an error checking the status!  " & _
                "Please contact your Database Administrator.", _
                vbCritical, "WARNING!"
            Exit Sub
            
        End Sub
        Private Function CheckEmployee(Employee As Integer)
        On Error GoTo EH
         
            fCheckIn = Nz(DLookup("[SignIn]", "tbl_Master", _
                "[Employee] = " & Employee & " " & _
                "AND Format([SignIn], 'yyyy-mm-dd') = '" & _
                Format(Date, "yyyy-mm-dd") & "'"), False)
         
            fCheckOut = Nz(DLookup("[SignOut]", "tbl_Master", _
                "[Employee] = " & Employee & " " & _
                "AND Format([SignOut], 'yyyy-mm-dd') = '" & _
                Format(Date, "yyyy-mm-dd") & "'"), False)
         
            Me("cmdSignIn" & Employee).Enabled = Not fCheckIn
            Me("cmdSignOut" & Employee).Enabled = Not fCheckOut
         
            Exit Function
        EH:
            MsgBox "There was an error checking the status!  " & _
                "Please contact your Database Administrator.", _
                vbCritical, "WARNING!"
            Exit Function
        End Function
        
        
        Private Sub Form_Open(Cancel As Integer)
        On Error GoTo EH
         
            Me.Filter = "Employee = ''"
            Me.FilterOn = True
        
            Exit Sub
        EH:
            MsgBox "There was an error initializing the Form!  " & _
                "Please contact your Database Administrator.", _
                vbCritical, "WARNING!"
            Exit Sub
             CheckStatus
        End Sub

        Comment

        • didacticone
          Contributor
          • Oct 2008
          • 266

          #49
          I stepped through the code and the error is coming up here:
          Code:
          Private Function CheckEmployee(Employee As Integer)
          On Error GoTo EH
           
             [B]fCheckIn = Nz(DLookup("[SignIn]", "tbl_Master", _
                  "[Employee] = " & Employee & " " & _
                  "AND Format([SignIn], 'yyyy-mm-dd') = '" & _
                  Format(Date, "yyyy-mm-dd") & "'"), False)[/B]
           
              fCheckOut = Nz(DLookup("[SignOut]", "tbl_Master", _
                  "[Employee] = " & Employee & " " & _
                  "AND Format([SignOut], 'yyyy-mm-dd') = '" & _
                  Format(Date, "yyyy-mm-dd") & "'"), False)
           
              Me("cmdSignIn" & Employee).Enabled = Not fCheckIn
              Me("cmdSignOut" & Employee).Enabled = Not fCheckOut
           
              Exit Function
          EH:
              MsgBox "There was an error checking the status!  " & _
                  "Please contact your Database Administrator.", _
                  vbCritical, "WARNING!"
              Exit Function
          End Function
          The bold or first part of that code skips it to the error

          Comment

          • didacticone
            Contributor
            • Oct 2008
            • 266

            #50
            i changed the error coding to:
            Code:
            EH:
                MsgBox "There was an error with the search!  " & vbCrLf & vbCrLf & _
                    Err.Description & vbCrLf & vbCrLf & _
                    "Please contact your Database Administrator.", vbCritical, "WARNING!"
            and received the following error upon opening the form
            "data type mismatch in criteria expression."

            Comment

            • twinnyfo
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3653

              #51
              Post #48, Line 49 should be:
              Code:
              Me.Filter = "Employee = 0"
              I forgot to make that change.

              Also, lines 25 and 30 should reference the Field [Employee2] (see Post #39).

              Put a break point at the MsgBox in the EH. When it gets there, type ?Err.Descriptio n in the Immediate Window. Cut and paste the error.
              Last edited by twinnyfo; Mar 29 '18, 07:42 PM.

              Comment

              • twinnyfo
                Recognized Expert Moderator Specialist
                • Nov 2011
                • 3653

                #52
                And.... my thought is that we are trying to set a boolean variable as a date.... If we were setting it as a number, we would be OK. But that is a minor problem.

                If your tbl_master also has a primary key (an ID field), we can change the DLookup() to this:

                Code:
                    fCheckIn = Nz(DLookup("[ID]", "tbl_Master", _
                        "[Employee2] = " & Employee & " " & _
                        "AND Format([SignIn], 'yyyy-mm-dd') = '" & _
                        Format(Date, "yyyy-mm-dd") & "'"), False)
                
                    fCheckOut = Nz(DLookup("[ID]", "tbl_Master", _
                        "[Employee2] = " & Employee & " " & _
                        "AND Format([SignOut], 'yyyy-mm-dd') = '" & _
                        Format(Date, "yyyy-mm-dd") & "'"), False)
                Also noticed in post #48: Line 58 should be line 51 - you should not have that statement in the EH, but in the main sub.

                Comment

                • didacticone
                  Contributor
                  • Oct 2008
                  • 266

                  #53
                  Put a break point at the MsgBox in the EH. When it gets there, type ?Err.Descriptio n in the Immediate Window. Cut and paste the error.

                  Not sure what you want me to do here.

                  Comment

                  • twinnyfo
                    Recognized Expert Moderator Specialist
                    • Nov 2011
                    • 3653

                    #54
                    You achieved the same by putting the Err.Description in the MsgBox.

                    These other changes need to be made to fix any additional problems.

                    Comment

                    • didacticone
                      Contributor
                      • Oct 2008
                      • 266

                      #55
                      Sorry misunderstood.

                      "Data type mismatch in criteria expression"

                      Comment

                      • didacticone
                        Contributor
                        • Oct 2008
                        • 266

                        #56
                        Here is the entire code, with the new changes:

                        Code:
                        Option Compare Database
                        Option Explicit
                        Dim fCheckIn  As Boolean
                        Dim fCheckOut As Boolean
                        Private Sub CheckStatus()
                        On Error GoTo EH
                         
                            CheckEmployee 1
                            CheckEmployee 2
                            CheckEmployee 3
                            CheckEmployee 4
                           
                            Exit Sub
                        EH:
                            MsgBox "There was an error checking the status!  " & _
                                "Please contact your Database Administrator.", _
                                vbCritical, "WARNING!"
                            Exit Sub
                            
                        End Sub
                        Private Function CheckEmployee(Employee As Integer)
                        On Error GoTo EH
                         
                           fCheckIn = Nz(DLookup("[ID]", "tbl_Master", _
                                "[Employee2] = " & Employee & " " & _
                                "AND Format([SignIn], 'yyyy-mm-dd') = '" & _
                                Format(Date, "yyyy-mm-dd") & "'"), False)
                         
                            fCheckOut = Nz(DLookup("[ID]", "tbl_Master", _
                                "[Employee2] = " & Employee & " " & _
                                "AND Format([SignOut], 'yyyy-mm-dd') = '" & _
                                Format(Date, "yyyy-mm-dd") & "'"), False)
                         
                            Me("cmdSignIn" & Employee).Enabled = Not fCheckIn
                            Me("cmdSignOut" & Employee).Enabled = Not fCheckOut
                         
                            Exit Function
                        EH:
                            MsgBox "There was an error with the search!  " & vbCrLf & vbCrLf & _
                                Err.Description & vbCrLf & vbCrLf & _
                                "Please contact your Database Administrator.", vbCritical, "WARNING!"
                           
                            Exit Function
                        End Function
                        
                        
                        Private Sub Form_Open(Cancel As Integer)
                        On Error GoTo EH
                         
                            Me.Filter = "Employee = 0"
                            Me.FilterOn = True
                        CheckStatus
                            Exit Sub
                        EH:
                            MsgBox "There was an error with the search!  " & vbCrLf & vbCrLf & _
                                Err.Description & vbCrLf & vbCrLf & _
                                "Please contact your Database Administrator.", vbCritical, "WARNING!"
                            Exit Sub
                             
                        End Sub

                        Comment

                        • twinnyfo
                          Recognized Expert Moderator Specialist
                          • Nov 2011
                          • 3653

                          #57
                          Make the changes I've identified and these errors should go away.

                          Comment

                          • twinnyfo
                            Recognized Expert Moderator Specialist
                            • Nov 2011
                            • 3653

                            #58
                            And does this form work yet? Any additional errors?

                            Comment

                            • didacticone
                              Contributor
                              • Oct 2008
                              • 266

                              #59
                              "Data type mismatch in criteria expression" on form open

                              Comment

                              • twinnyfo
                                Recognized Expert Moderator Specialist
                                • Nov 2011
                                • 3653

                                #60
                                Which line is causing the error? Everything looks fine to me.

                                Comment

                                Working...