VBA - Pass value from form to report for group/sorting options (1 Viewer)

Nyanko

Registered User.
Local time
Today, 13:36
Joined
Apr 21, 2005
Messages
57
Hi,

I'm using a form to return searched values in a subform and then have the ability to generate a report.

Adapted from this awesome code : http://www.access-programmers.co.uk/forums/showthread.php?t=99353

I have this working perfectly, however I would like to give my users the ability to group and sort the report. To generate the report I'm using the below code :

Code:
Private Sub btn_Report_Click()
    Dim stDocName As String
    
    'Send the current selection to a report
        If BuildFilter = "" Then
        Me.frm_CommissionSearch.Form.RecordSource = "SELECT * FROM Qry_CommissionSearch " & BuildFilter
    Else
        Me.frm_CommissionSearch.Form.RecordSource = "SELECT * FROM Qry_CommissionSearch WHERE " & BuildFilter
    End If

    stDocName = "Rpt_CommissionSearch"
    DoCmd.OpenReport stDocName, acPreview, WhereCondition:=BuildFilter
End Sub

and I have seen on this site how to group a report on the fly with vba :
http://www.techrepublic.com/article/how-to-group-an-access-report-on-the-fly/
Code:
Private Sub Report_Open(Cancel As Integer)
    Me.GroupLevel(0).ControlSource = Forms!criteria
End Sub

My question is how do I phrase the DoCmd to pass the criteria to the report ?
 

cyanidem

Nieóhfytny jaszczomp
Local time
Today, 13:36
Joined
Nov 30, 2015
Messages
106
Use OpenArgs parameter to pass criteria string on DoCmd.OpenReport:
Code:
DoCmd.OpenReport stDocName, acPreview, WhereCondition:=BuildFilter OpenArgs:="something you want to pass"
and then read it in Report_Open event sub:
Code:
Dim CriteriaString as txt
CriteriaString=Me.OpenArgs
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:36
Joined
May 7, 2009
Messages
19,246
pass the grouping on the seventh argument of the docmd.openreport (OpenArgs). then on the report's load event you evaluate the passed argument:

Private Sub Report_Load()
Me.GroupLevel(0).ControlSource = Me.OpenArgs
End Sub
 

Users who are viewing this thread

Top Bottom