mercia
asked on
Printing Access Report from VB?
Can anyone show the the correct way of attaching to a database and then printing one of its reports from within VB 6?
Const dbPathName = "c:\database.mdb"
Const strPassword = "PASSWORD"
Dim Acc As Object, db As Object
Set Acc = CreateObject("Access.Appli cation")
Set db = Acc.Application.DBEngine.W orkspaces( 0).OpenDat abase(dbPa thName, 0, False, ";pwd=" & strPassword)
Acc.OpenCurrentDatabase dbPathName, False
Set db = Nothing
Acc.DoCmd.OpenReport "ReportName", acPreview
Set Acc = Nothing
Const strPassword = "PASSWORD"
Dim Acc As Object, db As Object
Set Acc = CreateObject("Access.Appli
Set db = Acc.Application.DBEngine.W
Acc.OpenCurrentDatabase dbPathName, False
Set db = Nothing
Acc.DoCmd.OpenReport "ReportName", acPreview
Set Acc = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for this quick solution, it works well I would like the report to be displayed on screen instead of having to toggle to access, any clues??
how would call a access macro to run in vb please and send a recordsource to it?
You just call the RunMacro method of the Access Application object's DoCmd class:
Dim appAcc As Access.Application
Set appAcc = New Access.Application
With mappAcc
.OpenCurrentDatabase "DatabasePathAndName.mdb"
.DoCmd.RunMacr "myMacroName"
End With
The only optional arguments it supports have to do with how many times you want to repeat the call to the macro. You can't specify a recordsource for a macro - there just isn't any context to do so.
But your macro could run lots of queries for you that BUILD the recordsource you want. Or you could run the queries yourself from the VB side of your application and then dump the contents of the recordset you have into a table. The macro could invoke reports or other objects to use the contents of the table.
Dim appAcc As Access.Application
Set appAcc = New Access.Application
With mappAcc
.OpenCurrentDatabase "DatabasePathAndName.mdb"
.DoCmd.RunMacr "myMacroName"
End With
The only optional arguments it supports have to do with how many times you want to repeat the call to the macro. You can't specify a recordsource for a macro - there just isn't any context to do so.
But your macro could run lots of queries for you that BUILD the recordsource you want. Or you could run the queries yourself from the VB side of your application and then dump the contents of the recordset you have into a table. The macro could invoke reports or other objects to use the contents of the table.
then add a DataReport form, then u can design you form there.