rubbernilly
Programmer
I wrote this code because I often found myself needing to return multiple items from a particular lookup, but the syntax was very often the same and I got tired of dimensioning DAO.Recordsets in every procedure.
vDLookup returns a variant containing an array based on the sort of criteria and syntax the normal DLookup would use to return the first match.
I'm wondering if there is some way to make it more efficient or more widely-applicable so that it would be useful for more people.
Here is the code:
So, the usage would be...
Let's say you needed to return all of a Customer's managers who are setup in your DB with the authority to order. The table name is "CustAgents", the field is "ManagerName."
So, is there a way to make this better? I've thought about declaring an associated Type... off the top of my head:
Since vDLookup relies on redimensioning the array, and that can only be done for the last dimension of an array, this would be a workaround for getting up to (in this case) 5 fields involved in the return. What would be the changes to vDLookup? Hmm, I know that I'd have to dimension vReturns() as vDLookupReturn so that it would get the properties. I could require that Expr be of DLookupReturn Type, so that five fields could be assigned to the object and then those fields could be read into the SQL for the recordset.
What else? I'd appreciate any help to take this to the next level!
vDLookup returns a variant containing an array based on the sort of criteria and syntax the normal DLookup would use to return the first match.
I'm wondering if there is some way to make it more efficient or more widely-applicable so that it would be useful for more people.
Here is the code:
Code:
Public Function vDLookup(Expr As String, Optional Domain As String, Optional Criteria As String) As Variant
Dim vReturns() As String
Dim rs As DAO.Recordset
Dim a As Long
On Error GoTo Err_vDLookup
If IsMissing(Domain) Or Domain = "" Then Domain = "ProgramData" [green]'a default table for my DB configs[/green]
Domain = "SELECT [" & Expr & "] FROM [" & Domain & "]"
If IsMissing(Criteria) = False Or Criteria <> "" Then Domain = Domain & " WHERE " & Criteria
Set rs = CurrentDb.OpenRecordset(Domain)
If rs.EOF Then
GoTo Exit_vDLookup
End If
rs.MoveLast
rs.MoveFirst
For a = 0 To rs.RecordCount - 1
ReDim Preserve vReturns(a)
vReturns(a) = rs(Expr)
rs.MoveNext
Next a
Exit_vDLookup:
Set rs = Nothing
vDLookup = vReturns
Exit Function
Err_vDLookup:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_vDLookup
End Function
So, the usage would be...
Let's say you needed to return all of a Customer's managers who are setup in your DB with the authority to order. The table name is "CustAgents", the field is "ManagerName."
Code:
dim vManagers() as string, a as long
vManagers = vDLookup("ManagerName","CustAgents","CustomerID = " & lngCustID)
[green]'you could use it as the RowSource of a Listbox with Join()[/green]
me.lstCustAgents.RowSource = Join(vManagers)
[green]'or you can loop through it to process code[/green]
if len(join(vManagers)) >0 then
for a = 0 to UBound(vManagers)
[green]'do code[/green]
debug.print vManagers(a)
Next a
end if
So, is there a way to make this better? I've thought about declaring an associated Type... off the top of my head:
Code:
Public Type vDLookupReturn
Field1 as Variant
Field2 as Variant
Field3 as Variant
Field4 as Variant
Field5 as Variant
End Type
Since vDLookup relies on redimensioning the array, and that can only be done for the last dimension of an array, this would be a workaround for getting up to (in this case) 5 fields involved in the return. What would be the changes to vDLookup? Hmm, I know that I'd have to dimension vReturns() as vDLookupReturn so that it would get the properties. I could require that Expr be of DLookupReturn Type, so that five fields could be assigned to the object and then those fields could be read into the SQL for the recordset.
What else? I'd appreciate any help to take this to the next level!