AppendChunk Method

Applies To

Field object.

Description

Appends data from a string expression to a Memo or Long Binary Field object in a Recordset.

Syntax

recordset! field.AppendChunk source

The AppendChunk method syntax has these parts.

Part

Description

recordset

An object variable that represents the Recordset object containing the Fields collection.

field

An object variable that represents the name of a Field object whose Type property is set to dbMemo (Memo), dbLongBinary (Long Binary), or the equivalent.

source

A Variant (String subtype) expression or variable containing the data you want to append to the Field object specified by field.


Remarks

You can use the AppendChunk and GetChunk methods to access subsets of data in a Memo or Long Binary field.

You can also use these methods to conserve string space when you work with Memo and Long Binary fields. Certain operations (copying, for example) involve temporary strings. If string space is limited, you may need to work with chunks of a field instead of the entire field.

If there is no current record when you use AppendChunk, an error occurs.

Notes

  • The initial AppendChunk operation (after an Edit or AddNew call) will simply place the data in the field, overwriting any existing data. Subsequent AppendChunk calls within the same Edit or AddNew session will then add to the existing data.
  • In an ODBCDirect workspace, unless you first edit another field in the current record, using AppendChunk will fail (though no error occurs) while you are in Edit mode.
  • In an ODBCDirect workspace, after you use AppendChunk on a field, you cannot read or write that field in an assignment statement until you move off the current record and then return to it. You can do this by using the MoveNext and MovePrevious methods.
See Also

AddNew method, Edit method, FieldSize property, GetChunk method, Type property.

Example

This example uses the AppendChunk and GetChunk methods to fill an OLE object field with data from another record, 32K at a time. In a real application, one might use a procedure like this to copy an employee record (including the employee's photo) from one table to another. In this example, the record is simply being copied back to same table. Note that all the chunk manipulation takes place within a single AddNew-Update sequence.

Sub AppendChunkX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim rstEmployees2 As Recordset

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    ' Open two recordsets from the Employees table.
    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees", dbOpenDynaset)
    Set rstEmployees2 = rstEmployees.Clone

    ' Add a new record to the first Recordset and copy the
    ' data from a record in the second Recordset.
    With rstEmployees
        .AddNew
        !FirstName = rstEmployees2!FirstName
        !LastName = rstEmployees2!LastName
        CopyLargeField rstEmployees2!Photo, !Photo
        .Update
        ' Delete new record because this is a demonstration.
        .Bookmark = .LastModified
        .Delete
        .Close
    End With

    rstEmployees2.Close
    dbsNorthwind.Close

End Sub

Function CopyLargeField(fldSource As Field, _
    fldDestination As Field)

    ' Set size of chunk in bytes.
    Const conChunkSize = 32768

    Dim lngOffset As Long
    Dim lngTotalSize As Long
    Dim strChunk As String

    ' Copy the photo from one Recordset to the other in 32K
    ' chunks until the entire field is copied.
    lngTotalSize = fldSource.FieldSize
    Do While lngOffset < lngTotalSize
        strChunk = fldSource.GetChunk(lngOffset, conChunkSize)
        fldDestination.AppendChunk strChunk
        lngOffset = lngOffset + conChunkSize
    Loop

End Function
Example (Microsoft Access)

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.
                !Notes.AppendChunk strChunk    ' Append altered data.
                .Update                        ' Save changes.
                .MoveNext                    ' Move to next record.
            End With
        End If
    Loop
    rst.Close
    Set dbs = Nothing
End Sub