Calling printtopdf from another database problems

I have a procedure, say MyPrintProcedure, in Database A that selects records, groups, and totals some fields, then printtopdf makes a file using a report form.

But if I have a procedure in Database B that changes the active database then calls the procedure, the printtopdf statement malfunctions (at least it doesn’t do what I want). The code in Database B is,

setactivedatabase "A"
call "MyPrintProcedure"

The result is that the selecting, grouping, and totaling work correctly. But the resulting pdf file is very different. It looks like the active database switched back to Database B and printed the report using the data from Database B and the form from Database A. Using zlog statements confirmed that the active database immediately before the printtopdf statement was database A, and immediately after was Database B. Based on the data in the report, the switch occurred before the report was created. I could not find the code in the libraries for printtopdf to see what was happening.

It seems to me that what I tried to do should work, but perhaps there are reasons why that is not a good idea.

I haven’t analyzed all the aspects of what you’ve posted. I’ll suggest though that you take a look at PrintUsingForm since it allows you to specify the database and the form to be used. You may find that it’s a good alternative.

Sorry, printing does not work with “secret” windows. It always prints using the currently visible database. Unfortunately the documentation doesn’t make it clear that the definition of “current database” used by this statement is different than in most other situations.

Jim Cook’s suggestion will not help, because the print statement has the same issue – it always prints the database that is currently visibly active.

Looking at the source code for printing, I don’t think this can be changed, or if it can be changed, it will require an amount of work that would be much better allocated to other projects.