April 5, 2012

VFP reports not showing the same data as the preview

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.

10 comments:

  1. Dan, thanks for that.

    I've been having this problem with a report since last year. The report was working fine for some time before and I had not made any changes to it then I started having the problems you have described. I've moved the focus away from the grid and all is working now.

    Problem is what has caused the problem. VFP or Windows? If I get any further will let you know.

    Thanks again

    Roy Hendry

    ReplyDelete
  2. Hi Roy,

    Glad to hear my post was useful. I don't know for sure, but I'd guess the problem is caused by VFP rather than Windows. Let us know if you find out anything more. I'll do the same.

    Dan

    ReplyDelete
  3. thanks man, i have same problem. i will try as your advice

    ReplyDelete
  4. I am mark Gyto :

    I have the same problem but I already found the problem. The problem is that my VFP report need at least one table to be opened (Data environment) and the table must not empty. At least contain one record. No matter the table content has something to do with your report or not.

    ReplyDelete
  5. Found this fix many years ago while debugging the problem:
    KEYBOARD '{ESC}'
    BROWSE
    REPORT ....PREVIEW
    user never sees browse window
    never knew the real cause

    ReplyDelete
  6. Fixed mine! Thanks for the advice @Roy Hendry
    I really figure it out that the cause is the grid has been focused.

    *! The only way to fix is to put away the focus of any grid object (if any) before to set report record source
    *! It will affect the target record source of the report
    this.cmdGridAwayFocus.Visible = .T.
    this.cmdGridAwayFocus.SetFocus()

    *! Report Data Records
    SELECT * FROM DTRHistoryRecords INTO CURSOR ReportData
    GO TOP IN 'ReportData'
    SELECT ReportData && report target Record Source

    REPORT FORM "DTR Report" TO PRINTER PROMPT NODIALOG PREVIEW

    ReplyDelete
  7. Dear Dan,

    Thank you for this article! Have been suffering a one off bug for many years, finally thanks to you I found the culprit. Had a Grid and a button to save and invoice. The Save could also be triggered off with a keypress, which would have left focus on the Grid. Then when printing the invoice, the result Visual Foxpro Report was showing adhoc items and not the ones I had in the report cursor.

    Thanks again for that!

    ReplyDelete
  8. Printing or previewing Report Form results in incorrect data
    https://support.microsoft.com/en-us/kb/185628

    ReplyDelete
  9. Does this also happen when the focus is on a list? to print the user clicks a button and I ask a question using the messagebox command, so theoretically the focus is on that messagebox and even then PRINTER PROMPT PREVIEW is happening as soon as you exit the empty CURSOR.
    cret= MESSAGEBOX(" Deseja imprimir comprovante de venda ? ",292, "Aviso do Sistema ")
    thisform.combo1.SetFocus && I added after reading the post
    IF cret=6
    SELECT c_v && added now
    REPORT FORM relcupomsimples.frx TO PRINTER PROMPT PREVIEW
    endif
    thisform.list1.Clear

    ReplyDelete
  10. awesome. thanks Dan.
    my client just reported this disturbing behaviour yesterday, after trying to find the problem for a few hours, i gave up, an found your blog. �� suksma ��

    ReplyDelete