MsgBox asking what to do about duplicate value Ms Access Gurus

If you are helped, please return the favor and help support this site, thank you.

Unique Values using Access Form

Use a control BeforeUpdate event to ensure that a value is unique, not a duplicate. If another record has the value already, give the user a choice of undoing changes and moving to that record, or staying to edit the value

Quick Jump

Goto Top  

Logic

Although a unique index may be set on a field, the error message to a user entering data isn't friendly.

built-in error message for duplicate value isn't friendly

The solution is to use the BeforeUpdate event of a control to either move to another existing record or stay on the current record and make changes if a value entered isn't unique.

custom error message for duplicate value is friendly

Goto Top  

VBA

Modify to replace code in the CUSTOMIZE section. Your procedure name (MyControlname_BeforeUpdate) will undoubtably be different

'*************** Code Start *****************************************************
' Purpose  : test for unique value using control BeforeUpdate event
'            if not unique, move to other record or stay and edit
' Author   : crystal (strive4peace)
' Code List: www.msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/UniqueValues.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark your changes. Use at your own risk.
'-------------------------------------------------------------------------------

Private Sub MyControlname_BeforeUpdate(Cancel As Integer) 
's4p 221101 ... 11
'make sure value is unique on new or modified record
'  if not string, then modify delimiter too
'lookup table design: ideally there will be a Unique Index
'  on the value to lookup that isn't to be duplicated
'  but not necessary for this procedure to work
'assumes Value is a string and primary key (PK) is a long integer
'written generically so MyControlname will be different for you!!

   'dimension variables
   Dim nRecordID As Long _ 
      ,sWhere As String _ 
      ,sValue As String _ 
      ,sMsg As String _ 
      ,sFieldnameValue As String _ 
      ,sFieldnamePK As String _ 
      ,sTablename As String 
      
    '----------------------------------------- CUSTOMIZE
   sFieldnameValue =  "PropName" 'String, Name of Short Text field
   sFieldnamePK =  "PropertyID"  'String, Name of Long Integer/Autonumber PK field
   sTablename =  "dm_Property"   'String, name of table
   '------------------------------------------
      
   'get value to test
   With Me.ActiveControl 
      If IsNull(.Value) Then Exit Sub 
      sValue = Trim(.Value) 
   End With 
   
   'get primary key for changed record
   '  -99 will be used if PK doesn't yet have a value
   nRecordID = Nz(Me(sFieldnamePK),-99) 
   
   'construct WHERE clause
   sWhere =  "(" & sFieldnameValue &  "= '" & sValue &  "')"
   
   'if not new record, add PK to WHERE clause
   If nRecordID <> -99 Then 
      sWhere = sWhere _ 
         &  " AND (" & sFieldnamePK &  " <> " & nRecordID &  ")"
   End If 
   
   'get PK of other record with this value
   '  -99 means the value wasn't found on other record
   nRecordID = Nz(DLookup( _ 
         sFieldnamePK _ 
         ,sTablename _ 
         ,sWhere),-99) 
         
   If nRecordID <> -99 Then 
      'value found on another record
      sMsg =  "'" & sValue &  "' already exists" _ 
         & vbCrLf &  "    OK = Move to record" _ 
         & vbCrLf &  "    Cancel = Stay here and fix the value"
      
      If MsgBox(sMsg _ 
         ,vbOKCancel _ 
         , "Duplicate Value. Move or Stay?") = vbOK _ 
      Then 
         'move to record
         With Me.RecordsetClone 
            .FindFirst sFieldnamePK &  "=" & nRecordID 
            If Not .NoMatch Then 
               Me.Undo 
               Cancel = True 
               Me.Bookmark = .Bookmark 
            Else
               'code to remove filter and look again -- YOU write
            End If 
         End With 'RecordsetClone
      Else 
         'cancel control update
         Cancel = True 
      End If 
   End If 
End Sub 
'*************** Code End *******************************************************
Code was generated with colors using the free Color Code add-in for Access.

Goto Top  

Reference

Form.ActiveControl property

MsgBox function

Form.RecordsetClone property

Application.DLookup method

Recordset.FindFirst method (DAO)

Goto Top  


Download

Download text file with code: UniqueValue_MyControlname_BeforeUpdate__TXT.zip

If you have trouble with the download, you may need to unblock the ZIP file, aka remove Mark of the Web, before extracting the BAS file. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm

Goto Top  

Backstory

Even if a table has a unique index to disallow duplicate values, it is nice to take advantage of the control you can get with forms to give a friendly message so user knows what's wrong and can either fix it, or move to the record they are trying to create again

If you like this page, please let me know, thank you. Donations are always appreciated

Share with others

Here's the link for this page in case you want to copy it and share it with someone:

https://msaccessgurus.com/VBA/UniqueValues.htm

or in old browsers:
http://www.msaccessgurus.com/VBA/UniqueValues.htm

Get Help with Access so you can do it yourself

Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.

While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I'll give you lots of links to good resources.

Data structure is most important to get right. Users also need clear messages. Do you want your application to be better? I'd love to help you. Email me at training@msAccessGurus.com

~ crystal

the simplest way is best, but usually the hardest to see

Goto Top