![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
tblproduct (simplified) productid(pk) productcode productcost productprice warrantycode any warranty claim is invoiced out as the original productcode but with a w in front e.g. original code 3005015TPR becomes W3005015TPR (both of which are in [productcode]) *the warrantycode field is only used where the productcode starts with a W then three digits and is exactly the same as the productcode this gives two records, the original product with [productcode] as 3005015TPR and [warrantycode] as null, and the warranty product with [productcode] as W3005015TPR and [warrantycode] as 3005015TPR. when the warranty product is invoiced I want to use dlookup to find the cost of the original product so something like dlookup([productcost],"tblproduct", productcode = warrantycode) i.e. lookup the cost of the original product. keep getting errors. really want to use this in an update query which will select all the products begining W### and update the productcost of the warranty product with the productcost of the original product thanks in advance |
#3
| |||
| |||
|
|
tblproduct (simplified) productid(pk) productcode productcost productprice warrantycode any warranty claim is invoiced out as the original productcode but with a w in front e.g. original code 3005015TPR becomes W3005015TPR (both of which are in [productcode]) the warrantycode field is only used where the productcode starts with a W then three digits and is exactly the same as the productcode this gives two records, the original product with [productcode] as 3005015TPR and [warrantycode] as null, and the warranty product with [productcode] as W3005015TPR and [warrantycode] as 3005015TPR. when the warranty product is invoiced I want to use dlookup to find the cost of the original product so something like dlookup([productcost],"tblproduct", productcode = warrantycode) i.e. lookup the cost of the original product. keep getting errors. really want to use this in an update query which will select all the products begining W### and update the productcost of the warranty product with the productcost of the original product thanks in advance |
#4
| |||
| |||
|
|
Suggest you use ELookup which apparently is much faster than DLookup Public Function ELookup(expr As String, domain As String, Optional Criteria, Optional OrderClause) On Error GoTo Err_ELookup 'Purpose: Faster and more flexible replacement for ELookup() 'Arguments: Same as ELookup, with additional Order By option. 'Return: Value of the Expr if found, else Null or #Error. 'Author: Allen Browne. abrowne1 (AT) bigpond (DOT) net.au 'Examples: '1. To find the last value, include DESC in the OrderClause, e.g.: ' ELookup("[Surname] & [FirstName]", "tblClient", , "ClientID DESC") '2. To find the lowest non-null value of a field, use the Criteria, 'e.g.: ' ELookup("ClientID", "tblClient", "Surname Is Not Null" , "Surname") 'Note: Requires a reference to the DAO library. Dim MyDb As Database Dim rs As Recordset Dim SQLStg As String 'Build the SQL string. SQLStg = "SELECT TOP 1 " & expr & " FROM " & domain If Not IsMissing(Criteria) Then SQLStg = SQLStg & " WHERE " & Criteria End If If Not IsMissing(OrderClause) Then SQLStg = SQLStg & " ORDER BY " & OrderClause End If SQLStg = SQLStg & ";" 'Lookup the value. Set MyDb = DBEngine(0)(0) Set rs = MyDb.OpenRecordset(SQLStg, dbOpenForwardOnly) If rs.RecordCount = 0 Then ELookup = Null Else ELookup = rs(0) End If rs.Close Exit_ELookup: Set rs = Nothing Set MyDb = Nothing Exit Function Err_ELookup: ' MsgBox Err.Description, vbExclamation, "ELookup Error " & Err.number If Err.Number < 0& Or Err.Number > 65535 Then 'Valid range for CVErr() ELookup = CVErr(5) 'Out of range. Else ELookup = CVErr(Err.Number) End If Resume Exit_ELookup End Function Then Try ELookup("productcost","tblproduct","productcode= " & chr$(34) & warrantycode & chr$(34)) Chr$(34) is a double quotation mark and much less confusing than "'" Phil |
#5
| |||
| |||
|
|
"Phil Stanton" <p... (AT) stantonfamily (DOT) co.uk> wrote innews:13paam6b2rtq578 (AT) corp (DOT) supernews.com: Suggest you use ELookup which apparently is much faster than DLookup Public Function ELookup(expr As String, domain As String, Optional Criteria, Optional OrderClause) * * On Error GoTo Err_ELookup * * 'Purpose: * Faster and more flexible replacement for ELookup() * * 'Arguments: Same as ELookup, with additional Order By option. * * 'Return: * *Value of the Expr if found, else Null or #Error. * * 'Author: * *Allen Browne. abrow... (AT) bigpond (DOT) net.au * * 'Examples: * * '1. To find the last value, include DESC in the OrderClause, e.g..: * * ' *ELookup("[Surname] & [FirstName]", "tblClient", , "ClientID * * DESC") '2. To find the lowest non-null value of a field, use the * * Criteria, 'e.g.: * * ' *ELookup("ClientID", "tblClient", "Surname Is Not Null" , * * "Surname") 'Note: * * *Requires a reference to the DAO library. * * Dim MyDb As Database * * Dim rs As Recordset * * Dim SQLStg As String * * 'Build the SQL string. * * SQLStg = "SELECT TOP 1 " & expr & " FROM " & domain * * If Not IsMissing(Criteria) Then * * * * SQLStg = SQLStg & " WHERE " & Criteria * * End If * * If Not IsMissing(OrderClause) Then * * * * SQLStg = SQLStg & " ORDER BY " & OrderClause * * End If * * SQLStg = SQLStg & ";" * * 'Lookup the value. * * Set MyDb = DBEngine(0)(0) * * Set rs = MyDb.OpenRecordset(SQLStg, dbOpenForwardOnly) * * If rs.RecordCount = 0 Then * * * * ELookup = Null * * Else * * * * ELookup = rs(0) * * End If * * rs.Close Exit_ELookup: * * Set rs = Nothing * * Set MyDb = Nothing * * Exit Function Err_ELookup: ' * *MsgBox Err.Description, vbExclamation, "ELookup Error " & Err.number * * If Err.Number < 0& Or Err.Number > 65535 Then * *'Valid range for CVErr() * * * * ELookup = CVErr(5) * * * * * * * * ** * * *'Out of range. * * Else * * * * ELookup = CVErr(Err.Number) * * End If * * Resume Exit_ELookup End Function Then Try ELookup("productcost","tblproduct","productcode= " & chr$(34) & warrantycode & chr$(34)) Chr$(34) is a double quotation mark and much less confusing than "'" Phil That's elaughable, just what we needed on Blue Monday |
![]() |
| Thread Tools | |
| Display Modes | |
| |