简体   繁体   中英

How can I use OpenArgs to print multiple reports in a loop situation?

I am slimming down my databases, eliminating duplicate reports where I can and creating better code. I have one database that involves our welders and foremen. In this code, I can print a report for an individual foreman, which sends the string "strActive" through openargs The report looks at strActive and on the OpenForm action, sets the filter for active, inactive, or all welders, based on the string value passed through.

It works perfectly for the single page at a time code. There, the user chooses a foreman from a list or enters the foreman's clock number. The query the report is based on uses the global string "ForemanCLK" to only get results for that welder.

Formerly, I had three reports; one for all welders, one for active welders, and one for inactive welders.

Previously, I would set a variable based on strActive and open the appropriate report using another variable in the code in place of the report name. The loop worked fine and opened the report 39 times, each time with a new foreman name and data.

I'm baffled as to why opening the report now, with an openarg, doesn't work. I only get the first foreman name, 39 times. I've verified that I get the foremanCLK variable of the different foremen by commenting out the docmd line and debug.print(ing) the various values needed by the form. The report simply doesn't load it correctly.

Set rec = CurrentDb.OpenRecordset(sql)

    rec.MoveFirst

    For ctr = 0 To rec.RecordCount - 1
        ForemanCLK = rec(0).Value
        DoCmd.OpenReport "rptForeman", acViewNormal, , , , strActive
        'DoCmd.OpenReport "rptForeman", acViewNormal
    rec.MoveNext

    Next

The above code gets me many copies of the same foreman's report filtered by strActive

    For ctr = 0 To rec.RecordCount - 1
        ForemanCLK = rec(0).Value
        'DoCmd.OpenReport "rptForeman", acViewNormal, , , , strActive
        DoCmd.OpenReport "rptForeman", acViewNormal
    rec.MoveNext

But this gets me all the different foremen, except it is not filtered.

I've tried passing in a Where clause.

acViewNormal, ,"[active]=" & True

and

acViewNormal, ,"[active]=" & False

and

acViewNormal, ,"[active]=" & True & " OR [active]=" & False

I did the same verification with the single report, and it filters correctly, but in the loop, it does not filter at all. It does, however give me the different foremen.

The big question here is...

WHY? Does access not have enough time between reports to close it and therefore it doesn't perform the operations on the open event?

Any other ideas?

You must close the report explicitly inside your loop: DoCmd.Close acReport, "rptForeman" . If OpenReport is called a second time on an already-open report, the object gets the focus in access, but it doesn't re-run the Open event.

Okay, I must hang my head in shame.

I error checked the hell out of that code above using every variable except strActive.

When I added that to my debug.print line, it came up with nothing. How can that be!?!!??

I simply forgot to set that at the beginning of the sub, like I did with the other action that opens a single report. strActive is a global variable, but was not set at any other point in the code up until this piece.

Once this was added to the beginning of the sub, all worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM