I have a query in Microsoft Access which updates zipcodes/postcodes in a table (I_Postcodes) with the distance and time it takes to travel from a base origin zipcode (held in I_BasePostcode) to the zipcode concerned.
My Access query for determining distance only is:
UPDATE I_Postcodes, I_BasePostcode
SET I_Postcodes.DistanceFromBase = GetDistance(I_BasePostcode.Postcode,I_Postcodes.Postcode)
WHERE (I_Postcodes.DistanceFromBase Is Null);
The I_BasePostcode table looks like:
+----------+
| Postcode |
+----------+
| LS1 3EX |
+----------+
and an extract from the I_Postcodes table looks like:
+----------+------------------+--------------+
| Postcode | DistanceFromBase | TimeFromBase |
+----------+------------------+--------------+
| SW13 9EE | 50 | 200 |
| SW13 9EF | 50 | 201 |
| SW13 9EG | 52 | 210 |
+----------+------------------+--------------+
The GetDistance function is somewhat complicated for the purposes of this question but it works as I hope and returns an array consisting of two values: A distance integer and a time integer. I can access both by calling:
' Distance
GetDistance(Origin, Destination)(0)
' Time
GetDistance(Origin, Destination)(1)
I would like to update my query to include the time taken to travel to a destination but am unsure how I can do this without calling the GetDistance function twice?
GetDistancefunction expensive, and is it called frequently for the same values? If so, it might be worthwhile to build a dictionary of previous calls and the result; this would not only fix your issue of calling the same function twice (you would have two functions that call the same dictionary), but it could potentially speed up duplicate calls for the same from/to locations