Use Dlookup For A Quick Way To Find Items in a Table or Query
These are two examples of using the Dlookup command.
Program Code
' *********************************************************************** ' Loop Through Numerous Dlookup commands - varPersonID is a variant ' *********************************************************************** For lngEEIDKeyIndex = LBound(lngEEIDKeyValues) To UBound(lngEEIDKeyValues) varPersonID = DLookup("[PersonID]", "EmployeeData", "[EEID] = " & lngEEIDKeyValues(lngEEIDKeyIndex)) If Not IsNull(varPersonID) And varPersonID <> 0 Then lngPersonCount = lngPersonCount + 1 ReDim Preserve lngPersonKeyValues(1 To lngPersonCount) lngPersonKeyValues(lngPersonCount) = varPersonID End If Next lngEEIDKeyIndex Exit Sub ' *********************************************************************** ' Get The Rep Code and Name - Note varRepName is a Variant Data Type ' *********************************************************************** strRepCode = recIn!SalesRep varRepName = DLookup("[RepName]", "qryFCSTSalesRepCodesUnique", "[RepCode]=" & """" & strRepCode & """") If Not IsNull(varRepName) Then strRepName = varRepName Else strRepName = "" End If
Syntax Examples
General: DLookup Usage Samples Author Dev Ashish Note that the same logic applies to most Domain Aggregate Functions (DMax, DMin etc.) Normal usage For numerical values: DLookup("FieldName" , "TableName" , "Criteria = n") For strings: (note the apostrophe before and after the value) DLookup("FieldName" , "TableName" , "Criteria= 'string'") For dates: DLookup("FieldName" , "TableName" , "Criteria= #date#") Refering to a form control For numerical values: DLookup("FieldName", "TableName", "Criteria = " & forms!FormName!ControlName) For strings: (note the apostrophe before and after the value) DLookup("FieldName", "TableName", "Criteria = '" & forms!FormName!ControlName & "'") dates: DLookup("FieldName", "TableName", "Criteria = #" & forms!FormName!ControlName & "#") Mix-n-Match DLookup("FieldName", "TableName", "Criteria1 = " & Forms!FormName!Control1 _ & " AND Criteria2 = '" & Forms!FormName!Control2 & "'" _ & " AND Criteria3 =#" & Forms!FormName!Control3 & "#")