Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a report showing all data in an aribitrary table 7

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,505
Scotland
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.

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.

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
 
Mike said:
how easy it is to forget things in the vastness of Visual FoxPro.

I can distinctly remember that I never knew this neat trick existed! Thanx, Mike.

Steve
 
Is Create Report a distributable feature Mike?

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
That's a good question, Griff. It's something I've never been sure about.

The Help file topic "Distributable and Restricted Visual FoxPro Features and Files" has a list of commands that are NOT available in an EXE, and neither CREATE REPORT nor MODIFY REPORT are among them. It also says, "Although you cannot include commands that create or modify menus, forms, or queries, you can run compiled menu, form, or query programs in your application" - with no mention of reports.

So, on that basis, it looks like the answer to your question is Yes.

Has anyone else got any better information?

It's conceivable that, even if the standard form of CREATE REPORT was not allowed, CREATE REPORT ... FROM ... might be allowed, on the basis that it doesn't actually invoke the report designer. But that's just a guess on my part. I'll try to grab a moment to experiment.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Griff/Mike

i just tried CREATE REPORT in a runtime command window thing ... and yes it worked....

n
 
I compiled it into an exe and ran it on a machine without VFP installed... and it worked there too
Have a star

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Interesting, after months of getting no emails from T-T, they suddenly start working again!

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
That's good news. Thank you both. Stars all round.

This wasn't an issue for me because the utility I wrote was intended as part of a developer tool and so would usually run in the IDE. But it's good to know that the technique would work in an EXE as well.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top