[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference bump::msaccess

Title:MSACCESS
Moderator:BUMP::HONER
Created:Tue Dec 01 1992
Last Modified:Mon Jun 02 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1661
Total number of notes:6339

1626.0. "Count of a Count in MS Access" by GYMAC::DCASSIDY () Mon Feb 03 1997 11:36

    Is it possibel do write the following SQL code in Access Basic.
    
    SELECT Customer_Number,COUNT(DISTINCT Order_number) 
    	FROM Orders_Table
    	GROUP BY Customer_Number;
    
    I have tried various formats in Access but to no avail.
    
    Thanks in advance,
    
    Desmond. 
    		
T.RTitleUserPersonal
Name
DateLines
1626.1ExampleNSIC00::KLERKThunderbirds are GoMon Feb 03 1997 13:2438
  You can use any SQL code (as long as Access Jet understands it):

  dim rst as Recordset
  dim db as Database
  dim strSQL as string
  dim qdf as QueryDef
  
  set db = CurrentDB()
  strSQL = "SELECT *...."
  set rst = db.OpenRecordSet(strSQL, dbOpenDynaset)  ' or dbOpenSnapshot
  if rst.RecordCount > 0 then
  do until rst.EOF
    rst.MoveFirst
     ... handle each record
    rst.MoveNext
  loop


  If you're intested in action queries that update tables but not necessarily
  returns records, you could use:

  strSQL = "DELETE
  set qdf = db.CreateQueryDef("")   ' this is a temporary query
  qdf.SQL = strSQL
  qdf.ReturnsRecords = False        ' or true if records are returned
  qdf.Execute

  If the query does return records, you can access these one by one too:

  rst = qdf.OpenRecordSet (dbOpenTable) ' or dbOpenDynaset or dbOpenSnapshot


  Theo

  
  
  
1626.2And as far as the query itself goes...NSIC00::KLERKThunderbirds are GoMon Feb 03 1997 13:3011
 ...and if you were worried about the SQL statement itself, using the
 Orders sample database of MS Access does what you want with the SQL
 statement:
 
 SELECT DISTINCTROW Orders.CustomerID, Count(Orders.OrderID) AS CountOfOrderID
 FROM Customers
 INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
 GROUP BY Orders.CustomerID;

 Theo