-
5
- #1
Mike Lewis
Programmer
Isn't it funny. However well you think you know a product such as VFP, there are always hidden facts lurking in its depths that you either never knew about, or you once knew but have long since forgotten.
I came across this recently when I was asked to create a report that would print all the data from any arbitrary table or cursor. The report wouldn't know in advance which table it was to operate on, or what fields the table contained.
This is an example of the required output:
[tt] Product Name: Gustaf's Knäckebröd
English Name: Gustaf's Rye Crisp Bread
Category: BA
Unit size: 24 - 500 g pkgs.
Product Name: Tunnbröd
English Name: Thin Bread
Category: BA
Unit size: 12 - 250 g pkgs.
Product Name: Gumbär Gummibärchen
English Name: Gumbär Gummy Bears
Category: CS
Unit size: 100 - 250 g bags[/tt]
Two possible approaches came to mind.
The first was to actually create an FRX file programmatically. I did this once before, a few years ago, when I needed to create a generic function to print the contents of a grid. (I published that code in FoxPro Advisor, but I unfortunately no longer have access to it.)
So the function would open the table, then use AFIELDS() to loop through its fields, then add those fields as objects within an FRX. Rather than creating the FRX from scratch, I would start with a near-blank template report, and add the fields to that.
A simpler approach would be to forget about reports, and simply to produce a text file containing the required data. Again, it would mean using AFIELDS() to get the field names, then scan the table, picking up each value in turn. It would SET ALTERNATE to open a text file, then use the question mark command to send the field names and values to that file.
Then it occurred to me that there was a much simpler approach: one that only needed one line of code. Can you see what I was missing? Take a moment to think about it, and then look at the answer below.
I feel sure that many of you already knew this and you are thinking that I am pretty dim for not thinking of it straight away. It's just another example of how easy it is to forget things in the vastness of Visual FoxPro.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
I came across this recently when I was asked to create a report that would print all the data from any arbitrary table or cursor. The report wouldn't know in advance which table it was to operate on, or what fields the table contained.
This is an example of the required output:
[tt] Product Name: Gustaf's Knäckebröd
English Name: Gustaf's Rye Crisp Bread
Category: BA
Unit size: 24 - 500 g pkgs.
Product Name: Tunnbröd
English Name: Thin Bread
Category: BA
Unit size: 12 - 250 g pkgs.
Product Name: Gumbär Gummibärchen
English Name: Gumbär Gummy Bears
Category: CS
Unit size: 100 - 250 g bags[/tt]
Two possible approaches came to mind.
The first was to actually create an FRX file programmatically. I did this once before, a few years ago, when I needed to create a generic function to print the contents of a grid. (I published that code in FoxPro Advisor, but I unfortunately no longer have access to it.)
So the function would open the table, then use AFIELDS() to loop through its fields, then add those fields as objects within an FRX. Rather than creating the FRX from scratch, I would start with a near-blank template report, and add the fields to that.
A simpler approach would be to forget about reports, and simply to produce a text file containing the required data. Again, it would mean using AFIELDS() to get the field names, then scan the table, picking up each value in turn. It would SET ALTERNATE to open a text file, then use the question mark command to send the field names and values to that file.
Then it occurred to me that there was a much simpler approach: one that only needed one line of code. Can you see what I was missing? Take a moment to think about it, and then look at the answer below.
The trick is to use the FROM clause with CREATE REPORT. Just substitute the appropriate table name in the following command. The FORM clause tells it to output the fields one below another. Use COLUMN if you prefer a columnar report instread.
CREATE REPORT AnyReportName FROM TheTable FORM
Having executed the command, you can preview or print the report in the usual way, then delete the report file.
CREATE REPORT AnyReportName FROM TheTable FORM
Having executed the command, you can preview or print the report in the usual way, then delete the report file.
I feel sure that many of you already knew this and you are thinking that I am pretty dim for not thinking of it straight away. It's just another example of how easy it is to forget things in the vastness of Visual FoxPro.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads