As with forms, you can use the DoCmd to call your report within VBA. Let’s take a look at the code that follows.
Sub runReport() Dim con As ADODB.Connection Set con = New ADODB.Connection con.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\BegVBA\thecornerbookstore.mdb;" DoCmd.OpenReport "rptCustomer", acViewPreview End Sub
We have one variation from the form here. If you enter the line with just
DoCmd.OpenReport "rptCustomer"
Access will assume you want to send the report directly to the printer. In order to prevent that from happening, for the time being, you need to add a second parameter of acViewPreview.
Of course, as in previous chapters, you could include the DoCmd within a decision structure to decide which report to run. In addition, you can include it as part of the Switchboard form created in Chapter 12.