AppendChunk, GetChunk Methods Example (MDB)

The following example appends data to the Notes field for each record in an Employees table. The data type of the Notes field is Memo. The procedure returns the contents of the field by using the GetChunk method, adds to the data, and appends the altered data back to the Notes field by using the AppendChunk method.

Sub AddToMemo()
    Dim dbs As Database, rst As Recordset
    Dim fldNotes As Field, fldFirstName As Field
    Dim fldLastName As Field
    Dim lngSize As Long, strChunk As String

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Create table-type Recordset object.
    Set rst = dbs.OpenRecordset("Employees")
    ' Return references to Field objects.
    Set fldNotes = rst!Notes
    Set fldFirstName = rst!FirstName
    Set fldLastName = rst!LastName
    
    ' Loop through all records in recordset.
    Do Until rst.EOF
        ' Check if data exists in Notes field.
        If IsNull(fldNotes.Value) Then
            ' If no data, use AppendChunk method only.
            strChunk = fldFirstName _
                & " " & fldLastName & " is an excellent employee."
            With rst
                .Edit
                !Notes = strChunk
                .Update
                .MoveNext
            End With
        Else
            lngSize = Len(fldNotes)
            ' Use GetChunk to retrieve existing data.
            strChunk = fldNotes.GetChunk(0, lngSize)
            ' Alter data.
            strChunk = strChunk & "  " & fldFirstName _
                & " " & fldLastName & " is an excellent employee."
            With rst
                .Edit                             ' Enable editing.
                !Notes = ""                     ' Initialize field.
                ' Append altered data.
                !Notes.AppendChunk strChunk
                .Update                         ' Save changes.
                ' Move to next record.
                .MoveNext
            End With
        End If
    Loop
    rst.Close
    Set dbs = Nothing
End Sub