Prevent Duplicate Records, Query Before Creating New Records

后端 未结 1 1211
情话喂你
情话喂你 2021-01-07 01:04

Setup: I am creating an ms access database to record the results of experiments that will be done by myself and others. I have created a form and a subform;

1条回答
  •  时光说笑
    2021-01-07 01:23

    I believe you'll want two things, depending on how user-friendly you want the interface.

    Unique Index

    Add a multi-column unique index on the columns in the Settings table in which their combination of values you don't ever want to be duplicated in another row. Can read how in the link provided by KFleschner in the comments to your original post, or check out the second answer of this question which has a screenshot to go along with the steps: Can we create multicolumn unique indexes on MS access databases?. This will disallow duplicates in the Settings table.

    For instance, if your settings and experiments were for computer rigs and you had a Settings table with the following columns:

    SettingID, RAM_GB, CPU_GHz

    Then your primary key would be (SettingID) and your multi-column unique index would be on (RAM_GB, CPU_GHz), because you only want one record with the same RAM capacity and CPU speed.

    In database language, your primary key, SettingID, will be what is known as a surrogate key. And the new multi-column unique index will be what is known as a natural key. Both can be used to identify a unique row, but the primary key (the surrogate key) is what is used in any foreign key relationships, such as SettingID in your Experiments table.

    This by itself will prevent the duplicate issue, since it will be enforced at the database level. However, it won't automatically make your form jump to the record with a matching natural key. Instead Access will alert the user with a message along the lines of you've entered a record that violated an index. But nothing more. The user will have to cancel out of the new record and go find the matching one himself.

    Before Update Event

    The Before Insert event triggers upon the first character of input for a new record. See http://msdn.microsoft.com/en-us/library/office/ff835397.aspx for details. This is too soon, you want the Before Update event. And add code to the event like this:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
        Set rst = Me.RecordsetClone
        rst.FindFirst "[SettingID] <> " & Me.SettingID & " AND [RAM_GB] = " & Me.RAM_GB & " AND [CPU_GHz] = " & Me.CPU_GHz
        If Not rst.NoMatch Then
            Cancel = True
            If MsgBox("Setting already exist; goto existing record?", vbYesNo) = vbYes Then
                Me.Undo
                DoCmd.SearchForRecord , , acFirst, "[SettingID] = " & rst("SettingID")
            End If
        End If
        rst.Close
    End Sub
    

    This code assume a few things:

    1. [RAM_GB] and [CPU_GHz] are the columns of your multi-column unique index.
    2. [SettingID] is the column in your primary key.

    Tweak the column names and such to your needs and then you'll also have a way to prompt and auto-navigate to the existing record.

    0 讨论(0)
提交回复
热议问题