Dear PaulT
Thank you very much for your help so far! I have a problem though with
your queries, because what I am trying to do, is automate the whole
order 'cleanse' process. In other words, that would have to run at
night without any user intervention. The form where the code will
reside will be loaded automatically with a macro, that was set in
scheduled tasks to run at a time. When the form runs it will have to go
through that table and eliminate the duplicates. At the moment that
table (All Purchases) do not have any primary keys, because of all the
duplicates, but after the cleanse the unique records will be appended to
another table called: tbl_SupplierOrderLines, where the primary key is
the PurchaseOrderNumber and the ItemCode. Hope that this information
could help you a bit more! I am so depressed about this, I have spend
like 2 days and more on it doing only that, and not getting anywhere. I
shall add my 'developed code - so far' at the bottom of this message if
you would like to see what I have been doing so far. It is not working
correctly yet, because I am struggeling with the fact of when I have 4
or more lines of the same data...... then it just loops and loops...
Thanks again for your interest and help!
Kind Regards
Marlene
N.S. Here is the code (hope it makes sense at all!):
Option Compare Database
Option Explicit
Private Sub cmdEliminate_Click()
'Recordsets
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim dbsMax As DAO.Database
Dim rstMax As DAO.Recordset
Dim dbsMaxCount As DAO.Database
Dim rstMaxCount As DAO.Recordset
'Keeping track of the order lines as gets read
Dim ordlineone As String 'Order Numbers
Dim ordlinetwo As String
Dim intqtyone As Integer 'Quantities
Dim intqtytwo As Integer
Dim inttotal As Integer 'Total of Quantities
Dim dateone As Date 'Dates
Dim datetwo As Date
Dim invnumberone As String 'Invoice Numbers
Dim invnumbertwo As String
Dim statone As String 'Statusses
Dim stattwo As String
Dim partone As String 'Parts
Dim parttwo As String
Dim supone As String 'Suppliers
Dim suptwo As String
Dim rdateone As Date 'Received Dates
Dim rdatetwo As Date
Dim intMax As Integer 'Max count of an order in the file
Dim intMaxCount As Integer
Dim valid As Boolean 'Amount of times to loop
'counters
Dim intcountord As Integer 'Count appearance of a order number in
the file
Dim intcounter As Long 'Count amount of records in file
Dim fourlineqty As Integer
Dim i As Integer 'used in loop 2 maybe
Dim backord As String 'To keep track of order if more than 2
occurances maybe
intcountord = 0
intcounter = 0
intqtyone = 0
intqtytwo = 0
inttotal = 0
ordlineone = ""
ordlinetwo = ""
invnumberone = ""
invnumbertwo = ""
dateone = 0
datetwo = 0
rdateone = 0
rdatetwo = 0
statone = ""
stattwo = ""
partone = ""
parttwo = ""
supone = ""
suptwo = ""
valid = False
i = 0
backord = ""
'====================
'Database connections
'====================
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("SELECT [All
Purchases].PurchaseOrderNumber, [All Purchases].PurchaseOrderDate, [All
Purchases].ReceivedQuantity, [All Purchases].InvoiceNumber, [All
Purchases].ItemCode, [All Purchases].SupplierCode, [All
Purchases].Status , [All Purchases].[PurchaseOrderReceivedDate]" & _
"FROM [All Purchases] " & _
"WHERE ((([All
Purchases].PurchaseOrderNumber) In " & _
"(SELECT
[PurchaseOrderNumber] " & _
"FROM [All Purchases] As
Tmp " & _
"GROUP BY
[PurchaseOrderNumber],[ItemCode],[SupplierCode],[Status] " & _
"HAVING Count(*)>1 " & _
"And [ItemCode] = [All
Purchases].[ItemCode] " & _
"And [SupplierCode] =
[All Purchases].[SupplierCode] " & _
"And [Status] = [All
Purchases].[Status]))) " & _
"ORDER BY [All
Purchases].PurchaseOrderNumber" & _
", [All Purchases].ItemCode, [All
Purchases].Status")
Do While Not rst.EOF
If rst("PurchaseOrderNumber") = "570372" Then
MsgBox rst("PurchaseOrderNumber")
End If
Set dbsMaxCount = CurrentDb()
Set rstMaxCount = dbsMaxCount.OpenRecordset("SELECT [Find
duplicates on PO#,Item,Status,Supplier for All
Purchases].PurchaseOrderNumber, [Find duplicates on
PO#,Item,Status,Supplier for All Purchases].ItemCode, Count([Find
duplicates on PO#,Item,Status,Supplier for All
Purchases].PurchaseOrderNumber) AS [Counter]" & _
"FROM [Find
duplicates on PO#,Item,Status,Supplier for All Purchases] " & _
"where [Find
duplicates on PO#,Item,Status,Supplier for All
Purchases].PurchaseOrderNumber = '" & rst("PurchaseOrderNumber") & "'" &
_
" AND [Find
duplicates on PO#,Item,Status,Supplier for All Purchases].ItemCode = '"
& rst("ItemCode") & "'" & _
"GROUP BY [Find
duplicates on PO#,Item,Status,Supplier for All
Purchases].PurchaseOrderNumber, [Find duplicates on
PO#,Item,Status,Supplier for All Purchases].ItemCode " & _
"HAVING
(((Count([Find duplicates on PO#,Item,Status,Supplier for All
Purchases].PurchaseOrderNumber))>2))")
If Not rstMaxCount.EOF Then
intMaxCount = rstMaxCount("Counter")
End If
'MsgBox rst("PurchaseOrderNumber") & " " & rst("ItemCode")
If intMaxCount = 3 And intcountord = 0 Then 'will work for 3
If fourlineqty <> 0 Then
rst.Edit
rst("ReceivedQuantity") = fourlineqty +
rst("ReceivedQuantity")
rst.Update
End If
ordlinetwo = ""
ordlineone = ""
intqtytwo = 0
intqtyone = 0
datetwo = 0
dateone = 0
invnumbertwo = ""
invnumberone = ""
stattwo = ""
statone = ""
parttwo = ""
partone = ""
suptwo = ""
supone = ""
rdatetwo = 0
rdateone = 0
intcountord = 0
backord = ""
intMaxCount = 0
rst.MoveNext
End If
If intcountord = 0 Then
'backord = ordlineone
invnumberone = ""
invnumbertwo = ""
partone = ""
parttwo = ""
ordlineone = ""
ordlinetwo = ""
statone = ""
stattwo = ""
dateone = 0
datetwo = 0
rdateone = 0
rdatetwo = 0
supone = ""
suptwo = ""
intqtyone = 0
intqtytwo = 0
'backord = ""
End If
intcountord = intcountord + 1 'Count appearance of a order
number in the file
intcounter = intcounter + 1 'Count amount of records in
file
If intcounter = 1 Or intcountord = 0 Then 'the first read
of the file
ordlineone = rst("PurchaseOrderNumber")
intqtyone = rst("ReceivedQuantity")
dateone = rst("PurchaseOrderDate")
invnumberone = rst("InvoiceNumber")
statone = rst("Status")
partone = rst("ItemCode")
supone = rst("SupplierCode")
statone = rst("Status")
rdateone = rst("PurchaseOrderReceivedDate")
rst.Edit
rst.Delete
rst.MoveNext
Else 'intcounter > 1
'=========================
'Going to a new order now
'=========================
If (invnumberone = "" And partone = "" And ordlineone = ""
And statone = "" And dateone = 0 And supone = "" And intqtyone = 0) Then
ordlineone = rst("PurchaseOrderNumber")
intqtyone = rst("ReceivedQuantity")
dateone = rst("PurchaseOrderDate")
invnumberone = rst("InvoiceNumber")
statone = rst("Status")
partone = rst("ItemCode")
supone = rst("SupplierCode")
statone = rst("Status")
rdateone = rst("PurchaseOrderReceivedDate")
rst.Edit
rst.Delete
'MsgBox "ordline one " & ordlineone & " ordlinetwo " &
ordlinetwo & "invone " & invnumberone
If ordlineone <> ordlinetwo Then
ordlinetwo = ""
intqtytwo = 0
datetwo = 0
invnumbertwo = ""
stattwo = ""
parttwo = ""
suptwo = ""
rdatetwo = 0
intcountord = 1
rst.MoveNext
End If
Else
If (invnumberone = "" And partone = "" And ordlineone =
"" And statone = "" And dateone = 0 And supone = "" And intqtyone = 0)
Or (intcounter = 2) Or (intcountord = 2) Then
'or intcountord = 2 and all twos are blank!!
ordlinetwo = rst("PurchaseOrderNumber")
intqtytwo = rst("ReceivedQuantity")
datetwo = rst("PurchaseOrderDate")
invnumbertwo = rst("InvoiceNumber")
stattwo = rst("Status")
parttwo = rst("ItemCode")
suptwo = rst("SupplierCode")
rdatetwo = rst("PurchaseOrderReceivedDate")
If ordlinetwo = ordlineone Then
If partone = parttwo Then
If supone = suptwo Then
If statone = stattwo Then
'===============
'invoice numbers
'===============
If invnumberone = 0 And invnumbertwo
<> 0 Then
If dateone > datetwo Then
rst.Edit
rst("PurchaseOrderDate") =
dateone
rst.Update
End If
If rdateone > rdatetwo Then
rst.Edit
rst("PurchaseOrderReceivedDate") = rdateone
rst.Update
End If
'every field was checked now
update qty
'total of quantities
inttotal = intqtyone + intqtytwo
'MsgBox inttotal
rst.Edit
rst("ReceivedQuantity") = inttotal
rst.Update
invnumberone = ""
partone = ""
ordlineone = ""
statone = ""
dateone = 0
supone = ""
intqtyone = 0
rdateone = 0
intcountord = 0
backord = ordlinetwo
rst.MoveNext
Else
If invnumberone = 0 And
invnumbertwo = 0 Then
If dateone > datetwo Then
rst.Edit
rst("PurchaseOrderDate") =
dateone
rst.Update
End If
If rdateone > rdatetwo Then
rst.Edit
rst("PurchaseOrderReceivedDate") = rdateone
rst.Update
End If
Else
If invnumberone <> 0 And
invnumbertwo = 0 Then
rst.Edit
rst("InvoiceNumber") =
invnumberone
rst.Update
End If
End If
'every field was checked now
update qty
'total of quantities
inttotal = intqtyone + intqtytwo
rst.Edit
rst("ReceivedQuantity") = inttotal
rst.Update
backord = ordlinetwo
intcountord = 0
If intMaxCount = 3 Then
fourlineqty =
rst("ReceivedQuantity")
'fourlineqty = 6
rst.Edit
rst.Delete
'rst.Update
invnumberone = ""
invnumbertwo = ""
partone = ""
parttwo = ""
ordlineone = ""
ordlinetwo = ""
statone = ""
stattwo = ""
dateone = 0
datetwo = 0
rdateone = 0
rdatetwo = 0
supone = ""
suptwo = ""
intqtyone = 0
intqtytwo = 0
Else
fourlineqty = 0
End If
rst.MoveNext
End If
'END invoice numbers
'===================
'Statusses NOT the Same
Else
If statone = "RETURN" And stattwo =
"pending" Then
backord = ordlinetwo
intcountord = 0
rst.MoveNext 'line one should have
been deleted already so ignore this second line
Else
If stattwo = "RETURN" And statone
= "PENDING" Then
'update the whole line two
with line one's details
rst.Edit
rst("ReceivedQuantity") =
intqtyone
rst("PurchaseOrderDate") =
dateone
rst("InvoiceNumber") =
invnumberone
rst("Status") = statone
'should be pending
rst("PurchaseOrderReceivedDate") = rdateone
rst.Update
invnumberone = ""
partone = ""
ordlineone = ""
statone = ""
dateone = 0
supone = ""
intqtyone = 0
rdateone = 0
intcountord = 0
backord = invnumbertwo
rst.MoveNext
End If 'should only have pending
and return statusses
End If
End If
'END Statusses
'=============
'Suppliers NOT the same
'NO CASE LIKE THIS for this query
End If
'=======================================
Else 'might be same number but diff part
ordlineone = rst("PurchaseOrderNumber")
intqtyone = rst("ReceivedQuantity")
dateone = rst("PurchaseOrderDate")
invnumberone = rst("InvoiceNumber")
statone = rst("Status")
partone = rst("ItemCode")
supone = rst("SupplierCode")
statone = rst("Status")
rdateone = rst("PurchaseOrderReceivedDate")
rst.Edit
rst.Delete
End If 'parts
End If 'orderlines the same
End If
End If
End If
'rst.MoveNext
Loop
MsgBox "Records Updated: " & Str(intcounter), vbInformation,
"Updated PurchaseOrderNumbers"
rst.Requery
MsgBox rst.RecordCount
If rst.RecordCount = 0 Then
rstMaxCount.Close
dbsMaxCount.Close
rst.Close
dbs.Close
End If
'====================
'Close DB Connections
'====================
End Sub
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!