简体   繁体   中英

Ensure Unique Records in Form (MS Access)

Using a Form in MS Access, I need to ensure only unique records are entered into the table.

It's unfortunately in a situation where I can't enforce unique records via the table's primary keys.

I have the following code in the BeforeUpdate for the Form, but it is not working. Any help would be greatly appreciated!

Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "[Role_Details]", "[Role] = " & Me.[ComboRole] & " AND [Session] = " & Me.[ComboSession]) > 0 Then
    MsgBox "Duplicate!"
    Cancel = True
    Me.[ComboSession].SetFocus
    Exit Sub
End If
End Sub

Note: The table name is "Role_Details". Field names are "Role" and "Session". With "ComboRole" and "ComboSession" being the Form Field Labels.

Any thoughts on where I've gone wrong here?

Updates##

When I open the DataSheet Form, it presents a popup box saying "Enter Parameter Value" and "frm_Role_Details.Session". I'm not sure why this is, but I can enter past it and open the Form.

Then, them i'm entering a record an error pops up saying "Run-time error '2465': Can't find the field '|1' referred to in your expression. Both Fields are Text Strings. I'm at a loss!

When concatenating in VBA, text fields require apostrophe delimiters for inputs.

If DCount("*", "[Role_Details]", "[Role] = '" & Me.[ComboRole] & "' AND [Session] = '" & Me.[ComboSession] & "'")> 0 Then

Date/Time fields use # delimiter.

Number fields do not use any delimiter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM