Populate from Contact formula

This formula is called from the Populate from Contact button in Call Center. It searches for all addresses related to the contact.

Dim theRes As Result 
dim oAddress as IAddress=ServerApplication.NewComponent ("Hansen.Property.Address",oContact.UserInfo) 

'Where clause 
Dim oWhere As New DBOperativeCollection
Dim myAddresses As New ArrayList 
oWhere.Add(New IDOperativeByCommonID("Property", "AddressContactLink.Contact", 
ComparisonOperator.Equal, oContact.ContactKey)) 
'Create link collection 
dim oLinks as IAddressContactLinkCollection=ServerApplication.NewComponent
("Hansen.Property.AddressContactLinkCollection",oContact.UserInfo) 

'Where clause for tenants ------------------------------------ 
oWhere = New DBOperativeCollection 
oWhere.Add(New IDOperativeByCommonID("Property", "AddressContactLink.Contact", 
ComparisonOperator.Equal, oContact.ContactKey)) 
oWhere.Add(New IDOperativeByCommonID("Property", "AddressContactLink.IsOwner", 
ComparisonOperator.Equal, "N")) 
oWhere.Add(New IDOperativeByCommonID("Property", "AddressContactLink.IsOccupant",
 ComparisonOperator.Equal, "Y")) 

'Load tenant links 
theRes = CType(oLinks, IComponent).Load(oWhere) 
if 
oLinks.Count<1 
then 

'Where clause for owners ------------------------------------ 
oWhere = New DBOperativeCollection 
oWhere.Add(New IDOperativeByCommonID("Property", "AddressContactLink.Contact", 
ComparisonOperator.Equal, oContact.ContactKey)) 
oWhere.Add(New IDOperativeByCommonID("Property", "AddressContactLink.IsOwner", 
ComparisonOperator.Equal, "Y")) 

'Load owner links 
theRes = CType(oLinks, IComponent).Load(oWhere) 
end if 
if 
oLinks.Count<1 
then 

'Where clause for everyone ------------------------------------ 
oWhere = New DBOperativeCollection 
oWhere.Add(New IDOperativeByCommonID("Property", "AddressContactLink.Contact", 
ComparisonOperator.Equal, oContact.ContactKey)) 

'Load everyone links 
theRes = CType(oLinks, IComponent).Load(oWhere) 
end if 

'Add all the returned addresses to return arraylist 
dim oLink as IAddressContactLink 
for each oLink in oLinks 
myAddresses.Add(oLink.Address) 
next 
Dim data As New DataSet 
Dim dt As DataTable = data.Tables.Add() 
dt.Columns.Add("AddressKey", System.Type.GetType("System.Int32")) 
dt.Columns.Add("StreetNumber", System.Type.GetType("System.String")) 
dt.Columns.Add("StreetName", System.Type.GetType("System.String")) 
dt.Columns.Add("Suffix", System.Type.GetType("System.String")) 
dt.Columns.Add("City", System.Type.GetType("System.String")) 
dt.Columns.Add("State", System.Type.GetType("System.String")) 
dt.Columns.Add("ZIP", System.Type.GetType("System.String")) 
Dim i As Integer 
Dim oAddress As IAddress 
for i=0 To myAddresses.Count-1
oAddress = CType(myAddresses(i), IAddress) 
Dim dr As DataRow = dt.NewRow() 
dr("AddressKey") = oAddress.GetProperty("AddressKey") 
dr("StreetNumber") = oAddress.GetProperty("StreetNumber") 
dr("StreetName") = oAddress.GetProperty("StreetName") 
dr("Suffix") = oAddress.GetProperty("Suffix") 
dr("City") = oAddress.GetProperty("City") 
dr("State") = oAddress.GetProperty("State") 
dr("ZIP") = oAddress.GetProperty("ZIP") 
dt.Rows.Add(dr) 
next 
colAddresses.Add(data) 
return theRes