AppendChunk and GetChunk Methods Example

This example uses the AppendChunk and GetChunk methods to fill an image field with data from another record, 32K at a time. In a real application, one might use a procedure like this to copy a record containing a photo or graphic image field 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 rstPubInfo As ADODB.Recordset
	Dim rstPubInfoClone As ADODB.Recordset
	Dim strCnn As String

	' Define connection string.
	strCnn = "driver={SQL Server};server=srv;" & _
		"uid=sa;pwd=;database=pubs"

	' Open two recordsets from the pub_info table.
	Set rstPubInfo = New ADODB.Recordset
	rstPubInfo.CursorType = adOpenKeyset
	rstPubInfo.LockType = adLockBatchOptimistic
	rstPubInfo.Open "pub_info", strCnn
	Set rstPubInfoClone = rstPubInfo.Clone

	' Add a new record to the first Recordset and copy the
	' data from a record in the second Recordset. Display 
	' the size of the logo field before and after the copy
	' to show whether or not the operation was successful.
	With rstPubInfo
		.AddNew
		!pub_id = rstPubInfoClone!pub_id
		!pr_info = rstPubInfoClone!pr_info
		MsgBox "Logo field size before copy: " & _
			!logo.ActualSize
		CopyLargeField rstPubInfoClone!logo, !logo
		.Update
		MsgBox "Logo field size after copy: " & _
			!logo.ActualSize
		.Close
	End With

	rstPubInfoClone.Close

End Sub

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

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

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

	' Copy the logo 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(conChunkSize)
		fldDestination.AppendChunk strChunk
		lngOffset = lngOffset + conChunkSize
	Loop

End Function

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.