Question : Query

Hi,
PLease see the attached file first. I have a table named "Report" same as teh Report tab and I have another table named data same as Data Tab. I need a query which will be selecting the distinct valuew from Data Col A and will find out the total count of invoices per month wise. This can be done through Pivottable but I need a query for this. Thank you.

Answer : Query

My apologies!  I forgot to give you the code for DCountDistinct :)



Function DCountDistinct(CountCols As String, Tbl As String, Optional Criteria As String = "")
   
    ' Function by Patrick G. Matthews
   
    ' Requires reference to Microsoft DAO library
   
    ' This function provides "domain aggregate"-type functionality similar to
    ' COUNT(DISTINCT ColumnName) in Transact-SQL (i.e., SQL Server's dialect).  The function
    ' can be used in queries, forms, and reports in the Access UI and/or in VBA code once added
    ' to a regular VBA module
   
    ' CountColumns is a comma-delimited list of columns for which we are counting the distinct
    '   values (or combinations of values if 2+ columns are specified).  Place field names in
    '   square brackets if they do not meet the customary rules for naming DB objects
    ' Tbl is the table/query the data are pulled from.  Place table name in square brackets
    '   if they do not meet the customary rules for naming DB objects
    ' Criteria (optional) are the criteria to be applied in the grouping.  Be sure to use And
    '   or Or as needed to build the right logic, and to encase text values in single quotes
    '   and dates in #
   
    ' Just like DCount, if there are no items found matching the criteria, DCountDistinct
    ' returns zero
   
    Dim rs As DAO.Recordset
    Dim SQL As String
   
    ' Enable error handler
   
    On Error GoTo ErrHandler
   
    ' Build query that counts the number of distinct items (or combinations of items, if
    ' CountCols specifies 2+ columns) that meet the specified criteria.  If no criteria are
    ' specified, then count goes against entire table
   
    SQL = "SELECT Count(*) AS Result " & _
        "FROM (" & _
            "SELECT DISTINCT " & CountCols & " " & _
            "FROM " & Tbl & " " & _
            IIf(Criteria <> "", "WHERE " & Criteria, "") & _
        ")"
       
    ' Open recordset with result
   
    Set rs = CurrentDb.OpenRecordset(SQL)
   
    ' Set return value
   
    DCountDistinct = rs!Result
   
    rs.Close
   
    GoTo Cleanup
   
ErrHandler:
    DCountDistinct = CVErr(Err.Number)
   
Cleanup:
    Set rs = Nothing
   
End Function
Random Solutions  
 
programming4us programming4us