[{"data":1,"prerenderedAt":1217},["ShallowReactive",2],{"blog:2008:dlookup-for-excel":3,"blogMore-Development":1050,"comments-dlookup-for-excel":1063},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":16,"body":41,"_type":1042,"_id":1043,"_source":1044,"_file":1045,"_stem":1046,"_extension":1047,"url":1048,"wordCount":1049,"minutes":155,"commentCount":165},"/blog/2008/dlookup-for-excel","2008",false,"en","DLookup for Excel","I had to do a couple of ad-hoc Excel jobs today and found that while Excel has a VLookup function for spreadsheet/ranges it doesn’t have one for databases.","2008-02-28T13:33:02+00:00","Development",[13,14,15],"Microsoft","Excel","SQL Server",{"type":17,"children":18},"root",[19,36],{"type":20,"tag":21,"props":22,"children":23},"element","p",{},[24,27,34],{"type":25,"value":26},"text","I had to do a couple of ad-hoc Excel jobs today and found that while Excel has a ",{"type":20,"tag":28,"props":29,"children":31},"code",{"className":30},[],[32],{"type":25,"value":33},"VLookup",{"type":25,"value":35}," function for spreadsheet/ranges it doesn’t have one for databases.",{"type":20,"tag":21,"props":37,"children":38},{},[39],{"type":25,"value":40},"It’s been a while since I touched VBA and Access but the DLookup function was useful so here it is for Excel. Read the warnings below before you use it!",{"type":17,"children":42,"toc":1038},[43,53,57,612,625,632,637,717,738,838,858,973,978,984,989,1018,1023,1032],{"type":20,"tag":21,"props":44,"children":45},{},[46,47,52],{"type":25,"value":26},{"type":20,"tag":28,"props":48,"children":50},{"className":49},[],[51],{"type":25,"value":33},{"type":25,"value":35},{"type":20,"tag":21,"props":54,"children":55},{},[56],{"type":25,"value":40},{"type":20,"tag":58,"props":59,"children":64},"pre",{"className":60,"code":61,"language":62,"meta":63,"style":63},"language-vb shiki shiki-themes everforest-light dracula","Public Function DLookup(expression As String, domain As String, criteria As String, Optional connectionString As String)\n\n    Dim con As ADODB.connection\n    Set con = New ADODB.connection\n    If IsMissing(connectionString) Then\n        con.Open \"Driver={SQL Server};Server=abc;Database=def;Trusted_Connection=Yes;\"\n    Else\n        con.Open connectionString\n    End If\n\n    Dim rs As ADODB.Recordset\n    Dim sql As String\n    sql = \"SELECT TOP 1 \" + expression + \" FROM \" + domain + \" WHERE \" + criteria\n    Set rs = con.Execute(sql)\n    If Not rs.EOF Then DLookup = rs(0)\n    rs.Close\n    Set rs = Nothing\n\n    con.Close\n    Set con = Nothing\n\nEnd Function\n","vb","",[65],{"type":20,"tag":28,"props":66,"children":67},{"__ignoreMap":63},[68,153,163,191,220,245,271,280,289,298,306,332,354,444,476,528,537,558,566,575,595,603],{"type":20,"tag":69,"props":70,"children":73},"span",{"class":71,"line":72},"line",1,[74,80,86,92,98,103,109,114,118,122,127,131,135,140,144,148],{"type":20,"tag":69,"props":75,"children":77},{"style":76},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[78],{"type":25,"value":79},"Public",{"type":20,"tag":69,"props":81,"children":83},{"style":82},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[84],{"type":25,"value":85}," Function ",{"type":20,"tag":69,"props":87,"children":89},{"style":88},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[90],{"type":25,"value":91},"DLookup",{"type":20,"tag":69,"props":93,"children":95},{"style":94},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[96],{"type":25,"value":97},"(expression ",{"type":20,"tag":69,"props":99,"children":100},{"style":76},[101],{"type":25,"value":102},"As",{"type":20,"tag":69,"props":104,"children":106},{"style":105},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[107],{"type":25,"value":108}," String",{"type":20,"tag":69,"props":110,"children":111},{"style":94},[112],{"type":25,"value":113},", domain ",{"type":20,"tag":69,"props":115,"children":116},{"style":76},[117],{"type":25,"value":102},{"type":20,"tag":69,"props":119,"children":120},{"style":105},[121],{"type":25,"value":108},{"type":20,"tag":69,"props":123,"children":124},{"style":94},[125],{"type":25,"value":126},", criteria ",{"type":20,"tag":69,"props":128,"children":129},{"style":76},[130],{"type":25,"value":102},{"type":20,"tag":69,"props":132,"children":133},{"style":105},[134],{"type":25,"value":108},{"type":20,"tag":69,"props":136,"children":137},{"style":94},[138],{"type":25,"value":139},", Optional connectionString ",{"type":20,"tag":69,"props":141,"children":142},{"style":76},[143],{"type":25,"value":102},{"type":20,"tag":69,"props":145,"children":146},{"style":105},[147],{"type":25,"value":108},{"type":20,"tag":69,"props":149,"children":150},{"style":94},[151],{"type":25,"value":152},")\n",{"type":20,"tag":69,"props":154,"children":156},{"class":71,"line":155},2,[157],{"type":20,"tag":69,"props":158,"children":160},{"emptyLinePlaceholder":159},true,[161],{"type":25,"value":162},"\n",{"type":20,"tag":69,"props":164,"children":166},{"class":71,"line":165},3,[167,172,177,181,186],{"type":20,"tag":69,"props":168,"children":169},{"style":82},[170],{"type":25,"value":171},"    Dim",{"type":20,"tag":69,"props":173,"children":174},{"style":94},[175],{"type":25,"value":176}," con ",{"type":20,"tag":69,"props":178,"children":179},{"style":76},[180],{"type":25,"value":102},{"type":20,"tag":69,"props":182,"children":183},{"style":105},[184],{"type":25,"value":185}," ADODB",{"type":20,"tag":69,"props":187,"children":188},{"style":94},[189],{"type":25,"value":190},".connection\n",{"type":20,"tag":69,"props":192,"children":194},{"class":71,"line":193},4,[195,200,205,210,215],{"type":20,"tag":69,"props":196,"children":197},{"style":82},[198],{"type":25,"value":199},"    Set ",{"type":20,"tag":69,"props":201,"children":202},{"style":94},[203],{"type":25,"value":204},"con ",{"type":20,"tag":69,"props":206,"children":207},{"style":76},[208],{"type":25,"value":209},"=",{"type":20,"tag":69,"props":211,"children":212},{"style":82},[213],{"type":25,"value":214}," New ",{"type":20,"tag":69,"props":216,"children":217},{"style":94},[218],{"type":25,"value":219},"ADODB.connection\n",{"type":20,"tag":69,"props":221,"children":223},{"class":71,"line":222},5,[224,230,235,240],{"type":20,"tag":69,"props":225,"children":227},{"style":226},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[228],{"type":25,"value":229},"    If",{"type":20,"tag":69,"props":231,"children":232},{"style":88},[233],{"type":25,"value":234}," IsMissing",{"type":20,"tag":69,"props":236,"children":237},{"style":94},[238],{"type":25,"value":239},"(connectionString) ",{"type":20,"tag":69,"props":241,"children":242},{"style":226},[243],{"type":25,"value":244},"Then\n",{"type":20,"tag":69,"props":246,"children":248},{"class":71,"line":247},6,[249,254,260,266],{"type":20,"tag":69,"props":250,"children":251},{"style":94},[252],{"type":25,"value":253},"        con.Open ",{"type":20,"tag":69,"props":255,"children":257},{"style":256},"--shiki-default:#DFA000;--shiki-dark:#E9F284",[258],{"type":25,"value":259},"\"",{"type":20,"tag":69,"props":261,"children":263},{"style":262},"--shiki-default:#DFA000;--shiki-dark:#F1FA8C",[264],{"type":25,"value":265},"Driver={SQL Server};Server=abc;Database=def;Trusted_Connection=Yes;",{"type":20,"tag":69,"props":267,"children":268},{"style":256},[269],{"type":25,"value":270},"\"\n",{"type":20,"tag":69,"props":272,"children":274},{"class":71,"line":273},7,[275],{"type":20,"tag":69,"props":276,"children":277},{"style":226},[278],{"type":25,"value":279},"    Else\n",{"type":20,"tag":69,"props":281,"children":283},{"class":71,"line":282},8,[284],{"type":20,"tag":69,"props":285,"children":286},{"style":94},[287],{"type":25,"value":288},"        con.Open connectionString\n",{"type":20,"tag":69,"props":290,"children":292},{"class":71,"line":291},9,[293],{"type":20,"tag":69,"props":294,"children":295},{"style":226},[296],{"type":25,"value":297},"    End If\n",{"type":20,"tag":69,"props":299,"children":301},{"class":71,"line":300},10,[302],{"type":20,"tag":69,"props":303,"children":304},{"emptyLinePlaceholder":159},[305],{"type":25,"value":162},{"type":20,"tag":69,"props":307,"children":309},{"class":71,"line":308},11,[310,314,319,323,327],{"type":20,"tag":69,"props":311,"children":312},{"style":82},[313],{"type":25,"value":171},{"type":20,"tag":69,"props":315,"children":316},{"style":94},[317],{"type":25,"value":318}," rs ",{"type":20,"tag":69,"props":320,"children":321},{"style":76},[322],{"type":25,"value":102},{"type":20,"tag":69,"props":324,"children":325},{"style":105},[326],{"type":25,"value":185},{"type":20,"tag":69,"props":328,"children":329},{"style":94},[330],{"type":25,"value":331},".Recordset\n",{"type":20,"tag":69,"props":333,"children":335},{"class":71,"line":334},12,[336,340,345,349],{"type":20,"tag":69,"props":337,"children":338},{"style":82},[339],{"type":25,"value":171},{"type":20,"tag":69,"props":341,"children":342},{"style":94},[343],{"type":25,"value":344}," sql ",{"type":20,"tag":69,"props":346,"children":347},{"style":76},[348],{"type":25,"value":102},{"type":20,"tag":69,"props":350,"children":351},{"style":105},[352],{"type":25,"value":353}," String\n",{"type":20,"tag":69,"props":355,"children":357},{"class":71,"line":356},13,[358,363,367,372,377,381,386,391,396,400,405,409,413,418,422,426,431,435,439],{"type":20,"tag":69,"props":359,"children":360},{"style":94},[361],{"type":25,"value":362},"    sql ",{"type":20,"tag":69,"props":364,"children":365},{"style":76},[366],{"type":25,"value":209},{"type":20,"tag":69,"props":368,"children":369},{"style":256},[370],{"type":25,"value":371}," \"",{"type":20,"tag":69,"props":373,"children":374},{"style":262},[375],{"type":25,"value":376},"SELECT TOP 1 ",{"type":20,"tag":69,"props":378,"children":379},{"style":256},[380],{"type":25,"value":259},{"type":20,"tag":69,"props":382,"children":383},{"style":76},[384],{"type":25,"value":385}," +",{"type":20,"tag":69,"props":387,"children":388},{"style":94},[389],{"type":25,"value":390}," expression ",{"type":20,"tag":69,"props":392,"children":393},{"style":76},[394],{"type":25,"value":395},"+",{"type":20,"tag":69,"props":397,"children":398},{"style":256},[399],{"type":25,"value":371},{"type":20,"tag":69,"props":401,"children":402},{"style":262},[403],{"type":25,"value":404}," FROM ",{"type":20,"tag":69,"props":406,"children":407},{"style":256},[408],{"type":25,"value":259},{"type":20,"tag":69,"props":410,"children":411},{"style":76},[412],{"type":25,"value":385},{"type":20,"tag":69,"props":414,"children":415},{"style":94},[416],{"type":25,"value":417}," domain ",{"type":20,"tag":69,"props":419,"children":420},{"style":76},[421],{"type":25,"value":395},{"type":20,"tag":69,"props":423,"children":424},{"style":256},[425],{"type":25,"value":371},{"type":20,"tag":69,"props":427,"children":428},{"style":262},[429],{"type":25,"value":430}," WHERE ",{"type":20,"tag":69,"props":432,"children":433},{"style":256},[434],{"type":25,"value":259},{"type":20,"tag":69,"props":436,"children":437},{"style":76},[438],{"type":25,"value":385},{"type":20,"tag":69,"props":440,"children":441},{"style":94},[442],{"type":25,"value":443}," criteria\n",{"type":20,"tag":69,"props":445,"children":447},{"class":71,"line":446},14,[448,452,457,461,466,471],{"type":20,"tag":69,"props":449,"children":450},{"style":82},[451],{"type":25,"value":199},{"type":20,"tag":69,"props":453,"children":454},{"style":94},[455],{"type":25,"value":456},"rs ",{"type":20,"tag":69,"props":458,"children":459},{"style":76},[460],{"type":25,"value":209},{"type":20,"tag":69,"props":462,"children":463},{"style":94},[464],{"type":25,"value":465}," con.",{"type":20,"tag":69,"props":467,"children":468},{"style":88},[469],{"type":25,"value":470},"Execute",{"type":20,"tag":69,"props":472,"children":473},{"style":94},[474],{"type":25,"value":475},"(sql)\n",{"type":20,"tag":69,"props":477,"children":479},{"class":71,"line":478},15,[480,484,489,494,499,504,508,513,518,524],{"type":20,"tag":69,"props":481,"children":482},{"style":226},[483],{"type":25,"value":229},{"type":20,"tag":69,"props":485,"children":486},{"style":76},[487],{"type":25,"value":488}," Not",{"type":20,"tag":69,"props":490,"children":491},{"style":94},[492],{"type":25,"value":493}," rs.EOF ",{"type":20,"tag":69,"props":495,"children":496},{"style":226},[497],{"type":25,"value":498},"Then",{"type":20,"tag":69,"props":500,"children":501},{"style":94},[502],{"type":25,"value":503}," DLookup ",{"type":20,"tag":69,"props":505,"children":506},{"style":76},[507],{"type":25,"value":209},{"type":20,"tag":69,"props":509,"children":510},{"style":88},[511],{"type":25,"value":512}," rs",{"type":20,"tag":69,"props":514,"children":515},{"style":94},[516],{"type":25,"value":517},"(",{"type":20,"tag":69,"props":519,"children":521},{"style":520},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[522],{"type":25,"value":523},"0",{"type":20,"tag":69,"props":525,"children":526},{"style":94},[527],{"type":25,"value":152},{"type":20,"tag":69,"props":529,"children":531},{"class":71,"line":530},16,[532],{"type":20,"tag":69,"props":533,"children":534},{"style":94},[535],{"type":25,"value":536},"    rs.Close\n",{"type":20,"tag":69,"props":538,"children":540},{"class":71,"line":539},17,[541,545,549,553],{"type":20,"tag":69,"props":542,"children":543},{"style":82},[544],{"type":25,"value":199},{"type":20,"tag":69,"props":546,"children":547},{"style":94},[548],{"type":25,"value":456},{"type":20,"tag":69,"props":550,"children":551},{"style":76},[552],{"type":25,"value":209},{"type":20,"tag":69,"props":554,"children":555},{"style":520},[556],{"type":25,"value":557}," Nothing\n",{"type":20,"tag":69,"props":559,"children":561},{"class":71,"line":560},18,[562],{"type":20,"tag":69,"props":563,"children":564},{"emptyLinePlaceholder":159},[565],{"type":25,"value":162},{"type":20,"tag":69,"props":567,"children":569},{"class":71,"line":568},19,[570],{"type":20,"tag":69,"props":571,"children":572},{"style":94},[573],{"type":25,"value":574},"    con.Close\n",{"type":20,"tag":69,"props":576,"children":578},{"class":71,"line":577},20,[579,583,587,591],{"type":20,"tag":69,"props":580,"children":581},{"style":82},[582],{"type":25,"value":199},{"type":20,"tag":69,"props":584,"children":585},{"style":94},[586],{"type":25,"value":204},{"type":20,"tag":69,"props":588,"children":589},{"style":76},[590],{"type":25,"value":209},{"type":20,"tag":69,"props":592,"children":593},{"style":520},[594],{"type":25,"value":557},{"type":20,"tag":69,"props":596,"children":598},{"class":71,"line":597},21,[599],{"type":20,"tag":69,"props":600,"children":601},{"emptyLinePlaceholder":159},[602],{"type":25,"value":162},{"type":20,"tag":69,"props":604,"children":606},{"class":71,"line":605},22,[607],{"type":20,"tag":69,"props":608,"children":609},{"style":82},[610],{"type":25,"value":611},"End Function\n",{"type":20,"tag":21,"props":613,"children":614},{},[615,617,623],{"type":25,"value":616},"Copy and paste the code above into an Excel module and replace the ",{"type":20,"tag":28,"props":618,"children":620},{"className":619},[],[621],{"type":25,"value":622},"Driver=...",{"type":25,"value":624}," with what you want to be your default connection.",{"type":20,"tag":626,"props":627,"children":629},"h3",{"id":628},"some-examples-of-use",[630],{"type":25,"value":631},"Some examples of use:",{"type":20,"tag":21,"props":633,"children":634},{},[635],{"type":25,"value":636},"To select the Name column from the Customers table where the numeric ID is held in column 2.",{"type":20,"tag":58,"props":638,"children":640},{"className":60,"code":639,"language":62,"meta":63,"style":63},"=DLookup(\"Name\",\"Customers\",\"ID=\" & A2)\n",[641],{"type":20,"tag":28,"props":642,"children":643},{"__ignoreMap":63},[644],{"type":20,"tag":69,"props":645,"children":646},{"class":71,"line":72},[647,651,655,659,663,668,672,677,681,686,690,694,698,703,707,712],{"type":20,"tag":69,"props":648,"children":649},{"style":76},[650],{"type":25,"value":209},{"type":20,"tag":69,"props":652,"children":653},{"style":88},[654],{"type":25,"value":91},{"type":20,"tag":69,"props":656,"children":657},{"style":94},[658],{"type":25,"value":517},{"type":20,"tag":69,"props":660,"children":661},{"style":256},[662],{"type":25,"value":259},{"type":20,"tag":69,"props":664,"children":665},{"style":262},[666],{"type":25,"value":667},"Name",{"type":20,"tag":69,"props":669,"children":670},{"style":256},[671],{"type":25,"value":259},{"type":20,"tag":69,"props":673,"children":674},{"style":94},[675],{"type":25,"value":676},",",{"type":20,"tag":69,"props":678,"children":679},{"style":256},[680],{"type":25,"value":259},{"type":20,"tag":69,"props":682,"children":683},{"style":262},[684],{"type":25,"value":685},"Customers",{"type":20,"tag":69,"props":687,"children":688},{"style":256},[689],{"type":25,"value":259},{"type":20,"tag":69,"props":691,"children":692},{"style":94},[693],{"type":25,"value":676},{"type":20,"tag":69,"props":695,"children":696},{"style":256},[697],{"type":25,"value":259},{"type":20,"tag":69,"props":699,"children":700},{"style":262},[701],{"type":25,"value":702},"ID=",{"type":20,"tag":69,"props":704,"children":705},{"style":256},[706],{"type":25,"value":259},{"type":20,"tag":69,"props":708,"children":709},{"style":76},[710],{"type":25,"value":711}," &",{"type":20,"tag":69,"props":713,"children":714},{"style":94},[715],{"type":25,"value":716}," A2)\n",{"type":20,"tag":21,"props":718,"children":719},{},[720,722,728,730,736],{"type":25,"value":721},"To return ",{"type":20,"tag":28,"props":723,"children":725},{"className":724},[],[726],{"type":25,"value":727},"Forename",{"type":25,"value":729}," and ",{"type":20,"tag":28,"props":731,"children":733},{"className":732},[],[734],{"type":25,"value":735},"Surname",{"type":25,"value":737}," columns joined together with a space from the Contacts table in the crm database using the postcode from column 5:",{"type":20,"tag":58,"props":739,"children":741},{"className":60,"code":740,"language":62,"meta":63,"style":63},"=DLookup(\"Forname + ' ' + Surname\",\"crm..Contact\",\"PostCode='\" & A5 & \"'\")\n",[742],{"type":20,"tag":28,"props":743,"children":744},{"__ignoreMap":63},[745],{"type":20,"tag":69,"props":746,"children":747},{"class":71,"line":72},[748,752,756,760,764,769,773,777,781,786,790,794,798,803,807,811,816,821,825,830,834],{"type":20,"tag":69,"props":749,"children":750},{"style":76},[751],{"type":25,"value":209},{"type":20,"tag":69,"props":753,"children":754},{"style":88},[755],{"type":25,"value":91},{"type":20,"tag":69,"props":757,"children":758},{"style":94},[759],{"type":25,"value":517},{"type":20,"tag":69,"props":761,"children":762},{"style":256},[763],{"type":25,"value":259},{"type":20,"tag":69,"props":765,"children":766},{"style":262},[767],{"type":25,"value":768},"Forname + ' ' + Surname",{"type":20,"tag":69,"props":770,"children":771},{"style":256},[772],{"type":25,"value":259},{"type":20,"tag":69,"props":774,"children":775},{"style":94},[776],{"type":25,"value":676},{"type":20,"tag":69,"props":778,"children":779},{"style":256},[780],{"type":25,"value":259},{"type":20,"tag":69,"props":782,"children":783},{"style":262},[784],{"type":25,"value":785},"crm..Contact",{"type":20,"tag":69,"props":787,"children":788},{"style":256},[789],{"type":25,"value":259},{"type":20,"tag":69,"props":791,"children":792},{"style":94},[793],{"type":25,"value":676},{"type":20,"tag":69,"props":795,"children":796},{"style":256},[797],{"type":25,"value":259},{"type":20,"tag":69,"props":799,"children":800},{"style":262},[801],{"type":25,"value":802},"PostCode='",{"type":20,"tag":69,"props":804,"children":805},{"style":256},[806],{"type":25,"value":259},{"type":20,"tag":69,"props":808,"children":809},{"style":76},[810],{"type":25,"value":711},{"type":20,"tag":69,"props":812,"children":813},{"style":94},[814],{"type":25,"value":815}," A5 ",{"type":20,"tag":69,"props":817,"children":818},{"style":76},[819],{"type":25,"value":820},"&",{"type":20,"tag":69,"props":822,"children":823},{"style":256},[824],{"type":25,"value":371},{"type":20,"tag":69,"props":826,"children":827},{"style":262},[828],{"type":25,"value":829},"'",{"type":20,"tag":69,"props":831,"children":832},{"style":256},[833],{"type":25,"value":259},{"type":20,"tag":69,"props":835,"children":836},{"style":94},[837],{"type":25,"value":152},{"type":20,"tag":21,"props":839,"children":840},{},[841,842,848,850,856],{"type":25,"value":721},{"type":20,"tag":28,"props":843,"children":845},{"className":844},[],[846],{"type":25,"value":847},"Description",{"type":25,"value":849}," from a ",{"type":20,"tag":28,"props":851,"children":853},{"className":852},[],[854],{"type":25,"value":855},"Products",{"type":25,"value":857}," table held in another connection using a partial like match from column 1:",{"type":20,"tag":58,"props":859,"children":861},{"className":60,"code":860,"language":62,"meta":63,"style":63},"=DLookup(\"Description\",\"Products\",\"OrderCode LIKE '\" & A1 & \"%'\", \"DSN=SecondaryDSN\")\n",[862],{"type":20,"tag":28,"props":863,"children":864},{"__ignoreMap":63},[865],{"type":20,"tag":69,"props":866,"children":867},{"class":71,"line":72},[868,872,876,880,884,888,892,896,900,904,908,912,916,921,925,929,934,938,942,947,951,956,960,965,969],{"type":20,"tag":69,"props":869,"children":870},{"style":76},[871],{"type":25,"value":209},{"type":20,"tag":69,"props":873,"children":874},{"style":88},[875],{"type":25,"value":91},{"type":20,"tag":69,"props":877,"children":878},{"style":94},[879],{"type":25,"value":517},{"type":20,"tag":69,"props":881,"children":882},{"style":256},[883],{"type":25,"value":259},{"type":20,"tag":69,"props":885,"children":886},{"style":262},[887],{"type":25,"value":847},{"type":20,"tag":69,"props":889,"children":890},{"style":256},[891],{"type":25,"value":259},{"type":20,"tag":69,"props":893,"children":894},{"style":94},[895],{"type":25,"value":676},{"type":20,"tag":69,"props":897,"children":898},{"style":256},[899],{"type":25,"value":259},{"type":20,"tag":69,"props":901,"children":902},{"style":262},[903],{"type":25,"value":855},{"type":20,"tag":69,"props":905,"children":906},{"style":256},[907],{"type":25,"value":259},{"type":20,"tag":69,"props":909,"children":910},{"style":94},[911],{"type":25,"value":676},{"type":20,"tag":69,"props":913,"children":914},{"style":256},[915],{"type":25,"value":259},{"type":20,"tag":69,"props":917,"children":918},{"style":262},[919],{"type":25,"value":920},"OrderCode LIKE '",{"type":20,"tag":69,"props":922,"children":923},{"style":256},[924],{"type":25,"value":259},{"type":20,"tag":69,"props":926,"children":927},{"style":76},[928],{"type":25,"value":711},{"type":20,"tag":69,"props":930,"children":931},{"style":94},[932],{"type":25,"value":933}," A1 ",{"type":20,"tag":69,"props":935,"children":936},{"style":76},[937],{"type":25,"value":820},{"type":20,"tag":69,"props":939,"children":940},{"style":256},[941],{"type":25,"value":371},{"type":20,"tag":69,"props":943,"children":944},{"style":262},[945],{"type":25,"value":946},"%'",{"type":20,"tag":69,"props":948,"children":949},{"style":256},[950],{"type":25,"value":259},{"type":20,"tag":69,"props":952,"children":953},{"style":94},[954],{"type":25,"value":955},", ",{"type":20,"tag":69,"props":957,"children":958},{"style":256},[959],{"type":25,"value":259},{"type":20,"tag":69,"props":961,"children":962},{"style":262},[963],{"type":25,"value":964},"DSN=SecondaryDSN",{"type":20,"tag":69,"props":966,"children":967},{"style":256},[968],{"type":25,"value":259},{"type":20,"tag":69,"props":970,"children":971},{"style":94},[972],{"type":25,"value":152},{"type":20,"tag":21,"props":974,"children":975},{},[976],{"type":25,"value":977},"I think you get the idea!",{"type":20,"tag":626,"props":979,"children":981},{"id":980},"caveats",[982],{"type":25,"value":983},"Caveats",{"type":20,"tag":21,"props":985,"children":986},{},[987],{"type":25,"value":988},"There are a few caveats you need to be aware of using this function:",{"type":20,"tag":990,"props":991,"children":992},"ul",{},[993,999,1009],{"type":20,"tag":994,"props":995,"children":996},"li",{},[997],{"type":25,"value":998},"It only returns the first value of the first row",{"type":20,"tag":994,"props":1000,"children":1001},{},[1002,1007],{"type":20,"tag":28,"props":1003,"children":1005},{"className":1004},[],[1006],{"type":25,"value":91},{"type":25,"value":1008}," is not efficient as it opens connection each time it is referenced. This could put a lot of strain on your server.",{"type":20,"tag":994,"props":1010,"children":1011},{},[1012],{"type":20,"tag":28,"props":1013,"children":1015},{"className":1014},[],[1016],{"type":25,"value":1017},"DLookup syntax is open-ended and has some attack vectors",{"type":20,"tag":21,"props":1019,"children":1020},{},[1021],{"type":25,"value":1022},"If that doesn’t scare you then give it a shot. As usual no warranty expressed or implied!",{"type":20,"tag":21,"props":1024,"children":1025},{},[1026],{"type":20,"tag":1027,"props":1028,"children":1029},"em",{},[1030],{"type":25,"value":1031},"[)amien",{"type":20,"tag":1033,"props":1034,"children":1035},"style",{},[1036],{"type":25,"value":1037},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":63,"searchDepth":155,"depth":155,"links":1039},[1040,1041],{"id":628,"depth":165,"text":631},{"id":980,"depth":165,"text":983},"markdown","content:blog:2008:dlookup-for-excel.md","content","blog/2008/dlookup-for-excel.md","blog/2008/dlookup-for-excel","md","/blog/2008/dlookup-for-excel/",438,[1051,1055,1059],{"title":1052,"date":1053,"url":1054},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":1056,"date":1057,"url":1058},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":1060,"date":1061,"url":1062},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[1064,1085,1106],{"_path":1065,"_dir":1066,"_draft":6,"_partial":6,"_locale":7,"title":1067,"description":1068,"id":1069,"name":1070,"email":1071,"avatar":1072,"date":1073,"body":1074,"_type":1042,"_id":1082,"_source":1044,"_file":1083,"_stem":1084,"_extension":1047},"/comments/dlookup-for-excel/170876","dlookup-for-excel","170876","Thank you so much for posting.  I am so used to writing sql statements in VB or Access and needed one for an Excel project this morning.  I couldn't get a multiple criteria VLOOKUP or an INDEX/MATCH to work with the data I had - but this was a gem.",170876,"TxGeekGirl","lenam@sierraairconditioning.com","https://www.gravatar.com/avatar/2269058873d90bed9e1edfd9adc263f6?r=pg&d=retro","2017-04-17T11:30:10",{"type":17,"children":1075,"toc":1080},[1076],{"type":20,"tag":21,"props":1077,"children":1078},{},[1079],{"type":25,"value":1068},{"title":63,"searchDepth":155,"depth":155,"links":1081},[],"content:comments:dlookup-for-excel:170876.md","comments/dlookup-for-excel/170876.md","comments/dlookup-for-excel/170876",{"_path":1086,"_dir":1066,"_draft":6,"_partial":6,"_locale":7,"title":1087,"description":1088,"id":1089,"name":1090,"email":1091,"avatar":1092,"url":1093,"date":1094,"body":1095,"_type":1042,"_id":1103,"_source":1044,"_file":1104,"_stem":1105,"_extension":1047},"/comments/dlookup-for-excel/6521","6521","Yes, that makes more sense.  I've updated the function, thanks!",6521,"Damien Guard","damien@envytech.co.uk","https://www.gravatar.com/avatar/dc72963e7279d34c85ed4c0b731ce5a9?r=pg&d=retro","https://damieng.com/","2008-03-05T09:08:08",{"type":17,"children":1096,"toc":1101},[1097],{"type":20,"tag":21,"props":1098,"children":1099},{},[1100],{"type":25,"value":1088},{"title":63,"searchDepth":155,"depth":155,"links":1102},[],"content:comments:dlookup-for-excel:6521.md","comments/dlookup-for-excel/6521.md","comments/dlookup-for-excel/6521",{"_path":1107,"_dir":1066,"_draft":6,"_partial":6,"_locale":7,"title":1108,"description":1109,"id":1110,"name":1111,"email":1112,"avatar":1113,"date":1114,"body":1115,"_type":1042,"_id":1214,"_source":1044,"_file":1215,"_stem":1216,"_extension":1047},"/comments/dlookup-for-excel/6520","6520","As the function only returns one row you should force the query to only return one row using the TOP select constraint to minimise load on the server. Achieve this by modifying your sql assignment to read:",6520,"Brad","bradleycarr@gmail.com","https://www.gravatar.com/avatar/0c22046a7375e407725f2a62cdb978f0?r=pg&d=retro","2008-03-05T09:02:09",{"type":17,"children":1116,"toc":1212},[1117,1121,1208],{"type":20,"tag":21,"props":1118,"children":1119},{},[1120],{"type":25,"value":1109},{"type":20,"tag":58,"props":1122,"children":1124},{"className":60,"code":1123,"language":62,"meta":63,"style":63},"sql = \"SELECT TOP 1 \" + expression + \" FROM \" + domain + \" WHERE \" + criteria\n",[1125],{"type":20,"tag":28,"props":1126,"children":1127},{"__ignoreMap":63},[1128],{"type":20,"tag":69,"props":1129,"children":1130},{"class":71,"line":72},[1131,1136,1140,1144,1148,1152,1156,1160,1164,1168,1172,1176,1180,1184,1188,1192,1196,1200,1204],{"type":20,"tag":69,"props":1132,"children":1133},{"style":94},[1134],{"type":25,"value":1135},"sql ",{"type":20,"tag":69,"props":1137,"children":1138},{"style":76},[1139],{"type":25,"value":209},{"type":20,"tag":69,"props":1141,"children":1142},{"style":256},[1143],{"type":25,"value":371},{"type":20,"tag":69,"props":1145,"children":1146},{"style":262},[1147],{"type":25,"value":376},{"type":20,"tag":69,"props":1149,"children":1150},{"style":256},[1151],{"type":25,"value":259},{"type":20,"tag":69,"props":1153,"children":1154},{"style":76},[1155],{"type":25,"value":385},{"type":20,"tag":69,"props":1157,"children":1158},{"style":94},[1159],{"type":25,"value":390},{"type":20,"tag":69,"props":1161,"children":1162},{"style":76},[1163],{"type":25,"value":395},{"type":20,"tag":69,"props":1165,"children":1166},{"style":256},[1167],{"type":25,"value":371},{"type":20,"tag":69,"props":1169,"children":1170},{"style":262},[1171],{"type":25,"value":404},{"type":20,"tag":69,"props":1173,"children":1174},{"style":256},[1175],{"type":25,"value":259},{"type":20,"tag":69,"props":1177,"children":1178},{"style":76},[1179],{"type":25,"value":385},{"type":20,"tag":69,"props":1181,"children":1182},{"style":94},[1183],{"type":25,"value":417},{"type":20,"tag":69,"props":1185,"children":1186},{"style":76},[1187],{"type":25,"value":395},{"type":20,"tag":69,"props":1189,"children":1190},{"style":256},[1191],{"type":25,"value":371},{"type":20,"tag":69,"props":1193,"children":1194},{"style":262},[1195],{"type":25,"value":430},{"type":20,"tag":69,"props":1197,"children":1198},{"style":256},[1199],{"type":25,"value":259},{"type":20,"tag":69,"props":1201,"children":1202},{"style":76},[1203],{"type":25,"value":385},{"type":20,"tag":69,"props":1205,"children":1206},{"style":94},[1207],{"type":25,"value":443},{"type":20,"tag":1033,"props":1209,"children":1210},{},[1211],{"type":25,"value":1037},{"title":63,"searchDepth":155,"depth":155,"links":1213},[],"content:comments:dlookup-for-excel:6520.md","comments/dlookup-for-excel/6520.md","comments/dlookup-for-excel/6520",1779264587807]