By Dan Macleod
Has this happened to you? You preview a report in Visual FoxPro. Everything looks fine. You then print the report by clicking the Print button in the preview window's toolbar. When you do, the data disappears - the printed version of the report is empty.
In theory, that should never happen. The preview and the actual report should always contain the same data. That's the whole point of a preview, after all. But the exact behavior I just described does sometimes occur - and for an unexpected reason.
I've also heard of cases where the problem occurs entirely within the preview window. The first page of the preview shows up correctly, but subsequently pages don't.
In all these cases, the culprit is the FoxPro grid control. Specifically, if you launch the preview while a grid has focus, the grid changes the current work area to match its record source. For reasons that aren't completely clear, this doesn't immediately affect the preview window, but it does affect the selected alias as seen by the printed report. If the new work area is not the one the report is expecting, no data will appear in the printout.
Now you know what causes this behavior, the solution is fairly obvious: Take focus off the grid immediately before launching the report. You can do that by programmatically setting focus to another control (using that control's SetFocus method). Or you can temporarily disable the grid before running the report, and re-enable it afterwards.
I'm not sure whether this behavior is a bug or a design feature. I can sort of understand why the grid grabs the work area in the way that it does - that's the way the grid works. But I do know that this is not officially documented anywhere. So don't blame yourself if this is the first you've heard of it.