The creation and customization of database applications using Microsoft Access
Use a single text string with row delimiters and then put that string on the Windows/Office Clipboard; Excel (and other apps) will split it into rows when pasted.
For the specific function shown, build the string as already done, then move it to the Clipboard using either:
- Access
RunCommand(for a control’s contents), or - A
DataObject/ Clipboard API (for arbitrary text).
1. Loading the Clipboard from Access VBA
If the text is in a control (for example, a text box on a form):
Private Sub cmdCopy_Click()
Me!txtNotes = LoadClipboard("1356, 1360, 1366, 1368")
Me!txtNotes.SetFocus
DoCmd.RunCommand acCmdCopy
End Sub
DoCmd.RunCommand acCmdCopy copies the active control’s contents to the Clipboard.
Reference: Send information to the Clipboard
If the text is not in a control, use a DataObject and PutInClipboard to place the string directly on the Clipboard:
Dim MyData As DataObject
Sub CopyListToClipboard()
Dim s As String
s = LoadClipboard("1356, 1360, 1366, 1368") ' builds CRLF-delimited string
Set MyData = New DataObject
MyData.SetText s
MyData.PutInClipboard
End Sub
PutInClipboard transfers the data from the DataObject to the Clipboard. Excel will interpret vbCrLf as row breaks when pasting.
Reference: Paste, PutInClipboard, SetText methods example
2. Appending to the Clipboard
The standard Clipboard itself does not support “append” in the sense of keeping multiple separate text items and concatenating them later. Each time text is placed on the Clipboard in the same format, the previous text in that format is replaced.
To simulate appending:
- Maintain an accumulator string in code (for example,
ClipboardText = ClipboardText & NewChunk & vbCrLf). - When finished, call
SetText/PutInClipboardonce with the final accumulated string.
This matches the pattern already used in LoadClipboard.
3. Overwrite behavior
For text formats, the Clipboard holds one value per format. When new text is placed on the Clipboard in that format, the previous text is discarded.
From the Clipboard/DataObject guidance:
- “If you store data with a format that is already in use, the new data is saved and the old data is discarded.”
Reference: What is the difference between the DataObject and the Clipboard?
So:
- Load the Clipboard by copying from a control (
acCmdCopy) or by using aDataObjectwithSetText+PutInClipboard. - To “append,” build the full string in VBA and write it to the Clipboard once.
- The Clipboard accepts one value per format; new text in the same format overwrites the previous text.
References: