Results 1 to 12 of 12
  1. #1
    adamcm is offline Novice
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    12

    Access Questions for a Newbie - Duplicates using the Next Record button

    Hi all,

    I'm jsut trying to set up a simple Access DB for a co-worker, but I wanted to add a little bit of extra features. One thing that I am really having a tough time with is error checking and not allowing duplicates in the DB.

    Each of my forms are set up in the following format:
    Form Fields (Name, etc)
    Navigation buttons (First, Previous, Next, and Last)
    Record buttons (Add, Update, and Delete)

    I figured out how to display an error message to the user when entering a duplicate and clicking on the Add button by Exporting the Macros to VB, and by adding a DLookup. However, the Next button will allow for the record to be added as well (which I didn't know). Is there anyway to prevent the Next button from adding a duplicate record within the table? The only way I have found so far is by making the field indexed. This is fine except I can't display a nice error message to the user.

    Any ideas?

    Thanks!

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Actually you could replace the system error message in the OnError event of the form. You just need to know the error #.

  3. #3
    adamcm is offline Novice
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    12
    Quote Originally Posted by RuralGuy View Post
    Actually you could replace the system error message in the OnError event of the form. You just need to know the error #.
    Hey thanks for the help. But I don't think it's an actual error is it? I believe the Next Record functionality always will add the record won't it? I'm wondering if it is possible to do a duplicate check on using the Next Record button?

    Thanks.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Test for duplicates in the BeforeUpdate event of the form and Cancel the event if there is a duplicate. That will catch *any* method that tries to create a duplicate. Next, Previous, Save, Close, any method.

  5. #5
    adamcm is offline Novice
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    12
    Hey, that works great!

    The only issue I have is after receiving the message that the "Role already exists in the DB", I receive another message:

    "The value violates the validation rule for the field or record".

    I don't see any other validations on my form and have no idea why this is appearing. Any ideas?

    Thanks again!

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    If you open the table in design mode, what do you have in the validation for that field?

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I tend to do all of my validation in a form and none at the table level so I am in complete control of the messages.

  8. #8
    adamcm is offline Novice
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    12
    There is nothing listed for RoleName in the Validation Text or Validation Rule. I have modified some of my fields including:

    Required: Yes
    Allow Zero Length: No
    Indexed: Yes (No Duplicates)

    The rest I believe are defaults. Do I need to remove these?

    Thanks!

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Are you canceling the BeforeUpdate event when the user puts in a duplicate? That will stop the Update.

  10. #10
    adamcm is offline Novice
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    12
    Quote Originally Posted by RuralGuy View Post
    Are you canceling the BeforeUpdate event when the user puts in a duplicate? That will stop the Update.
    Here is the code...

    If (Not IsNull(DLookup("[ResourceName]", _
    "Resource", "[ResourceName] ='" _
    & Me!ResourceName & "'"))) Then
    MsgBox "Resource has already been entered in the Database."
    Cancel = True
    Me!ResourceName.Undo
    End If

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Instead of the DLookup() function how about using a .FindFirst with the RecordSource RecordsetClone (DAO)? It should run faster than the Domain function as well.

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    BTW, when you post event code it is useful if you start your copy with Private Sub... and end with End Sub.

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

Similar Threads

  1. Command button help - delete record
    By Zukster in forum Forms
    Replies: 0
    Last Post: 08-27-2009, 08:47 AM
  2. Best Design for Multiple Y/N Questions on each record
    By DanielHochman in forum Database Design
    Replies: 0
    Last Post: 07-20-2009, 02:51 PM
  3. make Add Record button work
    By janjan_376 in forum Forms
    Replies: 3
    Last Post: 06-05-2009, 04:22 AM
  4. Find Record button error
    By Zedlexx in forum Forms
    Replies: 0
    Last Post: 03-11-2009, 09:26 AM
  5. newbie questions
    By bigmac in forum Access
    Replies: 0
    Last Post: 10-07-2008, 12:53 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