ADO - ADO Field AppendChunk and GetChunk Methods

The AppendChunk and GetChunk methods in ADO are used to work with large text or binary data stored in database fields. They are especially useful when a field contains a large amount of data, such as documents, images, audio files, video data, or lengthy text. Instead of transferring the entire value between the application and the database at once, these methods allow the data to be processed in smaller portions called chunks.

1. Why AppendChunk and GetChunk Are Needed

Normally, an ADO application can read or write a field value directly:

value = rs.Fields("Description").Value

This works well when the field contains a relatively small amount of data. However, large objects can consume considerable memory when loaded completely into the application.

For example, a database may contain a field storing:

  • Images

  • PDF documents

  • Word documents

  • Large text documents

  • Audio files

  • Video files

  • Other binary objects

For such data, processing everything at once may be inefficient. ADO provides GetChunk and AppendChunk to handle large values incrementally.

2. GetChunk Method

The GetChunk method is used to retrieve a portion of a large field value from a database.

Its general syntax is:

Field.GetChunk(Size)

Here, Size specifies the approximate number of bytes or characters that should be retrieved in the current chunk.

For example:

data = rs.Fields("DocumentData").GetChunk(1024)

This requests a chunk of data from the DocumentData field.

If the field contains a large binary object, the returned value is generally represented as a byte array. For large text data, the returned value can be a string depending on the field's data type and provider.

3. Reading Large Data in Multiple Chunks

Suppose a database contains a large image in a field named ImageData. Instead of attempting to retrieve the complete image at once, the application can retrieve it in smaller portions.

A simplified example is:

Dim chunk
Dim imageData

Do Until rs.Fields("ImageData").EOF
    chunk = rs.Fields("ImageData").GetChunk(1024)

    'Process the chunk here
Loop

The actual implementation normally needs to maintain the position within the field and determine when the complete value has been retrieved.

The main idea is:

Large database value
        |
        v
+-------+-------+-------+-------+
|Chunk 1|Chunk 2|Chunk 3|Chunk 4|
+-------+-------+-------+-------+
        |
        v
Application processes each chunk

This approach can reduce the amount of large data that needs to be handled at one time.

4. AppendChunk Method

The AppendChunk method is used to write large data to an ADO field incrementally.

Its general syntax is:

Field.AppendChunk Data

Here, Data represents the portion of data that should be added to the field.

For example:

rs.Fields("ImageData").AppendChunk imageChunk

Instead of assigning an entire large binary object to the field at once, the application can append successive chunks.

5. Writing Large Data Using AppendChunk

Consider an application that needs to store an image in a database. The image can be read from a file in smaller portions.

Conceptually, the process is:

Image file
   |
   v
Read Chunk 1
   |
   v
AppendChunk
   |
Read Chunk 2
   |
   v
AppendChunk
   |
Read Chunk 3
   |
   v
AppendChunk
   |
   v
Complete database field

A simplified example could look like:

rs.Fields("ImageData").AppendChunk chunk1
rs.Fields("ImageData").AppendChunk chunk2
rs.Fields("ImageData").AppendChunk chunk3

After all chunks have been appended, the complete value can be saved by updating the record.

rs.Update

6. AppendChunk and Binary Data

One of the important uses of AppendChunk is handling binary large objects, commonly referred to as BLOBs.

Examples include:

Images
PDF files
Audio files
Video files
Scanned documents
Compressed files

For example, if a database field stores an image as binary data, the image can be divided into smaller byte arrays and appended sequentially.

This is useful because binary data can become very large, and transferring it in manageable pieces can be more practical than loading the entire object into memory.

7. AppendChunk and Large Text

These methods are not limited to images and other binary objects. They can also be used with large text fields.

For example, an application could store a large document by dividing the document into multiple pieces:

Large text document
        |
        +---- Part 1
        +---- Part 2
        +---- Part 3
        +---- Part 4
        |
        v
Database large-text field

Each part can be appended using AppendChunk.

Similarly, GetChunk can retrieve the stored text in portions.

8. Difference Between GetChunk and AppendChunk

The two methods perform opposite operations.

Method Purpose Direction
GetChunk Retrieves part of a large field value Database to application
AppendChunk Adds part of a large value to a field Application to database

In simple terms:

GetChunk
Database → Application

AppendChunk
Application → Database

9. Important Considerations

When using these methods, the application should pay attention to the following points.

First, the field must support the type of large data being processed. The exact behavior can also depend on the database provider.

Second, the application should select an appropriate chunk size. Very small chunks can result in many operations, while excessively large chunks can reduce the memory advantage of chunk-based processing.

Third, binary and text data should be handled according to their appropriate data types. Treating binary data as ordinary text can result in data corruption.

Fourth, the Recordset and Field objects must remain valid while the chunk operations are being performed.

10. Advantages of Chunk-Based Processing

The main advantage is controlled data transfer.

For large objects, chunking can provide:

  • Lower memory usage

  • More controlled data transfer

  • Better handling of large binary objects

  • More manageable processing of large text fields

  • Reduced need to hold the complete object in memory simultaneously

However, chunking does not automatically guarantee better performance in every situation. The ideal approach depends on the database provider, network conditions, object size, and application design.

11. Simple Example

A simplified conceptual example of reading data using GetChunk is:

Dim rs
Dim chunk

Set rs = conn.Execute("SELECT DocumentData FROM Documents")

If Not rs.EOF Then
    chunk = rs.Fields("DocumentData").GetChunk(4096)

    'Process the retrieved chunk
End If

For writing data, the conceptual operation is:

rs.Fields("DocumentData").AppendChunk chunk
rs.Update

In a real application, multiple chunks would normally be processed until the complete object has been transferred.

12. Summary

The ADO GetChunk and AppendChunk methods provide a mechanism for handling large field values in smaller pieces. GetChunk retrieves portions of data from a field, while AppendChunk adds portions of data to a field. They are particularly useful for large text and binary objects such as documents, images, audio, and other BLOB data.

Understanding these methods is important when developing traditional ADO applications that need to work efficiently with large database values, especially where loading the entire value into memory at once would be undesirable.