FoxPro TextMerge


You can use the Copy To command to create a simple text file from a table with various forms of delimiter between fields. The TextMerge commands let you create a more complex text file. The contents can be a mixture of plain text, fields and variables, and FoxPro functions.

The commands are:

Set Textmerge On

Tell Fox to evaluate anything it sees between << and >>. These delimiters can be changed with the Set Delimiters To command.

Set Textmerge To <filename> Noshow

Direct the output to a file. The file will be created if it does not already exist. If the file does exist and Safety is On then you will be asked whether you want to overwrite it. The Noshow clause suppresses output to the screen.

\xyz

Send the characters 'xyz' to the file preceded by a CR LF combination. There is no need for quotes around the characters. If you put quotes around the characters then the quotes will be output as well.

\\xyz

Send the characters 'xyz' to the file on the current line.

<<foxpro expression>>

Evaluate the FoxPro expression. The expression can be the name of a field or memory variable or a FoxPro function. The example below shows how you can mix <<foxpro expression>> with plain text on the same line.

Set TextMerge To

Close the output file.

Set Textmerge Off

Stop evaluating expressions within << and >>.

Text Merge Example

This example produces an HTML page holding a simple list of customers:


Use Customer

*-- Start the merge process
*-- Use the noshow option so that no output
*-- shows on screen
Set Textmerge On
Set Textmerge To customer.htm noshow

*-- Just a minimal HTML document
\<html>
\<head>
\<title>HTML from FoxPro</title>
\</head>

*-- Now for the body
\<body>

*-- Start with a level 1 heading and a line
\<h1>Customer List</h1>
\<hr />

*-- Output one line for each record in the table
Scan
   \Company name is <<Upper(Company)>>
   \<br />
Endscan

*-- Closing tags for the html page
\</body>
\</html>

*-- Close the text file and stop merging
Set Textmerge To
Set Textmerge Off
Use In Customer

This short program will produce a text file named 'customer.htm' which looks like this in a browser:

This is a very simple page and the mixture of TextMerge and HTML can be confusing to write because both use the < and > symbols. It is however a very powerful and flexible technique which lets you build a page which will display a mixture of text and data.

Visual FoxPro offers other ways of generating HTML. These are described on the HTML from FoxPro page.

Gotcha

There are two things to watch out for when using text merge and a third when using it to produce HTML:

  • Everything on the line after a \ or \\, including any trailing spaces, will be output.
  • The carriage return/linefeed comes before the characters are output.
  • Remember that a \\ produces a new line of output in the text of the HTML file. It will not produce a new line on the web page. You never really need to use a \\ when generating HTML because browsers ignore new lines in the HTML source.

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