Results 1 to 11 of 11
  1. #1
    Rafegh is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    13

    how to solve the error when creating duplicate records?

    hi
    i have designed a form that creates records in a table. As I don't want to have duplicate records, I have made 3 fields as primary key in that table. By pressing a button in the form, records are created in the table. When User wrongly clicks the button with the same data on the form, access shows a message that records could not be created and the form could not be closed. I have tried to solve the problem with showing a message box and not entering the sub, but it didn't raise the problem. How can I fix this problem?

    This my code:



    Private Sub cmdCreate_Click()


    On Error GoTo aaa
    bb:


    For i = 0 To Me.SampleCounter + 1
    Me.LayerSubform.Visible = True
    LayerSubform.SetFocus
    DoCmd.GoToRecord , , acNewRec
    [LayerSubform]![ProjectCode] = ProjectCode
    [LayerSubform]![ProjectName] = ProjectName
    [LayerSubform]![Employer] = Employer
    [LayerSubform]![Contractor] = Contractor
    [LayerSubform]![SectionNo] = SectionNo
    [LayerSubform]![LabName] = LabName
    [LayerSubform]![TestType] = TestType
    [LayerSubform]![LayerType] = LayerType
    [LayerSubform]![AggType] = AggType
    [LayerSubform]![MineName] = MineName
    [LayerSubform]![SampleDistance] = SampleDistance
    [LayerSubform]![SampleCounter] = i



    Next i



    Exit Sub

    aaa:


    MsgBox (" data already Exist")
    Exit Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    You should not be using code to add records. Access does all this for you.

    The form should be connected to the table/query so IT can do the data work, either via :
    connected to table for direct entry
    or
    unbound form that runs an append query.

    This will prevent or enable to stop dupliacate entry.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    @rafegh,

    As ranman said you shouldn't be updating your table via code. If you use a bound form, Access will update your table and add the new record when you move off that record.

    However, I do have some questions/comments.
    Your table seems to be a mixture of ideas ProjectCode, Employer, Layer, MineName .... which indicates a non Normalized structure (at first glance).
    Can you tell us WHAT your database is about in plain English? If you have a data model or a jpg of your relationships window, could you attach a copy to your next post.
    Good luck with your project.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    I also do not understand use of code in this situation to create records.

    Personally, I would avoid compound primary key. Set an autonumber field as primary key and save that as foreign key in related tables. Can set the 3 fields as compound index to prevent duplicates.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Rafegh is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    13
    thank you for your answers. The database is for entering the results of tests during a highway construction project. I have used the append query somewhere else in my database but in this case, I want to create a number of records with different value for two fields of them. Because of that I thought that using a loop in vba will be better. This is not a critical problem for me, as I will be the user of the database and I know that I should not create duplicate records. Now my problem is that after creating this records, I want to update them through another form. How can I filter the records that I want (whose project code, section number and layer type has a specific value) to update?

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    Can you post a jpg (screen capture) of your Relationships window?

    You can open the form to do the edits and specify the record(s) to work on via the OPENARGS or you can filter the recordsource of the form.

  7. #7
    Rafegh is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    13
    This is the relationships of my database:
    Click image for larger version. 

Name:	RelationShips.jpg 
Views:	15 
Size:	235.5 KB 
ID:	17822

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    Options for filtering form:

    1. dynamic parameterized query as form RecordSource

    2. VBA code that constructs filter criteria and applies to form Filter property or pass filter to form when it opens, review http://www.allenbrowne.com/ser-62code.html
    DoCmd.OpenForm "formname", , , strCriteria
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    Rafegh,

    What is the purpose of the tables with suffixes in the names?
    LayerProperties, LayerProperties_1, LayerProperties_2 this is usually a sign of a non normalized structure.

    The key to database is to get the database designed to meet the business needs.

    Where are you in the database design and development process?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    Looks like tables are included in relationships builder multiple times. This is not correct. If you want to use a compound primary/foreign key then there should be 3 link lines from 4-LayerData to one copy of 3-LayerProperties. And 2 link lines from 3-LayerProperties to 2-LayerProperties. Compound primary/foreign keys will drive you nuts.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    What are the PKs of each of your Tables? Specifically Sections.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. how to solve this error?
    By ariansman in forum Forms
    Replies: 2
    Last Post: 04-30-2013, 06:16 AM
  2. Replies: 3
    Last Post: 03-13-2013, 04:00 PM
  3. How to solve syntex Error In INSERT INTO Statement?
    By kingsoh in forum Programming
    Replies: 0
    Last Post: 06-07-2012, 12:00 PM
  4. Replies: 6
    Last Post: 03-15-2012, 06:21 PM
  5. Replies: 3
    Last Post: 05-15-2011, 08:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums