Using the Windows clipboard

It is very easy to read and write data on the Windows clipboard from Visual FoxPro and this is often the easiest way to transfer data between applications. The clipboard is not as sophisticated as using Automation or DDE but it's quick and simple and popular with users because it is a flexible technique which leaves them in control of the process.

The two commands are _CLIPTEXT to copy text to the clipboard and DataToClip() to copy values from a table.

_CLIPTEXT

Use the _CLIPTEXT system variable to read and write text to the clipboard. For example a button on a Customer form can assemble the four lines of the address into a block of text on the clipboard:

Declare laLines[4]

*-- Start with an empty array of lines.
laLines = ""
lnLine = 1

*-- Add fields to the array if there's something in them.
If Not Empty(Address1)
   laLines[lnLine] = Alltrim(Address1)
   lnLine = lnLine + 1
EndIf

If Not Empty(Address2)
   laLines[lnLine] = Alltrim(Address2)
   lnLine = lnLine + 1
EndIf

If Not Empty(Town)
   laLines[lnLine] = Alltrim(Town)
   lnLine = lnLine + 1
EndIf

If Not Empty(County)
   laLines[lnLine] = Alltrim(County)
   lnLine = lnLine + 1
EndIf

_CLIPTEXT = laLines[1] + Chr (13) + ;
            laLines[2] + Chr(13) + ;
            laLines[3] + Chr(13) + ;
            laLines[4]

With the address on the clipboard the user can switch to Microsoft Word and paste the block as the header of a letter or go to Excel and paste the address into an invoice.

This example used CHR(13) to insert a carriage return between each field so that the address will paste as four lines in Word or Excel. If you use CHR(9) to insert tab characters then the address will paste as four cells on the same line in a Word table or in Excel.

DataToClip()

DataToClip is a method of the application object and of the _VFP system variable. It copies data from the current work area onto the clipboard. If the TasTrade Customer table is open then the command:

_vfp.DataToClip()

will copy all the records of the current table onto the clipboard. The image below shows the effect of _vfp.DataToClip() followed by CTRL+V to paste the clipboard contents back into the Command Window:

[Table contents in the Command Window]

There's not enough space here to show the entire output but every field of every row of the table has been placed on the Clipboard. Fields are separated by a single space and each record is on a new line. The Cust_ID field is six characters wide. Each ID is only five characters long so there is a trailing space in the field followed by the space which separates fields.

Note that this screen shot is from Visual FoxPro 9 with the View White Space option selected to make the spaces visible.

Parameters

DataToClip() accepts up to three parameters:

  • cAlias: The work area from which records are copied
  • nRecords: Copy a specific number of records
  • nFormat: 1=Delimit with spaces, 3=Delimit with tabs

Notes

There are some annoying limitations to DataToClip():

  • The first line of the output will always be the field names.
  • There is no way of selecting which fields will be copied.
  • Memo fields are represented as 'Memo' or 'memo' depending on whether they hold any text.
  • General fields are not copied.

The best way to use DataToClip() is in conjunction with an SQL statement which will select the fields and records required into a cursor.

Finally, note that DataToClip() does not behave like a traditional FoxPro data export command. It cannot accept a FOR clause and the act of copying to the clipboard does not move the record pointer.

Related Items

Alvechurch Data - Microsoft FoxPro and Access Database Development and Training

Alvechurch Data are based close to Birmingham and provide Microsoft Access and Visual FoxPro training, development and support.

Read More

Autocomplete in Visual Fox Pro

Autocomplete in Visual Fox Pro

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.

Read More

FoxPro Functions

FoxPro has always had functions like FREAD and FWRITE to read and write files at a low level. They can handle files which defeat the STRTOFILE and FILETOSTR functions.

Read More

Development Services

Alvechurch Data specialise in Microsoft Access and Visual FoxPro databases and have fifteen years experience developing databases for small business use.

Read More

Access and Visual FoxPro Training

Microsoft Access and Visual FoxPro training courses and workshops offered by Alvechurch Data in Birmingham.

Read More