Prevent duplicates in same month in access (1 Viewer)

Usmantahir

New member
Local time
Tomorrow, 00:22
Joined
Oct 6, 2022
Messages
6
Hello All,

I have a access database to give pension for pensioners in transaction form i have 8000 accounts in my data table and i am using drop down list combo box to make transactions.

I want single entry in each month of every account if any user try to do duplicate entry ms access prevent to duplicate entry in same month.

I have these field in my transaction form
Date,account no,name of pensioner,amount of pension
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:22
Joined
Oct 29, 2018
Messages
21,474
Hi. Welcome to AWF!

One approach you could take is to create a multi-field unique index. I think that may be possible with using a calculated field to determine the month from your date field. Otherwise, another approach is to use the Form's BeforeUpdate event to validate the data entry to prevent any duplicates.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:22
Joined
Feb 19, 2002
Messages
43,280
To make the unique index, you're going to have to separate out yyyy and mm from the date unless your payments are always all on the same date.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:22
Joined
Oct 29, 2018
Messages
21,474
I think that may be possible with using a calculated field to determine the month from your date field.
I just did a quick test. Sadly, calculated fields can't be indexed. :(
 

Usmantahir

New member
Local time
Tomorrow, 00:22
Joined
Oct 6, 2022
Messages
6
Hi. Welcome to AWF!

One approach you could take is to create a multi-field unique index. I think that may be possible with using a calculated field to determine the month from your date field. Otherwise, another approach is to use the Form's BeforeUpdate event to validate the data entry to prevent any duplicates.
@theDBguy ,

Can you tell me please before update event code for validate data.
 
Last edited by a moderator:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:22
Joined
Feb 19, 2002
Messages
43,280
I just did a quick test. Sadly, calculated fields can't be indexed.
They are calculated on the fly. That is the point. If they don't permanently exist, they cannot be indexed.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:22
Joined
Feb 19, 2002
Messages
43,280
Code:
Dim yyyymm as string
Dim strSQL as string

yyyymm = Format(Me.YourDate, "yyyymm")
strSQL = Format(YourDate, "yyyymm") & " = '" & yyyymm & "'"
strSQL = strSQL & " AND YourID = " & Me.YourID

If dCount("*", "yourtable", strSQL ) > 0 Then
    msgbox " This payment already exists for " & yyyymm  & "for " & Me.YourID, vbOKOnly
    Cancel = True
    Me.YourDate.SetFocus
    Exit Sub
End If

Make sure to check the strings to ensure they are valid. Getting the quotes in the right place isn't all that easy when writing pseudo code.
 

Usmantahir

New member
Local time
Tomorrow, 00:22
Joined
Oct 6, 2022
Messages
6
Code:
Dim yyyymm as string
Dim strSQL as string

yyyymm = Format(Me.YourDate, "yyyymm")
strSQL = Format(YourDate, "yyyymm") & " = '" & yyyymm & "'"
strSQL = strSQL & " AND YourID = " & Me.YourID

If dCount("*", "yourtable", strSQL ) > 0 Then
    msgbox " This payment already exists for " & yyyymm  & "for " & Me.YourID, vbOKOnly
    Cancel = True
    Me.YourDate.SetFocus
    Exit Sub
End If

Make sure to check the strings to ensure they are valid. Getting the quotes in the right place isn't all that easy when writing pseudo code.
@Pat Hartman

Thanks a lot sir
I tried to this code and i hope it work.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:22
Joined
May 7, 2009
Messages
19,245
see table Transactions in Design view.
see the Before Change macro I made to prevent duplicate.
i only check the Account No and Month/Year of the transactions.

now Open Transaction table and input some data to test.
 

Attachments

  • Pensioner_db.accdb
    396 KB · Views: 94

Usmantahir

New member
Local time
Tomorrow, 00:22
Joined
Oct 6, 2022
Messages
6
see table Transactions in Design view.
see the Before Change macro I made to prevent duplicate.
i only check the Account No and Month/Year of the transactions.

now Open Transaction table and input some data to test.
@arnelgp

Its working sir can you explain how I apply it to my DB
 

Usmantahir

New member
Local time
Tomorrow, 00:22
Joined
Oct 6, 2022
Messages
6
see table Transactions in Design view.
see the Before Change macro I made to prevent duplicate.
i only check the Account No and Month/Year of the transactions.

now Open Transaction table and input some data to test.
Sir i have linked tables for transactions form how i apply this macro
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:22
Joined
May 7, 2009
Messages
19,245
you Edit the table in the BE and put the macro there.
 

Users who are viewing this thread

Top Bottom