Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

    Maybe I'm missing something basic. But in your earlier database29 -your composite index was not unique.
    In the latest version the composite index is unique. So when trying to change record 3 field to to "aaa", the underlying database system detects that dddaaa already exists. It then reports the error 3022 ("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.) as
    expected.

    You can capture and offer a custom error message for 3022 as CJ has described in #15
    Attached Thumbnails Attached Thumbnails Database29.accdb-comment.png  

  2. #17
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @ #15

    Yes, I explained that in #10, and what the problem, for me anyways, is.

  3. #18
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @#16 The first Database29, wasn't mine, it was created by CJ. I modified CJ's to be more in line with the question.
    In #10 I explained the issue, maybe a bit better than I did in the OP, because I tried to be brief in the OP of what I was looking for.

  4. #19
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Have you found a solution? Status?
    Can you adjust your validation process to include the composite value using DCount?

  5. #20
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Here's another attempt.

    In your Database29 in #14 try the following behind your SAVE button.

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: Command9_Click
    ' Purpose: This is the SAVE button click event
    '  This is a demo to identify whether the unique composite index would get
    '  a duplicate error if the current record change or addition would result
    '  in a duplication. And, if so, handle the error "gently" within the application
    '  rather than allowing raw msaccess error messages to be displayed
    '
    ' Procedure Kind: Sub
    ' Procedure Access: Private
    ' Author: Jack
    ' Date: 18-Jan-24
    ' ----------------------------------------------------------------
    Private Sub Command9_Click()
        ' Me.Dirty = False
        
        Dim ResponseNo As Integer
        Debug.Print CurrentDb.TableDefs("Table1").Indexes("F12").Fields 'For testing can be removed
        Debug.Print Field1 & Field2   'For testing can be removed
       
        If DCount("*", "Table1", "Field1 & Field2 = """ & Me.Field1 & Me.Field2 & """") > 0 Then
            ResponseNo = MsgBox("The combination (" & Field1 & Field2 & ") already exists. CANCELING THIS UPDATE.", vbOKOnly)
            If ResponseNo > 0 Then Me.Undo
        Else
             Debug.Print "Accepted Unique index value : " & Me.Field1 & Me.Field2 'For Testing can be removed
             DoCmd.GoToRecord , , acNext
        End If
    End Sub
    The idea here is:
    Identified the fields in your unique composite index,
    Created a DCount to check if the values to be recorded and indexed in composite index already exists
    If the value already exists, then give a meaningful message and cancel the intended change.
    Ensure the form works as expected when adding records

    I tried this in the Form Before update event, but although it worked partially, I still got the Access generated 3022 error. I found the above worked if I put the code behind the Save, and I did not get the 3022 Error

    Also, I changed the Form_Error event code as below:

    Code:
    Private Sub Form_Error(DataErr As Integer, response As Integer)
        MsgBox "oops! Error: " & DataErr & "  That would cause a duplicate record. CANCELING THIS UPDATE  "
        response = 0
        Me.Undo
    
    End Sub
    Note: The user can use the Save button or move to another record. You can adjust the messages to suit your purposes.

    There is a gif within the attached zip showing checking for duplicate values in the unique composite index when entering/modifying a record.
    Attached Files Attached Files
    Last edited by orange; 01-19-2024 at 11:54 AM. Reason: spelling

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA to check if meeting exists.
    By Homegrownandy in forum Programming
    Replies: 1
    Last Post: 02-12-2016, 09:27 AM
  2. Check to see if record already exists
    By zipaway in forum Programming
    Replies: 4
    Last Post: 06-05-2014, 09:16 AM
  3. Replies: 1
    Last Post: 03-21-2013, 02:14 PM
  4. Check if record exists, Check Number
    By burrina in forum Forms
    Replies: 9
    Last Post: 01-06-2013, 03:49 PM
  5. How to check if Table already exists
    By riaarora in forum Access
    Replies: 1
    Last Post: 08-12-2012, 09:48 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