{"id":24,"date":"2008-07-11T15:06:59","date_gmt":"2008-07-11T15:06:59","guid":{"rendered":"http:\/\/muratyaman.co.uk\/wp\/?p=24"},"modified":"2020-04-01T12:58:36","modified_gmt":"2020-04-01T11:58:36","slug":"dlookup-using-vba-collections","status":"publish","type":"post","link":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/2008\/07\/dlookup-using-vba-collections\/","title":{"rendered":"Faster lookup using VBA Collections"},"content":{"rendered":"<p><strong>Problem:<\/strong><br \/>\nReading records from the database and instead of using DLookup or querying the database all the time, to improve performance we can read them into a VBA collection object and refer to it in memory. Especially, if the data in particular tables are not changing at all or changing very rarely, we can use this facility and hold the data in memory while the application is running. Beware of the number of records in your table and the amount of memory it may consume.<\/p>\n<p>First, a very simple table to hold our data, with an ID (primary key) field to store unique numbers and a simple character field to save alphanumeric data.<\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-table1.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-table1-300x250.jpg\" alt=\"\" title=\"Table1\" width=\"300\" height=\"250\" class=\"aligncenter size-medium wp-image-25\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-table1-300x250.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-table1.jpg 586w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Then, based on our table, a VBA class definition to store and manipulate data in memory.<br \/>\n<a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-clstable1.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-clstable1-300x205.jpg\" alt=\"\" title=\"clsTable1\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-26\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-clstable1-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-clstable1.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre lang=\"vb\">\r\n'clsTable1\r\nOption Compare Database\r\nOption Explicit\r\n\r\n'definition same as Table1\r\nPublic id As Long\r\nPublic data As String\r\n\r\n'Flag to be set TRUE when loaded fine\r\nPublic isOK As Boolean\r\n\r\nPrivate Sub Class_Initialize()\r\n    isOK = False\r\n    id = 0\r\n    data = \"\"\r\nEnd Sub\r\n<\/pre>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-declare.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-declare-300x205.jpg\" alt=\"\" title=\"modMain declarations\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-33\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-declare-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-declare.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init-simple.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init-simple-300x205.jpg\" alt=\"Initialization of collection with simple\/random record objects\" title=\"modMain init-simple\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-37\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init-simple-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init-simple.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get-simple.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get-simple-300x205.jpg\" alt=\"Getting records from simple collection\" title=\"modMain get-simple\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-35\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get-simple-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get-simple.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init-300x205.jpg\" alt=\"Initialization of collection with records objects from Table1\" title=\"modMain init\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-36\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-init.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get-300x205.jpg\" alt=\"Getting record objects from the collection\" title=\"modMain get\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-34\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-modmain-get.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Here is the code of module modMain:<\/p>\n<pre lang=\"vb\">\r\n'modMain\r\nOption Compare Database\r\nOption Explicit\r\n\r\n'simple collection\r\nPrivate myColl_simple As Collection\r\n\r\n'collection to hold records of Table1 by id's\r\nPrivate myColl As Collection\r\n\r\nFunction myColl_init() As Boolean\r\nOn Error GoTo myErr\r\n    Dim rs As DAO.Recordset, sKey As String, r As clsTable1\r\n    Set myColl = New Collection\r\n    Set rs = CurrentDb.OpenRecordset(\"table1\")\r\n    rs.MoveFirst\r\n    Do While Not rs.BOF And Not rs.EOF\r\n        Set r = New clsTable1\r\n        r.id = rs.Fields(\"id\").Value\r\n        r.data = rs.Fields(\"data\").Value\r\n        r.isOK = True\r\n        sKey = \"A\" & r.id\r\n        myColl.Add r, sKey\r\n        rs.MoveNext\r\n    Loop\r\n    rs.Close: Set rs = Nothing\r\nmyExit:\r\n    myColl_init = (myColl.Count > 0)\r\n    Exit Function\r\nmyErr:\r\n    Resume myExit\r\nEnd Function\r\n\r\nFunction myColl_get_simple(i As Long) As String\r\nOn Error GoTo myErr\r\n    Dim k As String, v As String, r As clsTable1\r\n    k = \"A\" & i 'key\r\n    Set r = myColl_simple.Item(k)\r\n    v = r.data\r\nmyExit:\r\n    myColl_get_simple = v\r\n    Exit Function\r\nmyErr:\r\n    v = \"\" 'return an unexpected value\r\n    Resume myExit\r\nEnd Function\r\n\r\nFunction myColl_get(i As Long) As clsTable1\r\nOn Error GoTo myErr\r\n    Dim k As String, r As clsTable1\r\n    k = \"A\" & i 'key\r\n    Set r = myColl.Item(k) 'Flag is fine already\r\nmyExit:\r\n    Set myColl_get = r\r\n    Exit Function\r\nmyErr:\r\n    Set r = New clsTable1 'return an object, flag False\r\n    Resume myExit\r\nEnd Function\r\n\r\nFunction myColl_init_simple() As Boolean\r\n    Dim i As Long, k As String, v As String, t As clsTable1\r\n    Set myColl_simple = New Collection\r\n    Randomize\r\n    For i = 1 To 26\r\n        v = Chr(64 + i) 'A .. Z\r\n        v = v & v & v  ' AAA .. ZZZ\r\n        Set t = New clsTable1\r\n        t.id = i\r\n        t.data = v\r\n        t.isOK = True\r\n        k = \"A\" & i ' key\r\n        myColl_simple.Add t, k\r\n    Next i\r\n    myColl_init_simple = (myColl_simple.Count > 0)\r\nEnd Function\r\n<\/pre>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-load.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-load-300x205.jpg\" alt=\"Code run when loading the form\" title=\"frmTest code load\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-31\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-load-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-load.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get-simple.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get-simple-300x205.jpg\" alt=\"Putting all the code in use\" title=\"frmTest code get-simple\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-30\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get-simple-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get-simple.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-simple.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-simple-300x250.jpg\" alt=\"Putting all the code in use (runtime)\" title=\"frmTest simple\" width=\"300\" height=\"250\" class=\"aligncenter size-medium wp-image-32\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-simple-300x250.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-simple.jpg 586w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get-300x205.jpg\" alt=\"Putting all the code in use\" title=\"frmTest code get\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-29\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get-300x205.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-code-get.jpg 711w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest.jpg'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-300x250.jpg\" alt=\"Putting all the code in use (runtime)\" title=\"frmTest running\" width=\"300\" height=\"250\" class=\"aligncenter size-medium wp-image-27\" srcset=\"https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest-300x250.jpg 300w, https:\/\/www.muratyaman.co.uk\/blog\/wp-content\/uploads\/2008\/07\/db_vba_collections-frmtest.jpg 586w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Code of the form frmTest:<\/p>\n<pre lang=\"vb\">\r\n'frmTest\r\nOption Compare Database\r\nOption Explicit\r\n\r\nPrivate Sub cmdGet_Click()\r\nDim id As Long, r As clsTable1\r\n    id = CLng(Nz(Me.txtID, 0))\r\n    If id > 0 Then\r\n        Set r = myColl_get(id)\r\n        If r.isOK Then\r\n            Me.txtData.Value = r.data\r\n        End If\r\n    End If\r\nEnd Sub\r\n\r\nPrivate Sub cmdGetSimple_Click()\r\nDim id As Long, v As String\r\n    id = CLng(Nz(Me.txtID, 0))\r\n    If id > 0 Then\r\n        v = myColl_get_simple(id)\r\n        If v <> \"\" Then\r\n            Me.txtData.Value = v\r\n        End If\r\n    End If\r\nEnd Sub\r\n\r\n\r\nPrivate Sub Form_Load()\r\n    If Not myColl_init_simple() Then\r\n        MsgBox \"Error loading records!\"\r\n    End If\r\n    If Not myColl_init() Then\r\n        MsgBox \"Error loading records!\"\r\n    End If\r\nEnd Sub\r\n<\/pre>\n<p>Feel free to <a title=\"Access 2003 DB VBA Collections (zipped)\" href='http:\/\/muratyaman.co.uk\/wp\/wp-content\/uploads\/2008\/07\/db_vba_collections.zip'>download<\/a> the whole database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Reading records from the database and instead of using DLookup or querying the database all the time, to improve performance we can read them into a VBA collection object and refer to it in memory.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[15,17],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-technology","tag-access","tag-vba"],"_links":{"self":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":3,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/24\/revisions\/986"}],"wp:attachment":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}