tags:

views:

77

answers:

1

Hi all,

I have a make table query that is used to run a report (due to a series of joins and various other complicated criteria - it has to be a table or it fails with too many queries).

I now need to be able to filter the data used (it is summing fields) using an 'IN' with selections from a list box.

Here is the query I want, with [EWAList] being the selected items in the list box, can anyone help?

SELECT C.Num, P.Name, E.Phase, C.Code, 
       Sum(C.Hours) AS Hours, Sum(C.Total) AS Total, 
       Sum(TotalForC.Total) AS ETotal, E.E_Date     
INTO CData
FROM (P INNER JOIN 
     (C INNER JOIN E ON C.EId=E.EId) 
                     ON P.Number=E.Num) 
        INNER JOIN TotalForC ON E.EId=TotalForC.EId    
WHERE (((C.DateCreated)>=[mydate])) 
And E.E_Date<>'0' 
And E.E_Date<>'' 
AND E.E_Number IN ([EWAList])    
GROUP BY C.Num, P.Name, E.Phase, C.Code, E.E_Date;

Any help is appreciated! I'm out of ideas at this point

Thanks for the tip on the line breaks :-)

Thanks! Christy

+2  A: 

You will need to use VBA to build the query to get the IN list. For example:

Dim qdf As QueryDef

'' ListBox is the name of your listbox, the item you are looking for is
'' in column 0 (first column) and it is numeric

For Each itm In Me.ListBox.ItemsSelected
  EWAList = EWAList & "," & Me.ListBox.Column(0, itm)
Next

strSQL = "SELECT C.Num, P.Name, E.Phase, C.Code, " _
   & "Sum(C.Hours) AS Hours, Sum(C.Total) AS Total, " _
   & "Sum(TotalForC.Total) AS ETotal, E.E_Date " _
   & "INTO CData " _
   & "FROM (P INNER JOIN " _
   & "(C INNER JOIN E ON C.EId=E.EId) " _
   & "ON P.Number=E.Num) " _
   & "INNER JOIN TotalForC ON E.EId=TotalForC.EId " _
   & "WHERE (((C.DateCreated)>=[mydate])) " _
   & "And E.E_Date<>'0' " _
   & "And E.E_Date<>'' " _
   & "AND E.E_Number IN (" & Mid(EWAList,2) & ") " _
   & "GROUP BY C.Num, P.Name, E.Phase, C.Code, E.E_Date"

Set qdf = CurrentDB.QueryDefs("NameOfAnExistingQuery")
'' Permanentlt overwrite the sql of the query
qdf.SQL = strSQL
Remou
I can build the list with vba - what I can't figure out is how to get the query to use an 'IN' as a parameterIf you try to run the query above and you enter a list of values it will not return any rows, but if you type in the list of values instead of using a parameter, it returns everything as expected....
Christy
I do not think you can use a parameter in this case, just a text string for the sql. I do not think that SQL Injection will be a problem with Access in this case, because the data for IN is predefined.
Remou
I probably didn't explain very well above :-) I have a form where the user chooses a date, then clicks continue. Once they click continue, I want to show them the list box, have them select the items they want to include and click run report. Then my vba code runs the make table query (above) and opens the report based on a query that uses the created table. Does that make sense?
Christy
I have added some notes. They are not tested, so I hope they are right. You should not really be using make table queries in a finished application, it will lead to bload, and is probably not necessary with a little rearrangement.
Remou
While I agree that Make Tables are not a good idea in a production app, the bloat issue is not that big a deal, as the correct approach is a persistent temp table where records will be appended and deleted, so there's going to be bloat either way. The key is that temp tables shouldn't be in your front-end application, but in a separate temp data file that is discardable/compactable when need be.
David-W-Fenton