Hi is there a more intuitive way I can display thee standard “The changes you requested to the table were not successful because they would create duplicate values in the index” replaced with a user message of my own?
Thanks, Darren.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Capture system message
You cannot do this at the table or query level, but you can use the On Error event of the form used for data entry. The error number of the duplicate index error is 3022.
For example, if there is a field ID that must be unique:
Private Sub Form_Error(DataErr As Integer, Response As Integer) Select Case DataErr Case 3022 ' duplicate index ' Activate offending control Me.ID.SetFocus ' Display custom message MsgBox "You have entered a duplicate ID value, you dummy!", _ vbExclamation ' Suppress built-in error message Response = acDataErrContinue Case Else ' Display built-in error message Response = acDataErrDisplay End Select End Sub
Hi is there a more intuitive way I can display thee standard “The changes you requested to the table were not successful because they would create duplicate values in the index” replaced with a user message of my own?
Thanks, Darren.
I do this in two ways:
In an earlier post I asked if there wa sa way of trapping the index duplicate keys message that shows and I was givne this reply by John H…
I do this in two ways:
“Use the Before Update event of the form to count whether any records already exist that would lead to a duplicate. If so display your own message, then Cancel the update, or
If I am using combo boxes, only offer values that have not already been used, so that you can’t use values that would lead to duplicates.”
This looks cool but I am unsure how to code. Any guiance would be great.
Thanks, Darren
In an earlier post I asked …
I have moved your new topic into the original topic because it’s a follow-up question.
See the attached sample database. Both approaches are demonstrated in frmDemo.
The Before Update event of the form tests whether the value entered for UniqueNumber hasn’t already been used:
Private Sub Form_BeforeUpdate(Cancel As Integer) If DCount("*", "tblTest", "UniqueNumber = " & _ Me.UniqueNumber & " AND ID " & Me.ID) > 0 Then Me.UniqueNumber.SetFocus MsgBox "The value " & Me.UniqueNumber & _ " has already been used.", vbExclamation Cancel = True End If End Sub
The combo box for UniqueLookup has a row source that only displays available values:
SELECT ID, Description FROM tblLookup WHERE ID Not In (SELECT UniqueLookup FROM tblTest) OR ID = Forms!frmDemo!UniqueLookup
The combo box is requeried when the user moves to another record:
Private Sub Form_Current() Me.UniqueLookup.Requery End Sub
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.