Using the Windows clipboard


It's easy to read and write data on the Windows clipboard from FoxPro. This is often the easiest way to transfer data between applications. It's not as sophisticated as 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]

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

This example uses 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 Excel.

DataToClip()

DataToClip is a method of the application object or 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()

followed by CTRL+V will put the following text onto the clipboard and paste it back into the Command Window:

[Text in the Command Window]

There's not enough space to show the entire output but every field of 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. This screen shot is from VFP 9 with the View White Space option selected.

Parameters

DataToClip() accepts up to three parameters:

  • cAlias: Copy records from this work area
  • nRecords: Copy a specific number of records
  • nFormat&tab: 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 command. It cannot accept a FOR clause and the act of copying to the clipboard does not move the record pointer.


Hints & tips

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.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map