Prevent duplication of records in the entry form (1 Viewer)

Gigitty

Member
Local time
Today, 02:07
Joined
Mar 29, 2007
Messages
52
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?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:07
Joined
May 7, 2009
Messages
19,230
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
 

Gigitty

Member
Local time
Today, 02:07
Joined
Mar 29, 2007
Messages
52
Thanks for your prompt reply Arnel. Placed the code in but absolutely nothing happens when trying to trigger the code..

1627018982430.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:07
Joined
May 7, 2009
Messages
19,230
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.
 

Gigitty

Member
Local time
Today, 02:07
Joined
Mar 29, 2007
Messages
52
So now it's having an issue with the Index I created on that field. Ok to remove the Index??

1627019610395.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:07
Joined
May 7, 2009
Messages
19,230
Replace Me.UMRN.Undo
With Me.Undo
 

Gigitty

Member
Local time
Today, 02:07
Joined
Mar 29, 2007
Messages
52
Thanks So Much Arnel!! Syntax sensitivities always bring me undone.

Thankyou to everyone for your assistance.
Gigitty
 

accessonly11

Member
Local time
Today, 14:07
Joined
Aug 20, 2022
Messages
91
dear access family
i want some more feature.
this code is working fine
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "OrderDetails", "ProductID = " & Me.ProductID & " AND OrderID = " & Me.OrderID) > 0 Then
    MsgBox "This Product already entered"
    Cancel = True
    Me.ProductID.SetFocus
    Exit Sub
End If

End Sub
i want that after entring duplicate entry, and after msg box ok button clicked, the current entry cancelled and and previous entry of same product set focused.

View attachment 102857
how it could be possible in vba.
 

accessonly11

Member
Local time
Today, 14:07
Joined
Aug 20, 2022
Messages
91
dear access family
i want some more feature.
this code is working fine
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "OrderDetails", "ProductID = " & Me.ProductID & " AND OrderID = " & Me.OrderID) > 0 Then
    MsgBox "This Product already entered"
    Cancel = True
    Me.ProductID.SetFocus
    Exit Sub
End If

End Sub
i want that after entring duplicate entry, and after msg box ok button clicked, the current entry cancelled and and previous entry of same product set focused.

View attachment 102857
how it could be possible in vba.
1661671554440.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:07
Joined
May 7, 2009
Messages
19,230
here check the code in this demo.
i added code to the ProductID BeforeUpdate event.
 

Attachments

  • SetfocusOnDuplicate.accdb
    516 KB · Views: 73

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:07
Joined
May 7, 2009
Messages
19,230
it's a Validation code (BeforeUpdate), so you cannot just leave the control/record it is validating (or setFocus on another control).
you need to complete the validation first.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:07
Joined
Sep 21, 2011
Messages
14,238
it's a Validation code (BeforeUpdate), so you cannot just leave the control/record it is validating (or setFocus on another control).
you need to complete the validation first.
Thank you
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:07
Joined
Feb 19, 2002
Messages
43,233
@accessonly11 Please do not hijack old threads to ask new questions. Start a NEW thread for each new question. If you want to refer to an existing thread, post a link.

The solution posted by @arnelgp is incomplete. There is no validation in the form's BeforeUpdate event so you can add rows without a ProductID.
 

accessonly11

Member
Local time
Today, 14:07
Joined
Aug 20, 2022
Messages
91
@accessonly11 Please do not hijack old threads to ask new questions. Start a NEW thread for each new question. If you want to refer to an existing thread, post a link.

The solution posted by @arnelgp is incomplete. There is no validation in the form's BeforeUpdate event so you can add rows without a ProductID.
Thanks, i was not aware,

it's a Validation code (BeforeUpdate), so you cannot just leave the control/record it is validating (or setFocus on another control).
you need to complete the validation first.
and thanks to arnelgp and all of you, now this function is working after putting code given by arnelgp
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:07
Joined
Feb 19, 2002
Messages
43,233
Did arnel fix the problem I mentioned? Or do you just not care that the validation is incomplete?
 

Users who are viewing this thread

Top Bottom