1

Topic: Adding data to the table TAXON_OCCURRENCE_DATA

Hello,
I need to populate records, which are without a measurement in Recorder with information about the abundance (always P for just present).
I would like to do that with a query in Access. As far as I can see, I have to add records into the table TAXON_OCCURRENCE_DATA. I've already a query, where I generate all values for the table TAXON_OCCURRENCE_DATA apart form the field TAXON_OCCURRENCE_DATA_KEY.
I now wonder, if there is an easy way to generate the values for this primary key field?
If I manage to generate values for this field, will I get problems, when I later enter records via the standard user interface and Recorder would like to use these values?

Thanks,
Wolfgang

2

Re: Adding data to the table TAXON_OCCURRENCE_DATA

I do this quite a lot.

Below are two Access functions which might help.

First you need the last key which was used on the TAXON_OCCURRENCE_DATA table. Before you do this, run the R6 Addin UpdateLastKey to make sure the Last_KEY table is properly sychronised) I use this function (but there are many different ways of doing it, I do it this way for historic reasons which I won't go into (of course it assumes that your access database has the necessary tables linked into it.) The parameter TableName should be set to "TAXON_OCCURRENCE_DATA"

Public Function LastKey(ByVal TableName As String) As String

    Dim KeySet As Recordset
    Set KeySet = CurrentDb.OpenRecordset("SELECT LAST_KEY.LAST_KEY_TEXT " & _
                                            "FROM LAST_KEY " & _
                                            "WHERE (((LAST_KEY.TABLE_NAME)='" & TableName & "'));")
    KeySet.MoveFirst
    LastKey = KeySet!LAST_KEY_TEXT
   
End Function

The output from this is fed into the following function, which gives you the last eight characters of the next available key.

Public Function IncrementKey(ByVal LastKey As String) As String

    'Increments key (base 36)
    Dim ThisChar, IncChar As String, CharPlace, CarryOver As Integer
    CharPlace = 8: CarryOver = 1
    Do While CharPlace > 0
        ThisChar = Mid(LastKey, CharPlace, 1)
        If CarryOver = 1 Then
            Select Case ThisChar
                Case "9"
                    IncChar = "A"
                    CarryOver = 0
                Case "Z"
                    IncChar = "0"
                    CarryOver = 1
                Case Else
                    IncChar = Chr(Asc(ThisChar) + 1)
                    CarryOver = 0
            End Select
        Else
            IncChar = ThisChar
            CarryOver = 0
        End If
        LastKey = Left(LastKey, CharPlace - 1) & IncChar & Right(LastKey, 8 - CharPlace)
        CharPlace = CharPlace - 1
    Loop
    If LastKey <> "00000000" Then
        IncrementKey = LastKey
    Else
        IncrementKey = "#ERROR#"
    End If
   
End Function

Simply add your 8 character SiteID to the beginning of this to get the full 16 character key.

Again there are lots of ways which you can make this work as part of an Access query. My preferred one is to create a Global variable in access to hold the LastKey value (as output from the first function), then update it with the output from the second function each time it runs. This should give you sequential keys.

At the end of the process, once you have inserted all the data be sure to run UpdateLastKey again to synchronise the table.

Does that make sense, or have I just baffled you?

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

3

Re: Adding data to the table TAXON_OCCURRENCE_DATA

Hi Rob,
thanks for your help. It looks reasonable to me. I'm only a little bit worried about the R6 Addin UpdateLastKey. I probably don't have enough admin rights and might have to negotiate with our IT.

Thanks a lot,
Wolfgang

4

Re: Adding data to the table TAXON_OCCURRENCE_DATA

That addin is essential. If you do not have it installed, then you will definately need to talk to your IT people.

If you do not run the addin after the process above the next time you try to add data through the R6 UI it will assign a key which has already been used (by you) and an error will occur. A lack of sychrony in the LAST_KEY table can sometimes occur for other reasons when you are using recorder normally, so its a good tool to have.

However you can produce the same effect using SQL So:

UPDATE LAST_KEY
SET LAST_KEY.LAST_KEY_TEXT = (SELECT Max(RIGHT(TAXON_OCCURRENCE_DATA_KEY, 8) FROM TAXON_OCCURRENCE_DATA WHERE LEFT(TAXON_OCCURRENCE_DATA_KEY, 8) = 'your site ID goes here')
WHERE LAST_KEY.TABLE_NAME = 'TAXON_OCCURRENCE_DATA'

Should produce the same effect (but be sure to back up before trying this)

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre