This is an example of what I use in printing data reports. It may not be the best but it works for me. I just call this to print it. It gathers the recordset and prints the report. This example I pass the start and stop date of the query.
Public Sub PRINTTOTALREPORT(mystartdate As Date, myenddate As Date)
On Error GoTo errorhandler
'-- PRINT THE TOTALS REPORT
'-- GET THE REPORT DATA
Dim conTotal As New ADODB.Connection
Dim comTotal As New ADODB.Command
Dim rstTotal As New ADODB.Recordset
With conTotal
.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DATABASENAMEHERE;Data Source=DATABASESERVERNAMEHERE"
.CursorLocation = adUseClient
.Open
End With
With comTotal
.ActiveConnection = conTotal
.CommandType = adCmdText
.CommandText = "SQL STATEMENT HERE"
End With
Set rstTotal = comTotal.Execute
'-- IF NO MATCHES
If rstTotal.RecordCount = 0 Then
MsgBox ("No data was found for your request. No report will be generated."), vbOKOnly, "No data matches"
Exit Sub
End If
'-------------------------------------------------------------------------------
'-- MISC DETAILS SECTION OF THE REPORT
'-------------------------------------------------------------------------------
'-- REPORTS ORIENTATION AND MARGINS
rptDepartmenttotal.LeftMargin = 50
rptDepartmenttotal.RightMargin = 50
'-- ANY LABELS GO HERE
rptDepartmenttotal.Sections(1).Controls("label3").Caption = mystartdate & " - " & myenddate
'-- REPORT'S DATA SOURCE
With rptDepartmenttotal
Set .DataSource = rstTotal
End With
rptDepartmenttotal.Refresh
'-- PRINT THE REPORT
rptDepartmenttotal.Show
Exit Sub
errorhandler:
MsgBox ("The program has generated an error. An error report has been sent."), vbExclamation, "Error"
End Sub