When Loading All Does not Load a Constituent

I wrote a recent customisation where I needed to start with an empty database with no constituents and put in a subset of records from my client’s database. I took the sample database and globally deleted all records. So far so good.

Some code that I had written ages ago was going to be a part of this customisation and I ran it as part of a larger piece. The code would load all constituents (OK I forgot that I did not have any) and select the first one. This is a really simple task but I got some strange results.

The code that I wrote was as follows:

    Dim constits As New CRecords
    Dim constit As CRecord
    Dim id As Long
    Dim ind As New CIndividual2

    constits.Init REApplication.SessionContext, tvf_record_All

    If constits.Count > 0 Then
        Set constit = constits(1)
        id = constit.Fields(RECORDS_fld_ID)
        constit.CloseDown
        Set constit = Nothing
    End If

    ind.Init REApplication.SessionContext
    ind.Fields(INDIVIDUAL2_fld_CONSTIT_ID) = id    'Code failed here

To paraphrase the error message that I got it said that I could not assign a non constituent to an individual relationship. What? And then I remembered that I did not have any constituents and that this should never have worked in the first place. But it did get this far.

What was going on? It seems that the filter tvf_record_All took everything in the Records table. When I change the filter to tvf_record_Constituents it worked fine.

I doubt that this would occur very often as most database would have constituents in them but this was certainly an eye opener