SELECT A.ID, A.Description,
(SELECT B.SomeValue FROM B WHERE B.SomeID = A.ID) as SomeValue
FROM A
2. When you use a domain function (DLookup, DMax, DMin, DSum) or a user defined function in your query, the query parser is unable to see inside the function, so it has no means of determining the optimal execution plan. This is the main reason you don't want to use Domain functions (or UDF's that return data) in queries.
Public Function fnTaxRateLookup(StateCode as Variant, _
ProductCode as Variant, _
RateCode as Variant, _
Optional Reset as Boolean = False, _
Optional CloseRS as Boolean) as Variant
Static myRS As DAO.Recordset
Static myState As Variant
Static myProduct As Variant
Static myRateCode As Variant
Static myRate As Variant
Dim strSQL As String
Dim strCriteria As String
fnTaxRateLookup = Null
If IsNull(StateCode) Or IsNull(ProductCode) Or IsNull(RateCode) Then Exit Function
If (Nz(myState, "") = StateCode) And (Nz(myProduct, "") = ProductCode) _
And (Nz(myRateCode, "") = RateCode) Then
fnTaxRateLookup = myRate
Exit Function
End If
If (myRS Is Nothing) Or Reset Then
Set myRS = CurrentDb.OpenRecordset("qry_Current_Tax_Rates", , dbFailOnError)
End If
Dim strQuote as string
strQuote = chr$(34)
strCriteria = "[State_CD] = " & strQuote & StateCode & strQuote _
& " AND [Product] = " & strQuote & ProductCode & strQuote _
& " AND [Rate_CD] = " & strQuote & RateCode & strQuote
With myRS
.FindFirst strCriteria
IF .NoMatch Then
fnTaxRateLookup = NULL
ELSE
fnTaxRateLookup = !TaxRate
END IF
End With
If (myRS Is Nothing) Or Reset Then
Dim db as DAO.Database
Set db = DBEngine.OpenDatabase("SourceDatabaseName")
Set myRS = Db.OpenRecordset("SourceTableName", dbOpenTable, dbFailOnError)
End If
With myRS
.Index = "StateProductRate" '<= this is the name of the index
.Seek "=", StateCode, ProductCode, RateCode
IF .NoMatch then
fnTaxRateLookup = NULL
ELSE
fnTaxRateLookup = !Tax_Rate
END IF
END With
If CloseRS Then
If (myRS Is Nothing) = False Then
myRS.Close
Set myRS = Nothing
End If
If Reset = False Then Exit Function
End If
In the Close event of the form that uses this function, I simply call the function and pass it NULL values for all the arguments but CloseRS.
fnTaxRateLookup NULL, NULL, NULL,, True
Public Function fnTaxRateLookup(StateCode As Variant, _
ProductCode As Variant, _
RateCode As Variant, _
Optional Reset As Boolean = False, _
Optional CloseRS As Boolean = False) As Variant
Static myRS As DAO.Recordset
Static myState As Variant
Static myProduct As Variant
Static myRateCode As Variant
Static myRate As Variant
Dim strSQL As String
Dim strCriteria As String
On Error GoTo ProcError
'Explicitly set the default return value of the function
fnTaxRateLookup = Null
'If user passed the CloseRS = True, then close the recordset
If CloseRS Then
If (myRS Is Nothing) = False Then
myRS.Close
Set myRS = Nothing
End If
If Reset = False Then Exit Function
End If
'If any of the required arguments is NULL, exit function
If IsNull(StateCode) Or IsNull(ProductCode) _
Or IsNull(RateCode) Then Exit Function
'If all of the required arguments are the same as during the
'previous function call return the previous selected rate
If (Nz(myState, "") = StateCode) _
And (Nz(myProduct, "") = ProductCode) _
And (Nz(myRateCode, "") = RateCode) Then
fnTaxRateLookup = myRate
Exit Function
End If
'Open the recordset if not already open or Reset = true
If (myRS Is Nothing) Or Reset Then
Set myRS = CurrentDb.OpenRecordset("qry_Current_Tax_Rates", , dbFailOnError)
End If
strCriteria = "[State_CD] = " & Quotes(StateCode) & " AND " _
& "[Product] = " & Quotes(ProductCode) & " AND " _
& "[Tax_Rate_Code] = " & Quotes(RateCode)
With myRS
.FindFirst strCriteria
myRate = IIf(.NoMatch, Null, !Tax_Rate)
End With
'set the value of the static variables
myState = StateCode
myProduct = ProductCode
myRateCode = RateCode
fnTaxRateLookup = myRate
ProcExit:
Exit Function
ProcError:
Debug.Print "fnTaxRateLookup", Err.Number, Err.Description
MsgBox Err.Number & vbCrLf & Err.Description, , "fnTaxRateLookup"
End Function
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Author
Commented:I pulled them all out in the latest version.
What I was trying to do was get a label just below the code blocks, centered, but because of the "Select All" and "Open in new Window" hyperlinks below each block, the labels were well below the image.
I guess I could add the text to the the bottom of the code block, but I checked and did not see any references to the code blocks any more, so don't think I need to worry about it.
Thanks.
Commented: