G
Guest
I am trying to find the most efficient way to look up values twice-removed
from the data used to perform the lookup.
Simple example:
Company table
CompanyID (PK)
CompanyName
Client table
ClientID (PK)
CompanyID (FK)
ClientName
Invoice table
InvoiceID (PK)
ClientID (FK)
Now, after creating the obvious relationships, I want to look up the
CompanyName using the InvoiceID.
Method 1: Create a joining query and single function
SELECT Company.CompanyName
FROM Company INNER JOIN (Client INNER JOIN Invoice ON Client.ClientID =
Invoice.ClientID) ON Company.CompanyID = Client.CompanyID
WHERE (((Invoice.InvoiceID)=[Enter invoice ID]));
Public Function GetCompanyName(ID As Long) As String
GetCompanyName = DLookup("[CompanyName]", "[myQuery]", "[ClientID]=" & ID)
End Function
and call it like this (from VBA on an Invoice form):
Company = GetCompanyName(ClientID)
Method #2: Create two functions and nest them
Public Function GetCompanyID(ID As Long) As Long
GetCompanyID = DLookup("[CompanyID]", "[Client]", "[ClientID]=" & ID)
End Function
Public Function GetCompanyName(ID As Long) As String
GetCompanyName = DLookup("[CompanyName]", "[Company]", "[CompanyID]=" & ID)
End Function
Call them like this:
Company = GetCompanyName(GetCompanyID(ClientID)
Here are my questions
1. Would it be more efficient to use the nested functions, each against a
table, or a single function against a joining query?
2. Is there a better method than DLookup to use within the function(s)? The
function could be called from VBA on a form, but is more likely to be called
from within a query where I need the CompanyName for each of many records on
a list (e.g. a RowSource of a combo box).
I understand that it could be more efficient to open a recordset from within
the function, but how can I do that once for my entire query rather than once
for each record? Or is it still more efficient to open the recordset for each
record than to run a DLookup?
from the data used to perform the lookup.
Simple example:
Company table
CompanyID (PK)
CompanyName
Client table
ClientID (PK)
CompanyID (FK)
ClientName
Invoice table
InvoiceID (PK)
ClientID (FK)
Now, after creating the obvious relationships, I want to look up the
CompanyName using the InvoiceID.
Method 1: Create a joining query and single function
SELECT Company.CompanyName
FROM Company INNER JOIN (Client INNER JOIN Invoice ON Client.ClientID =
Invoice.ClientID) ON Company.CompanyID = Client.CompanyID
WHERE (((Invoice.InvoiceID)=[Enter invoice ID]));
Public Function GetCompanyName(ID As Long) As String
GetCompanyName = DLookup("[CompanyName]", "[myQuery]", "[ClientID]=" & ID)
End Function
and call it like this (from VBA on an Invoice form):
Company = GetCompanyName(ClientID)
Method #2: Create two functions and nest them
Public Function GetCompanyID(ID As Long) As Long
GetCompanyID = DLookup("[CompanyID]", "[Client]", "[ClientID]=" & ID)
End Function
Public Function GetCompanyName(ID As Long) As String
GetCompanyName = DLookup("[CompanyName]", "[Company]", "[CompanyID]=" & ID)
End Function
Call them like this:
Company = GetCompanyName(GetCompanyID(ClientID)
Here are my questions
1. Would it be more efficient to use the nested functions, each against a
table, or a single function against a joining query?
2. Is there a better method than DLookup to use within the function(s)? The
function could be called from VBA on a form, but is more likely to be called
from within a query where I need the CompanyName for each of many records on
a list (e.g. a RowSource of a combo box).
I understand that it could be more efficient to open a recordset from within
the function, but how can I do that once for my entire query rather than once
for each record? Or is it still more efficient to open the recordset for each
record than to run a DLookup?