How to Pass this parameter to this type of query - I'm struggling

Lateral

Registered User.
Local time
Yesterday, 17:41
Joined
Aug 28, 2013
Messages
388
Hi guys

I have the following query that displays the selected data as a Pivotchart.

DoCmd.OpenQuery "qPaymentsQueryJustMonth", acViewPivotChart

I action the about via an VBA Event.

1625707472877.png


This all works fine

The graph displays the "Sum of Payments Amount" on a monthly graph just for all the months of January for every year.

I want to use the same query but pass the month number (eg: 2,3,4,5,6,7,8,9,10,11,12)

I have tried to figure this out but can't.

Dim sWhere As String

sWhere = "[PaymentDate] = "DatePart("m",[PaymentDate]) = 1"


DoCmd.OpenQuery "qPaymentsQueryJustMonth", acViewPivotChart


Any help would be greatly appreciated.

Cheers
Greg
 
change the last Column:

Field: DatePart("m", [PaymentDate])
Total: Expression
Criteria: [p1]

''''''''''
now, before opening your query, supply the value of [p1]:

Code:
With DoCmd
    .SetParameter "p1", 1    'change 1 with the month number
    .OpenQuery "qPaymentsQueryJustMonth", acViewPivotChart
End With
 
Thanks for the quick reply but I'm getting the following error when I compile:

1625710830096.png



Also, I'm using Access 2007.
 
Last edited:
oopps, sorry i am using higher version.
can you copy (and paste) this in a Module:

Code:
Public Function fnQueryParameter(ByVal strQuery As String, ByVal strParam As String, ByVal varParamValue As Variant)
    Dim db As DAO.Database
    Dim qd As DAO.QueryDef
    Dim strSQL As String
    Set db = CurrentDb
    On Error Resume Next
    Set qd = db.QueryDefs("zt_" & strQuery)
    If Err Then
        Err.Clear
        DoCmd.CopyObject , "zt_" & strQuery, acQuery, strQuery
        db.QueryDefs.Refresh
        Application.RefreshDatabaseWindow
    End If
    qd.Close
    Set qd = Nothing
    strSQL = db.QueryDefs("zt_" & strQuery).SQL
    If InStr(1, strSQL, "[" & strParam & "]") <> 0 Then
        strSQL = Replace$(strSQL, "[" & strParam & "]", varParamValue)
    Else
        strSQL = Replace$(strSQL, strParam, varParamValue)
    End If
    Set qd = db.QueryDefs(strQuery)
    qd.SQL = strSQL
    qd.Close
    Set qd = Nothing
    Set db = Nothing
End Function

your program would now look like this:
Code:
    Call fnQueryParameter("qPaymentsQueryJustMonth", "p1", 1)    'change 1 with the month number
    DoCmd.OpenQuery "qPaymentsQueryJustMonth", acViewPivotChart
 
oopps, sorry i am using higher version.
can you copy (and paste) this in a Module:

Code:
Public Function fnQueryParameter(ByVal strQuery As String, ByVal strParam As String, ByVal varParamValue As Variant)
    Dim db As DAO.Database
    Dim qd As DAO.QueryDef
    Dim strSQL As String
    Set db = CurrentDb
    On Error Resume Next
    Set qd = db.QueryDefs("zt_" & strQuery)
    If Err Then
        Err.Clear
        DoCmd.CopyObject , "zt_" & strQuery, acQuery, strQuery
        db.QueryDefs.Refresh
        Application.RefreshDatabaseWindow
    End If
    qd.Close
    Set qd = Nothing
    strSQL = db.QueryDefs("zt_" & strQuery).SQL
    If InStr(1, strSQL, "[" & strParam & "]") <> 0 Then
        strSQL = Replace$(strSQL, "[" & strParam & "]", varParamValue)
    Else
        strSQL = Replace$(strSQL, strParam, varParamValue)
    End If
    Set qd = db.QueryDefs(strQuery)
    qd.SQL = strSQL
    qd.Close
    Set qd = Nothing
    Set db = Nothing
End Function

your program would now look like this:
Code:
    Call fnQueryParameter("qPaymentsQueryJustMonth", "p1", 1)    'change 1 with the month number
    DoCmd.OpenQuery "qPaymentsQueryJustMonth", acViewPivotChart


Thanks mate!

It works great!

I assume that SetParamter is not available in Access 2007 and this is a module that provides exactly the same functionality.

Thanks again!

Cheers
Greg
 
@arnelgp
Now I am curious.? I have seen you post many times code like

P1= VariableName
P2 = VariableName

then run the query with those parameters?

Why did you not use that method this time please?
 
There is no .SetParameter method on DoCmd object in a2007
 
The op want to Run the query (pivot). It is a select query and not an action query.
 

Users who are viewing this thread

Back
Top Bottom