Connecting Tech Pros Worldwide Forums | Help | Site Map

Just a warning on duplicate records

Newbie
 
Join Date: Dec 2006
Posts: 17
#1: Dec 3 '06
I am trying to set up a message box warning (or open to some sort of other warning) when a duplicate record is entered. I do want to allow duplicate records but I am looking to warn when it does occur.

Example of warning: Check number all ready entered are you sure you want to continue.

Any Ideas?



msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,875
#2: Dec 3 '06

re: Just a warning on duplicate records


Quote:

Originally Posted by jjstevens

I am trying to set up a message box warning (or open to some sort of other warning) when a duplicate record is entered. I do want to allow duplicate records but I am looking to warn when it does occur.

Example of warning: Check number all ready entered are you sure you want to continue.

Any Ideas?

When relevant field is entered on form (lets call it RecNo for now) then you need a before update event to check if number is already entered.

Expand|Select|Wrap|Line Numbers
  1. Private Sub RecNo_BeforeUpdate()
  2. Dim rslt As Integer
  3.  
  4.    If nz(DLookup("[RecNo]", "TableName", "[RecNo]=" & Me.RecNo),0) <> 0 Then
  5.       rslt = Msgbox ("This number has already been entered. Do you wish to continue?", vbYesNo)
  6.       If rslt = vbNo Then
  7.          Me.RecNo = Null
  8.          Me.RecNo.SetFocus
  9.       End If
  10.    End If
  11.  
  12. End Sub
  13.  
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,722
#3: Dec 3 '06

re: Just a warning on duplicate records


Mary's answer exactly matches the question.
However, in most circumstances you may be better served not entering a record number as such but allowing Access to supply an AutoNumber for the Primary Key. There are reasons why this is not always appropriate, but should always be considered.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,875
#4: Dec 4 '06

re: Just a warning on duplicate records


Quote:

Originally Posted by NeoPa

Mary's answer exactly matches the question.
However, in most circumstances you may be better served not entering a record number as such but allowing Access to supply an AutoNumber for the Primary Key. There are reasons why this is not always appropriate, but should always be considered.

This won't allow for duplicate entry Ade which seems to be a requirement.

Mary
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,722
#5: Dec 4 '06

re: Just a warning on duplicate records


Quote:

Originally Posted by mmccarthy

This won't allow for duplicate entry Ade which seems to be a requirement.

Mary

Quite right.
I must have misread it the first time because it states the requirement quite clearly. Sorry - ignore my post.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,875
#6: Dec 4 '06

re: Just a warning on duplicate records


Quote:

Originally Posted by NeoPa

Quite right.
I must have misread it the first time because it states the requirement quite clearly. Sorry - ignore my post.

Now Ade

I could never just ignore you. :D

Mary
Newbie
 
Join Date: Dec 2006
Posts: 17
#7: Dec 5 '06

re: Just a warning on duplicate records


Thank you all, this solution works great.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,875
#8: Dec 5 '06

re: Just a warning on duplicate records


Quote:

Originally Posted by jjstevens

Thank you all, this solution works great.

You're welcome.
Newbie
 
Join Date: Feb 2007
Posts: 2
#9: Feb 15 '07

re: Just a warning on duplicate records


Hello,
I've tried adding this to my table to get it to flash up a warning for duplicate postcodes, but it doesn't work.

Is it because I'm using a string instead of a number?
The error message I get is Run Time error 3075:
Syntax error (missing operator) in query expression '[Postcode]=TN8 6HR'

my code is:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Postcode_BeforeUpdate(Cancel As Integer)
  2. Dim rslt As Integer
  3.  
  4.    If Nz(DLookup("[Postcode]", "Addresses", "[Postcode]=" & Me.Postcode), 0) <> 0 Then
  5.       rslt = MsgBox("This number has already been entered. Do you wish to continue?", vbYesNo)
  6.       If rslt = vbNo Then
  7.          Me.Postcode = Null
  8.          Me.Postcode.SetFocus
  9.       End If
  10.    End If
  11. End Sub
can you help?
btw, I entered it in on the form design screen under Events >> Before Update as an Event Procedure
Newbie
 
Join Date: Feb 2007
Posts: 2
#10: Feb 15 '07

re: Just a warning on duplicate records


Quote:

Originally Posted by BrerBunny

Hello,
I've tried adding this to my table to get it to flash up a warning for duplicate postcodes, but it doesn't work.

Is it because I'm using a string instead of a number?
The error message I get is Run Time error 3075:
Syntax error (missing operator) in query expression '[Postcode]=TN8 6HR'

my code is:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Postcode_BeforeUpdate(Cancel As Integer)
  2. Dim rslt As Integer
  3.  
  4.    If Nz(DLookup("[Postcode]", "Addresses", "[Postcode]=" & Me.Postcode), 0) <> 0 Then
  5.       rslt = MsgBox("This number has already been entered. Do you wish to continue?", vbYesNo)
  6.       If rslt = vbNo Then
  7.          Me.Postcode = Null
  8.          Me.Postcode.SetFocus
  9.       End If
  10.    End If
  11. End Sub
can you help?
btw, I entered it in on the form design screen under Events >> Before Update as an Event Procedure



looks like I should change this to:


Expand|Select|Wrap|Line Numbers
  1. Private Sub Postcode_BeforeUpdate(Cancel As Integer)
  2. Dim rslt As Integer
  3.  
  4.    If Nz(DLookup("[Postcode]", "Addresses", "[Postcode]='" & Me.Postcode & "'"), 0) <> 0 Then
  5.       rslt = MsgBox("This number has already been entered. Do you wish to continue?", vbYesNo)
  6.       If rslt = vbNo Then
  7.          Cancel = True
  8.          Me.Postcode.SetFocus
  9.       End If
  10.    End If
  11. End Sub
I'll try it and see!
Reply