In MS Access using VBA: I want to transfer records with less than N^2 time complexity

I have one orders table with 5000 orders. And I have one customers table with 5000 customers.

Some orders have null at values “CustomerID” field which I want to fix with an algorithm.

Currently, for each null value at “CustomersID” in the Orders table the algorithm considers three fields from the orders table ( 1. name, 2. country, 3. cluster) and loop through the customers table to find a match with identical fields in the customers table (1. name, 2. country, 3. cluster). If there is a match I add the “CustomerID” in the orders table (replacing null value at “CustomersID”, otherwise I first create a new customer record in the Customers table and then add “CustomerID” in the Orders table.

The problem is slow algorithm. When looping though all fields that have null values in orders table (worst case 5000) and for each field try to find a match in the Customers table (worst case 5000 if there is no match) then the total time complexity becomes quadratic. N^2.

Thanks in advance!
Attached is my code. The first method opens record set of orders table (rss). It then loops through orders recordset to find null values. The second method opens record set for the Customers table (rst). It then loops through to find a match in fields shared from the orders table (1. name 2. country 3. cluster).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Public Sub TransferNullValues() 'hard coded parameters are "Orders", "Customers"
Set coll = New Collection
coll.Add "CustomerID"
coll.Add "End_customer"
coll.Add "End_customer_country"
coll.Add "Cluster"
Dim sqlStr As String
sqlStr = "SELECT " & buildSql(coll) & " FROM Orders where Orders.CustomerID is null;"
coll.Remove (1) 'remove customer id
Dim rss As DAO.Recordset
Set rss = Basic.getRs(sqlStr)
Do While Not rss.EOF
Dim locals As Collection
Set locals = New Collection
Dim v As Variant
For Each v In coll
locals.Add (rss(v).value)
Next
Dim id As Integer
id = getCustID(locals)
rss.Edit
rss("CustomerID").value = id
rss.Update
rss.MoveNext
Loop
rss.Close
Set rss = Nothing
End Sub
Private Function getCustID(locals As Collection) As Integer
Dim sqlStr As String
sqlStr = "SELECT * FROM CUSTOMERS;"
Dim rst As DAO.Recordset
Set rst = Basic.getRs(sqlStr)
Dim trgt As Collection
Do While Not rst.EOF
Set trgt = New Collection
Dim v As Variant
For Each v In coll
trgt.Add (rst(v).value)
Next
Dim allExists As Boolean
allExists = True
For Each v In locals
If Not Basic.Exists(trgt, v) Then
allExists = False
Exit For
End If
Next
If allExists Then
getCustID = rst("CustomerID").value
'Debug.Print "customer exists in customers table"
Exit Function
End If
rst.MoveNext
Loop
rst.AddNew
Dim count As Integer
count = 1
For Each v In locals
rst.Fields(count).value = v
count = count + 1
Next
Dim id As Integer
id = rst("CustomerID")
rst.Update
rst.Close
Set rst = Nothing
getCustID = id
End Function
</code>
<code>Public Sub TransferNullValues() 'hard coded parameters are "Orders", "Customers" Set coll = New Collection coll.Add "CustomerID" coll.Add "End_customer" coll.Add "End_customer_country" coll.Add "Cluster" Dim sqlStr As String sqlStr = "SELECT " & buildSql(coll) & " FROM Orders where Orders.CustomerID is null;" coll.Remove (1) 'remove customer id Dim rss As DAO.Recordset Set rss = Basic.getRs(sqlStr) Do While Not rss.EOF Dim locals As Collection Set locals = New Collection Dim v As Variant For Each v In coll locals.Add (rss(v).value) Next Dim id As Integer id = getCustID(locals) rss.Edit rss("CustomerID").value = id rss.Update rss.MoveNext Loop rss.Close Set rss = Nothing End Sub Private Function getCustID(locals As Collection) As Integer Dim sqlStr As String sqlStr = "SELECT * FROM CUSTOMERS;" Dim rst As DAO.Recordset Set rst = Basic.getRs(sqlStr) Dim trgt As Collection Do While Not rst.EOF Set trgt = New Collection Dim v As Variant For Each v In coll trgt.Add (rst(v).value) Next Dim allExists As Boolean allExists = True For Each v In locals If Not Basic.Exists(trgt, v) Then allExists = False Exit For End If Next If allExists Then getCustID = rst("CustomerID").value 'Debug.Print "customer exists in customers table" Exit Function End If rst.MoveNext Loop rst.AddNew Dim count As Integer count = 1 For Each v In locals rst.Fields(count).value = v count = count + 1 Next Dim id As Integer id = rst("CustomerID") rst.Update rst.Close Set rst = Nothing getCustID = id End Function </code>
Public Sub TransferNullValues() 'hard coded parameters are "Orders", "Customers"
    Set coll = New Collection
    coll.Add "CustomerID"
    coll.Add "End_customer"
    coll.Add "End_customer_country"
    coll.Add "Cluster"
    
    Dim sqlStr As String
    sqlStr = "SELECT " & buildSql(coll) & " FROM Orders where Orders.CustomerID is null;"
    coll.Remove (1) 'remove customer id
    
    Dim rss As DAO.Recordset
    Set rss = Basic.getRs(sqlStr)
    
    Do While Not rss.EOF
        Dim locals As Collection
        Set locals = New Collection
        
        Dim v As Variant
        For Each v In coll
            locals.Add (rss(v).value)
        Next
        
        Dim id As Integer
        id = getCustID(locals)
        
        rss.Edit
        rss("CustomerID").value = id
        rss.Update
            
        rss.MoveNext
    Loop
    
    rss.Close
    Set rss = Nothing
    
End Sub

Private Function getCustID(locals As Collection) As Integer
    Dim sqlStr As String
    sqlStr = "SELECT * FROM CUSTOMERS;"
    Dim rst As DAO.Recordset
    Set rst = Basic.getRs(sqlStr)
    Dim trgt As Collection
    
    
    Do While Not rst.EOF
        Set trgt = New Collection
        
        Dim v As Variant
        For Each v In coll
            trgt.Add (rst(v).value)
        Next
        
        Dim allExists As Boolean
        allExists = True
        
        For Each v In locals
            If Not Basic.Exists(trgt, v) Then
                allExists = False
                Exit For
            End If
        Next
        
        If allExists Then
            getCustID = rst("CustomerID").value
            'Debug.Print "customer exists in customers table"
            Exit Function
        End If
    
        rst.MoveNext
        
    Loop
    
    rst.AddNew
    Dim count As Integer
    count = 1
    For Each v In locals
        rst.Fields(count).value = v
        count = count + 1
    Next
    
    Dim id As Integer
    id = rst("CustomerID")
    
    rst.Update
        rst.Close
    Set rst = Nothing
    
    getCustID = id
End Function

I thought about how to decrease the time complexity but could not find a solution.

New contributor

user25413699 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật