I'm attaching the code per your request -- it's pretty hideous, and I'm sorry for that. Formatting makes more sense in the Module editor. Cost_Item table has a FK from Person.
Note that my problem here is that I'm trying to create an entirely parameter-driven function to get the same values from whatever subset of data I specify. As I see it, I could always create four or so queries and call into the appropriate one depending on what of the optional parameters are null.
-----------------------------------------------
SQLString = "SELECT Hours_AM,Cost_Item.Person_ID,Cost_Item_DT " _
& "FROM Cost_Item INNER JOIN Person ON Person.Person_ID=Cost_Item.Person_ID " _
& "WHERE Cost_Item.WBS_CD='" & WBSCD & "' AND Cost_Item.FY_CD='" & FYCD & "' "
If PersonID > 0 Then
SQLString = SQLString & "AND Cost_Item.Person_ID=" & PersonID
Else
If ContractorCD <> "" Then
SQLString = SQLString & "AND Contractor_CD='" & ContractorCD & "' "
End If
If LaborCategoryCD <> "" Then
SQLString = SQLString & "AND Labor_Category_CD='" & LaborCategoryCD & "' "
End If
End If
If InvoiceID > 0 Then
SQLString = SQLString & "AND Cost_Item.Invoice_ID=" & InvoiceID & " "
Else
SQLString = SQLString & "AND Cost_Item.Invoice_ID is null "
End If
SQLString = SQLString & " AND Cost_Item.Cost_Item_Type_CD = '" & CostItemType _
& "' AND Cost_Item.Cost_Item_DT>=#" & POPStart _
& "# AND Cost_Item.Cost_Item_DT<=#" & POPEnd & "#"