Question : How do I parameterise a Union query so that a prompt it not issued each time it runs...

I have a union query which combines two very similar queries. The underlying queries in turn rely on subqueries to run. Several of the Subqueries rely on a [MTH] parameter. Now if I run the parent union query from the Access interface I get prompted for the [MTH} parameter (fair enough), but the query should run automatically with the [MTH] value being picked up from a user form.

Normally you could add a WHERE clause if it was a simple query, such as MyMonth = Forms!frmCostReport!cboMonth, and that would resolve accordingly. But with a UNION query you do not get access to the Querybuilder window. Furthermore the [MTH} parameter cannot be applied directly to any field in the parent query but is required in subqueries much further down.

So I want a way of providing the [MTH] parameter to the parent query (the union query) via the form field cboMonth such that I dont get prompted. I can accept defining the query in code (i.e. defining a querydef dynamically) or running an existing query, but the prompt must go!

The attached is my first attempt which still issues a prompt for the month value....

Tony
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
strSQL = "PARAMETERS [MTH] Integer;" & _
        "SELECT [Cost Sector ID], [Cost Function ID], ProgrammeID, ProgrammeName as [Programme Name], " & _
            "SubProgrammeName as [Sub-Programme Name], ProjectName as [Project Name], " & _
            "CostTypeName as [Cost Type Name], [Manpower Direct Cost Flag], " & _
            "BAF_JAN, BUS_JAN, BAF_FEB, BUS_FEB, BAF_MAR, BUS_MAR, BAF_APR, BUS_APR, BAF_MAY, BUS_MAY, " & _
            "BAF_JUN, BUS_JUN, BAF_JUL, BUS_JUL, BAF_AUG, BUS_AUG, BAF_SEP, BUS_SEP, BAF_OCT, BUS_OCT, " & _
            "BAF_NOV, BUS_NOV, BAF_DEC, BUS_DEC, BAF_TOT, BUS_TOT " & _
        "FROM q_s_CostReport_BAFTA " & _
    "UNION " & _
        "SELECT [Cost Sector ID], [Cost Function ID], ProgrammeID, ProgrammeName as [Programme Name], " & _
            "SubProgrammeName as [Sub-Programme Name], ProjectName as [Project Name], " & _
            "CostTypeName as [Cost Type Name], [Manpower Direct Cost Flag], " & _
            "BAF_JAN, BUS_JAN, BAF_FEB, BUS_FEB, BAF_MAR, BUS_MAR, BAF_APR, BUS_APR, BAF_MAY, BUS_MAY, " & _
            "BAF_JUN, BUS_JUN, BAF_JUL, BUS_JUL, BAF_AUG, BUS_AUG, BAF_SEP, BUS_SEP, BAF_OCT, BUS_OCT, " & _
            "BAF_NOV, BUS_NOV, BAF_DEC, BUS_DEC, BAF_TOT, BUS_TOT " & _
        "FROM q_s_CostReport_HRI;"
    
    Set qDef = dbs.CreateQueryDef("q_CostSummary", strSQL)
    qDef.Parameters("MTH") = cboMonth

Answer : How do I parameterise a Union query so that a prompt it not issued each time it runs...

When you are running a TransferSpreadsheet it will only work with a Full Forms Reference. The Parameters in the UNION will not allow you to set the parameter like in some languages.

Once you set the parameter you can open a recordset or execute the querydef but these do not help with the Transfer.

Set rst = qd.OpenRecordset
or
qd.Execute

Cheers, Andrew
Random Solutions  
 
programming4us programming4us