Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / July 2007

Tip: Looking for answers? Try searching our database.

Assign new ID from duplicate records

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nils Pettersson - 02 Jul 2007 16:38 GMT
Hi,

My table contains records with duplicate IDs.

ID        Name
AAA     John Doe
AAA     Jane Doe
AAA     Baby Doe
FFF      Gorilla
FFF      Giraffe

I am trying to find a way to make each ID unique for each name by appending
a number it.

ID        Name
AAA01 John Doe
AAA02 Jane Doe
AAA03 Baby Doe
FFF01  Gorilla
FFF02  Giraffe

I hope this is clear. I simply want to add numbers to the ID starting with 1
or 01 and start with 1 again when it encounters a different ID.

Many thanks.
Nils
Ofer Cohen - 02 Jul 2007 17:28 GMT
First Back Up your data, then try this code

Function UpdateTable()
Dim MyDB As DAO.Database, MyRec As DAO.Recordset
Dim MyCount As Integer, OldValue As String
OldValue = ""
Set MyDB = CurrentDb
Set MyRec = MyDB.OpenRecordset("Select ID from TableName Order By ID")
While Not MyRec.EOF
   If OldValue = MyRec!ID Then
       MyCount = MyCount + 1
   Else
       MyCount = 1
   End If
   OldValue = MyRec!ID
   MyRec.Edit
   MyRec!ID = MyRec!ID & Format(MyCount, "00")
   
   MyRec.Update
   MyRec.MoveNext
Wend
End Function

Signature

Good Luck
BS"D

> Hi,
>
[quoted text clipped - 22 lines]
> Many thanks.
> Nils
Nils Pettersson - 03 Jul 2007 10:24 GMT
Thanks Ofer. This worked just as well. This information will prove very useful.

> First Back Up your data, then try this code
>
[quoted text clipped - 45 lines]
> > Many thanks.
> > Nils
John Spencer - 02 Jul 2007 17:41 GMT
If you can guarantee that the Name field is unique then you could try

UPDATE YourTable
SET ID = ID & DCount("ID","YourTable","ID=""" & [ID] & """ AND [Name] <="""
& [Name] &"""")

Alternative with a limited number of  duplicated ids might be to repeat the
query below changing  the 1 to 2, 3, 4, etc.
UPDATE YourTable
SET ID = [Id] & "1"
WHERE
(SELECT Count([Name])
FROM YourTable as YT
WHERE YT.ID = YourTable.ID and YT.Name <=YourTable.Name) = 1
AND YourTable.ID Not Like "*#"

<<< add the last criteria to only update records that do not end in a
number>>> hopefully keeping you from updating records that have already been
updated.

AS always, back up your data FIRST.  There is no going back

Signature

John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> Hi,
>
[quoted text clipped - 24 lines]
> Many thanks.
> Nils
Nils Pettersson - 03 Jul 2007 10:22 GMT
Thank you so much John! I've been trying so hard to get the right logic for
this to work in a query. It's been a thorn on my side for so long!

> If you can guarantee that the Name field is unique then you could try
>
[quoted text clipped - 46 lines]
> > Many thanks.
> > Nils
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.