I have a target table that has rows grouped by two columns, customer and order for the sake of argument. Customers can have many rows with the same order number. For each group of customers, I need to update the first row of a unique customer_number and order_number with a possible max and possible min value that is in another table, the source table where there is one row per customer - order. Here is the code I put in my module:
Function Update_Table()
Dim dbs As Database
Dim dssource As Recordset
Dim dstarget As Recordset
Dim mcriteria As String
Set dbs = CurrentDb
Set dssource = dbs.OpenRecordset("source_table", DB_OPEN_DYNASET)
Set dstarget = dbs.OpenRecordset("target_table", DB_OPEN_DYNASET)
dssource.MoveFirst
Do While Not dssource.EOF
mcriteria = "Customer_Number = " & """" & dssource("Customer_Number"
& """" & "and Order_Number = " & dssource("Order_Number"
& ""
dstarget.FindFirst mcriteria
If Not dstarget.NoMatch Then
dstarget.Edit
dstarget("PMAX"
= dssource("PMAX"
dstarget("PMIN"
= dssource("PMIN"
dstarget.Update
Else
MsgBox "There is something wrong"
End If
dssource.MoveNext
Loop
dssource.Close
dstarget.Close
Set dbs = Nothing
End Function
The target table has an index on customer_number (which is a text field) and has over 183,000 rows. It will be growing each month. When I called this code from a form using a macro which called the function, it took several days to run.
I'm hoping there's a better way.
Thanks for any help in advance.
Ken
Function Update_Table()
Dim dbs As Database
Dim dssource As Recordset
Dim dstarget As Recordset
Dim mcriteria As String
Set dbs = CurrentDb
Set dssource = dbs.OpenRecordset("source_table", DB_OPEN_DYNASET)
Set dstarget = dbs.OpenRecordset("target_table", DB_OPEN_DYNASET)
dssource.MoveFirst
Do While Not dssource.EOF
mcriteria = "Customer_Number = " & """" & dssource("Customer_Number"
dstarget.FindFirst mcriteria
If Not dstarget.NoMatch Then
dstarget.Edit
dstarget("PMAX"
dstarget("PMIN"
dstarget.Update
Else
MsgBox "There is something wrong"
End If
dssource.MoveNext
Loop
dssource.Close
dstarget.Close
Set dbs = Nothing
End Function
The target table has an index on customer_number (which is a text field) and has over 183,000 rows. It will be growing each month. When I called this code from a form using a macro which called the function, it took several days to run.
I'm hoping there's a better way.
Thanks for any help in advance.
Ken