

What would be useful is a function that performs the lookup on any table or query, which is what I attempt to achieve with the dmwVLookUp function below.įor the function to work, these conditions must apply to the data source upon which the lookup is to be performed: If you needed to perform an alternative lookup, then you’d have to write another function. The fnDmwVLookUpByDate function, above, is restrictive in its application: it’s looking up a date in a particular table, the details of which are buried in the code. Then in the Editor’s Immediate Window input, for example, the following statement and press Enter (don’t omit the leading question mark): ?fnDmwVLookUpByDate(DateSerial(2019,08,26))
#HOW TO USE VLOOKUP IN EXCEL 2019 CODE#
To execute the function, copy and paste the code into a module in your database’s Visual Basic Editor. In this circumstance the function returns the latest recorded weight assigned as it is to the y! variable.

"FROM tblBodyWeight ORDER BY DateWeight DESC "Īs the function commences its loop through the records in the table, the following code deals with the condition that the date being looked up is later than any date recorded in the table.
#HOW TO USE VLOOKUP IN EXCEL 2019 HOW TO#
Later in this article I’ll explain how to adapt it for general lookup purposes.įunction fnDmwVLookUpByDate(d As Date) As Single The function described below is aimed at the data in tblBodyWeight, illustrated above. Hence the lookup function I’ve created returns the person’s weight for any date, even for one for which a weight is not recorded. Neither Access’s DLookup nor Excel’s VLOOKUP are fit for purpose when required to return a body weight for a date for which no weight was recorded. Because she was on a weight-loss programme, recorded weights varied a lot.


In the database used by another person, weighings were taken place every two to three weeks. The table above shows that weighing was made on reasonably frequent occasions and weight variations were not large. In using this database the person isn’t required to weigh himself every day. The database records the food the person has eaten and compares their daily intake of nutritional components - carbohydrates, sugars, fat, protein and so on - with recommendations for a healthy diet based on their body weight. The table of body weights, above, is take from a database I’ve created for monitoring a person’s diet. But there’s an additional requirement, explained in the next section. So what’s needed is a function that’s akin to VLOOKUP. In Microsoft Excel, it’s VLOOKUP function allows you to avoid a null result by instructing the function to return the nearest matching result. In this context Access requires you to supply a date in the mm/dd/yyyy form.)įor a date that does not appear in the table DLookup returns null. (The use of the # character signifies to Access that the is a date and not 03÷01÷2019. For example, to look up a person’s weight on a certain date recorded in this table:Īccess's Dlookup function will return a value from the table, as in this example for March 1st 2019 in which it would return 79.3:ĭlookup("Weight","tblBodyWeight","DateWeight=#") How to create a VBA procedure in Microsoft Access that behaves like Excel's VLOOKUP function but with additional functionality.Ĭonsider needing to look up a value in a list that matches a certain criterion. (e) A Look Up Function for Microsoft ® Access
