Prevent duplication of records in the entry form

lojain

New member
Local time
Today, 15:45
Joined
Sep 26, 2019
Messages
5
hello,
I'm working on Access 2016,

First, I have a regester table that contains three fields
(ID), (courseNo#) and (memberNo#)
I need to prevent duplecation in (courseNo#) and (memberNo#) in Form wizerd.
I know how to prevent duplection in one field but I don't know for two fields:D.
The tabe has many (courseNo#) and each (courseNo#) has many (memberNo#) are regestered in it.
It's same attache pic.

Second, I want alert to the user tell him the data is duplicated when entry duplicate data.

I would like to ask for your kind help in this matter.
Thanks.
 

Attachments

  • PIC OF Access.PNG
    PIC OF Access.PNG
    10.2 KB · Views: 497
Hi. The simplest approach is to create a multi field unique index; but if you want to control the message, you might have to use some VBA.
 
Thank you so much for quick reply,

When I try your code appeared this message,

(This error can occur when an event fails to run because the logic location of this event could not be evaluated. For example, if the form's OnOpen property is set to = [Field], this error occurs because an event or macro name is expected to run when the event starts).

I puted my table name and fields, and I chooes BeforeUpdate()

This is my code:
Code:
Private Sub N_BeforeUpdate()

If DCount("*", "Register1", "courseNo = " & Me.courseNo & " AND memberNo = " & Me.memberNo) > 0 Then
    MsgBox "some meaningful message"
    Cancel = True
    Me.courseNo.SetFocus
    Exit Sub
End If
End Sub

Thanks.
 
That should be

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
as gasman suggested, add code to the Form's BeforeUpdate event.
remove your code, and put it in the above event:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "Register1", "courseNo = " & Me.courseNo & " AND memberNo = " & Me.memberNo) > 0 Then
    MsgBox "some meaningful message"
    Cancel = True
    Me.courseNo.SetFocus
    Exit Sub
End If
End Sub
 
It dose not work!!:(

That statement helps no one.:banghead:

Have you checked you have values for the lookup, and if so is the data correct.?
The logic works, so clearly you are doing something wrong.

If you use the debugger and F8 with a breakpoint at the start of the code DLookup, you can inspect the values and then follow the path of the code.

Are both controls numeric?
 
Thank you all for your input. I have definitely used elements from all of the above, all of which are useful.:)
thank you.
 
Hi guys. Trying to achieve the same result based purely on the UMRN field but keep screwing up my syntax. I'm a VBA novice..

1626673489649.png


1626673618109.png
 
You do not say what the error is? :(
Is UMRN text at all?, if so you need to surround with single quotes.
 
You do not say what the error is? :(
Is UMRN text at all?, if so you need to surround with single quotes.
Hi Gasman. Yes UMRN is a text field. Which part of the text do I need to place the single quotes around??
 
If dcount("1","DemographicData", "UMRN = '" & Me!UMRN & "'") <> 0 Then
 
Got one step closer, then got this one..
1626681112353.png


Should there be something in here that then takes you to the record that already exists??
 
remove thw UMRN.Setfocus, the focus is already at that control
and you don't Setfocus (on any control) when you are on Validation Event.
 
If you want an expression to read the contents of a control before the focus moves elsewhere then simply use the Text property of the control.

Me.URMN.Text
 
Thanks guys!! that fixed the error. Now if I want to take the user to the record that already exists as part of this process?
 
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
With Me.RecordsetClone
    .FindFirst "UMRN = '" & Me!UMRN & "'"
    Cancel = (.NoMatch = False)
    If Cancel Then
        MsgBox "UMRN already exists"
        Me.UMRN.Undo
        Me.Bookmark = .Bookmark
    End If
End With
End Sub
 
Thanks for your prompt reply Arnel. Placed the code in but absolutely nothing happens when trying to trigger the code..

1627018982430.png
 
ooh, you have UMRN_BeforeUpdate.

copy the code from the Form's BeforeUpdate and paste it in UMRN_BeforeUpdate
replacing All the code (of UMRN_BeforeUpdate).

delete the Form's BeforeUpdate event.
 

Users who are viewing this thread

Back
Top Bottom