Viewing duplicate entry

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashenton
    New Member
    • Oct 2008
    • 24

    Viewing duplicate entry

    Hi,

    I have a stock database and have recently solved an issue with duplication.

    When a user enters a duplicate value in the 'DAMS_Number' field on a form, a message box alerts the user that the value already exists.

    What I would like is for the form to display the corresponding record as soon as the user clicks 'Ok' on the message box.

    The code I have for the duplicate alert (which I obtained from this site btw!) is as follows:

    Code:
    Private Sub DAMS_Number_AfterUpdate()
    
    Dim tmpID As Long
      
       tmpID = Nz(DLookup("[DAMS Number]", "DAMS Stock", "[DAMS Number]=" & Me!DAMS_Number), 0)
       If tmpID <> 0 Then
            MsgBox "This DAMS asset already exists in the database", vbOKOnly
       End If
    
    End Sub
    Can anybody help me with this?

    Thanks!
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Try replacing your code above with the following revised code:

    Code:
    Private Sub DAMS_Number_AfterUpdate() 
      
    Dim tmpID As Long 
      
       tmpID = Nz(DLookup("[DAMS Number]", "DAMS Stock", "[DAMS Number]=" & Me!DAMS_Number), 0) 
       If tmpID <> 0 Then 
            MsgBox "This DAMS asset already exists in the database", vbOKOnly 
           Me.Filter = "[DAMS Number] = " & Me!DAMS_Number
           Me.FilterOn = True
       End If 
      
    End Sub

    Comment

    • ashenton
      New Member
      • Oct 2008
      • 24

      #3
      When I try the revised code I get this error:

      Run-time error '3314':

      The field 'DAMS Stock.Model' cannot contain a Null value because the Required property for this field is set to true. Enter a value in this field.


      I need certain values to be entered when creating a new record where one does not exist, but having the Required property set to true results in the above error.

      If I set all the Required properties to False, I get this error:

      Run-time error '3022':

      The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.


      The primary key is a field which is set to AutoNumber and has no real purpose other than to number each entry. The index for 'DAMS_Number' is set to 'Yes (No Duplicates) to prevent duplicate entries for the same asset.

      Any ideas?

      Comment

      • ashenton
        New Member
        • Oct 2008
        • 24

        #4
        Also, if I set the Index to Yes (Duplicates ok) I get a pop-up box asking for the 'DAMS_Number' value which I have just entered in the form. When I enter the value in the box and press enter it goes to the existing entry.

        Having duplicates allow whilst it automatically notifies and takes the user to the corresponding record I can live with, but I really don't want the user to have to type the asset number in twice!

        Comment

        • puppydogbuddy
          Recognized Expert Top Contributor
          • May 2007
          • 1923

          #5
          please undo all the changes you made other than the changes I gave you.....otherwi se you will be giving me a moving target. The code I gave you should work (if you make the correction below) because it is in the same loop that generates the message box and uses the same criteria.

          If you've made the correction and it still does not work, please post your code and provide details.....but do not make any other changes.


          Just change this line:
          Me.Filter = "[DAMS Number] = " & Me!DAMS_Number

          To this:
          Me.Filter = "[DAMS Number] = " & tmpID

          Comment

          • ashenton
            New Member
            • Oct 2008
            • 24

            #6
            It still doesn't work. I've returned everything to how it was before you sent me the code (except the code of course!) and changed the line you specified.

            Certain values are set to be required and the Required property of DAMS_Number is set to 'Yes (No Duplicates)'

            The code now reads

            Code:
            Private Sub DAMS_Number_AfterUpdate()
              
            Dim tmpID As Long
              
               tmpID = Nz(DLookup("[DAMS Number]", "DAMS Stock", "[DAMS Number]=" & Me!DAMS_Number), 0)
               If tmpID <> 0 Then
                    MsgBox "This DAMS asset already exists in the database", vbOKOnly
                   Me.Filter = "[DAMS Number] = " & tmpID
                   Me.FilterOn = True
               End If
              
            End Sub
            I still get run-time error 3314 after clicking 'ok' in the message box.

            According to VB it is the line which contains 'Me.FilterOn = True' which is causing the issue (I'm guessing anyway as it is highlighted in yellow)

            Comment

            • puppydogbuddy
              Recognized Expert Top Contributor
              • May 2007
              • 1923

              #7
              I think all you need to do now is set the Required property of DAMS_Number to No. Is that a problem for you in terms of control? If it is, I will need to give you another method. Let me know.

              Comment

              • ashenton
                New Member
                • Oct 2008
                • 24

                #8
                I set the required property for DAMS_Number to no but there are several other fields which are required (as well as DAMS_Number) which create the 3314 error.

                DAMS_Number is the stock asset number and is required for more or less everything I do. It is the primary identifier on our stock so must be included in the database :)

                Comment

                • puppydogbuddy
                  Recognized Expert Top Contributor
                  • May 2007
                  • 1923

                  #9
                  You should not have to set the required of any field other than the DAMS Number. We are only filtering on the DAMS number. What message did you get exactly?

                  Comment

                  • puppydogbuddy
                    Recognized Expert Top Contributor
                    • May 2007
                    • 1923

                    #10
                    See if this works for you without changing the field settings that you had.
                    Code:
                    Private Sub DAMS_Number_AfterUpdate() 
                      
                    Dim tmpID As Long 
                    Dim strFilter As String
                      
                    tmpID = Nz(DLookup("[DAMS Number]", "DAMS Stock", "[DAMS Number]=" & Me!DAMS_Number), 0) 
                       If tmpID <> 0 Then 
                            MsgBox "This DAMS asset already exists in the database", vbOKOnly 
                           strFilter = "[DAMS Number] = " & tmpID 
                           DoCmd.OpenForm Me, , , strFilter 
                       End If 
                      
                    End Sub

                    Comment

                    • ashenton
                      New Member
                      • Oct 2008
                      • 24

                      #11
                      Hi,

                      The existing code gives the following error:

                      Run-time error '3314':

                      The field 'DAMS Stock.####' cannot contain a Null value because the Required property for this field is set to true. Enter a value in this field.


                      Where #### is the item on the form whose property is set to required.

                      I need certain values to be entered when creating a new record where one does not exist, but having the Required property set to true results in the above error.

                      The new code gives me the following error:

                      Run-time error '2498':

                      An expression you entered is the wrong data type for one of the arguments.


                      When I click debug it highlights this line:

                      Code:
                      DoCmd.OpenForm Me, , , strFilter
                      Certain properties on the form need to be required for asset tracking. Unfortunately the required entries are of different data types.

                      The 'DAMS Number' field is a number
                      The 'Model' field is a dropdown list
                      The 'Status' field is a dropdown list
                      The 'Date In Stock' field is a date

                      All four of those fields have to be entered in a new record for the purpose of asset tracking. They were required before I started adding code.

                      I don't know much about VBA code and this is turning out to be more complex than I expected!!

                      Comment

                      • puppydogbuddy
                        Recognized Expert Top Contributor
                        • May 2007
                        • 1923

                        #12
                        Be patient. It may take a few times back and forth, but we will get it. The error is addressed to the field contained in strFilter, not the other fields.

                        Try this:

                        Private Sub DAMS_Number_Aft erUpdate()

                        Dim tmpID As Long
                        Dim strFilter As String

                        tmpID = Nz(DLookup("[DAMS Number]", "DAMS Stock", "[DAMS Number]=" & Me!DAMS_Number) , 0)
                        If tmpID <> 0 Then
                        MsgBox "This DAMS asset already exists in the database", vbOKOnly
                        strFilter = "[DAMS Number] = tmpID"
                        DoCmd.OpenForm Me, , , strFilter
                        End If

                        End Sub

                        Comment

                        • ashenton
                          New Member
                          • Oct 2008
                          • 24

                          #13
                          It gives the same error in the same line :(

                          Comment

                          • puppydogbuddy
                            Recognized Expert Top Contributor
                            • May 2007
                            • 1923

                            #14
                            Try this:
                            Code:
                            Private Sub DAMS_Number_AfterUpdate() 
                            
                            Dim tmpID As Long 
                            Dim lngFilter As Long 
                            
                            tmpID = Nz(DLookup("[DAMS Number]", "DAMS Stock", "[DAMS Number]=" & Me!DAMS_Number), 0) 
                            If tmpID <> 0 Then 
                            MsgBox "This DAMS asset already exists in the database", vbOKOnly 
                            lngFilter = "[DAMS Number] = " & tmpID 
                            DoCmd.OpenForm Me, , , lngFilter 
                            End If 
                            
                            End Sub

                            Comment

                            • ashenton
                              New Member
                              • Oct 2008
                              • 24

                              #15
                              I've got a whole new error now lol

                              Run-time error '13':

                              Type mismatch


                              It occurs in this line:

                              Code:
                              lngFilter = "[DAMS Number] = " & tmpID

                              Comment

                              Working...