accpert.com
accpert.com

Reputation: 129

How to open out of python a ms access report with parameters

so far i have this coding which works, only the where clause is not working. I can pass in the where clause whatever i want, it is not considered:

import win32com.client

a = win32com.client.Dispatch("Access.Application")
path = r'C:\Users\Egon\Documents\Kassenbuch_py\Pro Version\kassenbuch.accdb'
filename =r'C:\Users\Egon\Documents\Kassenbuch_py\Pro Version\pdf\kassenbuch.pdf'
# db = a.CloseCurrentDatabase()
db = a.OpenCurrentDatabase(path)
ReportName = 'kassenbuch'
try:
                

    StartDate = '#2021/06/13#'
    EndDate = '#2021/06/14#'

    acView = 'acViewPreview'
    where_cl = '[DATUM] BETWEEN ' + StartDate + ' AND ' + EndDate
    # where_cl = '"[EINNAHME] = 200"'

    a.DoCmd.OpenReport(ReportName, 2, where_cl ,3)
    # a.DoCmd.OpenReport(ReportName, 2, where_cl ,3)
    a.DoCmd.Save(3, ReportName)
    # a.visible = 1

    a.DoCmd.OutputTo(3, ReportName, r'PDF Format (*.pdf)', filename)

except Exception as e:
    
    QMessageBox.warning(self, 'Fehler', str(e), QMessageBox.Ok)

a.DoCmd.CloseDatabase
a.Quit()
a=None

Upvotes: 1

Views: 155

Answers (1)

Erik A
Erik A

Reputation: 32672

Well, that's because you're passing a WHERE condition as the third argument, the filtername. Review the docs.

If you pass an empty string there and pass your WHERE condition as the WHERE condition , it'll work:

a.DoCmd.OpenReport(ReportName, 2, '', where_cl ,3)

Note that the DoCmd.Save should not really be there, it does nothing since the design of the report wasn't changed.

Upvotes: 2

Related Questions