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.

Apparently this applies to the creation of PDF file using PrintToPDF as well. I had tested it a few days ago and it seemed to work, but have now discovered that only parts of the report are reproduced.

Currently I’m exploring for workarounds such as found for closing secret files. So far an inserted Save isn’t helpful - it causes variable errors.

This is the last remaining step in my weeks long quest to run a timer on a server to create PDF reports for clients to download.

Some additional info…
As I wrote, before comitting a lot of time to it, I did test printing from a secret database a few days ago and it worked. I created a form with some graphic objects, a date formula and a variable and it reliably saved the PDF with all elements present.

OpenSecret dbpath()+"printtest"
SetActiveDatabase "printtest"
LetFileGlobal fgText = "XXXXX"
printtopdf "printtest.pdf","Database","printtest","Form","MyForm"
SetActiveDatabase "printtest"
Save
CloseDatabase "printtest"

I tried it again this morning and it worked - until I added a lookup to the form. Then it went fully haywire. After deleting the lookup, it continued to fail.

I created another file with similar form and graphic objects and a date formula. It failed in the same way. Quit Panorama and tried again, the forms sans lookups worked again Adding data from fields in the database does work.

Still experimenting…

Yes, I would expect it to behave the same way, the same code is involved.

With the timer, secret windows to this, it’s been a very long road to a dead end. :unamused: