I've got a database with a lot of identical number but with different other information in the record.
As I have more than 3 times the same number I would like to take the 3 first and delete the others.
How can I do that?
Could you help me asap?
How to detremine... randomly! Doesn't matter wich 3 are remaining.
Table descrition:
PHone number|Company|Name
111|me inc.|John
111|me inc|Mark
111|me inc|Bob
111|me inc|Bill
112|you inc|fred
112|you corp|Ron
113|them|William
...
I would like to keep 113, all 112 and only 3 of the 111, doesn't matter wich one (the 3 first by example)
A bit nasty but should work. I tested it on a small database sample.
You'll need to change the following values:
Table1 to the name of your table
TableID to the name of the duplicate field
Public Function TidyMyData() As Boolean
Const MyTable As String = "Table1"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(MyTable)
Dim intID As Integer
Dim intCountID As Integer
Dim intCountChecks As Integer
With rs
Do While Not .EOF
If intID <> .Fields(.Fields(0).Name) Then
intID = .Fields(.Fields(0).Name)
End If
intCountID = DCount("TableID", MyTable, "TableID = " & intID)
intCountChecks = DCount("Test", MyTable, "Test = True And TableID = " & intID)
If intCountID > 3 Then
If intCountChecks < 3 Then
.Edit
.Fields("Test") = True
.Update
End If
Else
.Edit
.Fields("Test") = True
.Update
End If
.MoveNext
Loop
.Close
End With
db.Close
strSQL = "DELETE * FROM " & MyTable & " WHERE Test = False;"
Same function but a few tweaks to work with a text field.
Public Function TidyMyData() As Boolean
Const MyTable As String = "Table1"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(MyTable)
Dim strID As String
Dim intCountID As Integer
Dim intCountChecks As Integer
With rs
Do While Not .EOF
If intID <> .Fields(.Fields(0).Name) Then
intID = .Fields(.Fields(0).Name)
End If
intCountID = DCount("TableID", MyTable, "TableID = """ & strID & """")
intCountChecks = DCount("Test", MyTable, "Test = True And TableID = """ & strID & """")
If intCountID > 3 Then
If intCountChecks < 3 Then
.Edit
.Fields("Test") = True
.Update
End If
Else
.Edit
.Fields("Test") = True
.Update
End If
.MoveNext
Loop
.Close
End With
db.Close
strSQL = "DELETE * FROM " & MyTable & " WHERE Test = False;"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(MyTable)
Dim strID As String
Dim strSQl As String
Dim intCountID As Integer
Dim intCountChecks As Integer
With rs
Do While Not .EOF
If strID <> .Fields(.Fields(0).Name) Then
strID = .Fields(.Fields(0).Name)
End If
intCountID = DCount("TableID", MyTable, "TableID = """ & strID & """")
intCountChecks = DCount("Test", MyTable, "Test = True And TableID = """ & strID & """")
If intCountID > 3 Then
If intCountChecks < 3 Then
.Edit
.Fields("Test") = True
.Update
End If
Else
.Edit
.Fields("Test") = True
.Update
End If
.MoveNext
Loop
.Close
End With
db.Close
strSQl = "DELETE * FROM " & MyTable & " WHERE Test = False;"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.