"Phil" <
ph...@stantonfamily.co.uk> wrote in message
news:k65loo$b2u$1...@speranza.aioe.org...
To eliminate all the string issues in dLookup, Dsum ect. I modified Allen
Browne's
abro...@bigpond.net.au functions to allow parameter queries.
Following is pLookup version of eLookup
Public Function pLookup(strExpr As String, strDomain As String, strCriteria
As String, _
strOrderClause As String, ParamArray vaParameters()
As Variant)
On Error GoTo Err_pLookup
'Purpose: Modify elookup
'Arguments: strExpr Column to return
' strDomain Query
' strCriteria Extra search criteria
' strOrderClause Extra order by, to return specific if more
than 1 record matches
' ParamArray List of query parameter values
'Return: Value of the strExpr if found, else Null or #Error.
'Example:
'1. To search the saved parameter query
' pLookup("[ID]", "qryContactLookup", "","", "TREEO'S SERVICES")
'2. Search with built up query,
' note: strCriteria and strOrderClause will be ignored if PARAMETERS
is at start of strDomain
' pLookup("[ID]", "PARAMETERS ContactName Text ( 255 );
' SELECT TOP 1 tblContact.ID FROM tblContact
' WHERE tblContact.Name1=[ContactName];",
vbNullString, vbNullString, "TREEO'S SERVICES")
'Note: Requires a reference to the DAO library.
Dim db As Database
Dim rs As Recordset
Dim qryMyQuery As QueryDef
Dim SQLStg As String
Dim i As Integer
If UCase(Left$(strDomain, 10)) = "PARAMETERS" Then
SQLStg = strDomain
Else
'Build the SQL string.
SQLStg = "SELECT TOP 1 " & strExpr & " FROM " & strDomain
If strCriteria <> vbNullString Then
SQLStg = SQLStg & " WHERE " & strCriteria
End If
If strOrderClause <> vbNullString Then
SQLStg = SQLStg & " ORDER BY " & strOrderClause
End If
SQLStg = SQLStg & ";"
End If
'Lookup the value.
Set db = dbLocal()
Set qryMyQuery = db.CreateQueryDef("", SQLStg) ' Create a temp query
from SQL string
For i = 0 To UBound(vaParameters())
qryMyQuery.Parameters(i) = vaParameters(i)
Next
Set rs = qryMyQuery.OpenRecordset(dbOpenForwardOnly)
If rs.RecordCount = 0 Then
pLookup = Null
Else
pLookup = rs(0)
End If
rs.Close
Exit_pLookup:
Set rs = Nothing
Set db = Nothing
Set qryMyQuery = Nothing
Exit Function
Err_pLookup:
Debug.Print Err.Description, vbExclamation, "pLookup Error " &
Err.Number
If Err.Number < 0& Or Err.Number > 65535 Then 'Valid range for
CVErr()
pLookup = Null ' CVErr(5) 'Out of range.
Else
pLookup = Null ' CVErr(Err.Number)
End If
Resume Exit_pLookup
End Function