OK I have a table called Lease which holds all the information of current and archived leases. Structure is [LeaseID, PropertyID, MoveInDate, StartDate, EndDate, (other info)]. When I create a Lease one of the options is to specify the tenant(s). The tenants are display in a multi select listbox with a Row Source Coming from the Tenants table. On the After Update of that list box i have a bit of code that checks all the the names selected and unselected and either adds or deletes from a junction table called LeaseTenants. Then another bit of code writes the resulting info into a table here is the code that writes to the table.
Code:
Public Function TenantsPerAddressTable() As Boolean
On Error Resume Next
Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
Dim strColumn1 As String, strColumn2 As String
Set db = CurrentDb
sSQL = "SELECT LeaseTenants.LeaseID,[Tenant Name] " _
& "FROM [Tenants Extended] RIGHT JOIN (LeaseTenants RIGHT JOIN [Active Leases] ON LeaseTenants.LeaseID = [Active Leases].LeaseID) ON [Tenants Extended].TenantID = LeaseTenants.TenantID " _
& "ORDER BY [Active Leases].LeaseID, [Tenants Extended].[Tenant Name]"
Set rst = db.OpenRecordset(sSQL, dbOpenSnapshot)
If Not rst.BOF And Not rst.EOF Then
rst.MoveFirst
strColumn1 = rst!LeaseID
strColumn2 = rst![Tenant Name]
rst.MoveNext
Do Until rst.EOF
If strColumn1 = rst!LeaseID Then
strColumn2 = strColumn2 & ", " & rst![Tenant Name]
Else
'check if this property is already listed
If IsNull(DLookup("[LeaseID]", "[TenantsPerAddress]", "LeaseID = " & strColumn1 & "")) Then
sSQL = "INSERT INTO TenantsPerAddress (LeaseID, Tenants) " _
& "VALUES('" & strColumn1 & "','" & strColumn2 & "')"
Else
sSQL = "UPDATE TenantsPerAddress SET Tenants ='" & strColumn2 & "' WHERE LeaseID = " & strColumn1
End If
db.Execute sSQL
strColumn1 = rst!LeaseID
strColumn2 = rst![Tenant Name]
End If
rst.MoveNext
Loop
' Insert Last Record
'check if this property is already listed
If IsNull(DLookup("[LeaseID]", "[TenantsPerAddress]", "LeaseID = " & strColumn1 & "")) Then
sSQL = "INSERT INTO TenantsPerAddress (LeaseID, Tenants) " _
& "VALUES('" & strColumn1 & "','" & strColumn2 & "')"
Else
sSQL = "UPDATE TenantsPerAddress SET Tenants ='" & strColumn2 & "' WHERE LeaseID = " & strColumn1
End If
db.Execute sSQL
End If
Set rst = Nothing
Set db = Nothing
End Function
Problem is, is that the way this is done, if i remove a tenant on a multiple tenant property this change effects the whole lease not just from that time forward. I need to still be able to maintain the the history, that that person actually stayed there for the first however many months.
I don't have alot of DB experience. This is my first major Access DB project. Most of my experience either comes from post secondary and small web projects using MySQL.
Hope that helps