Results 1 to 4 of 4
  1. #1
    greyscale is offline Novice
    Windows XP Access 97
    Join Date
    Aug 2010
    Posts
    5

    Query that returns number of times an item appears alongside others in the row

    Hi,



    I have a strange request. We recently asked phone staff to press a radio button on an access form (access97) each time they received a certain type of enquiry (100 enquiry types). They were able to press multiple enquiry types for each call.

    We now need to report on the number of times that enquiry was selected as a single enquiry or as part of other enquiries

    How do I write a query that would return the number of times a field appears on its own, as part of a double query, triple query etc. We need to account for the fact that up to 6 enquiries can be included on the one call/row. I have attached a small sample of the database, replacing sensitive data with generic field names, and an example of the report layout.

    Thanks in advance
    Last edited by greyscale; 08-22-2010 at 11:45 PM. Reason: Did not accept the paste of the report layout in body of text

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    your data needs to be normalized. 104 fields is not normal really. plus, this would done MUCH easier in excel with the setup that you currently have. I would first recommend moving it there because doing any caluclations horizontally like this in access is next to impossible.

    however, if you feel like using arrays you probably can. this might be the tough way to do it, but I did an experiment with your data and I came up with this query:

    Code:
    SELECT FIELDLIST.[FIELDNAME], 
    
    GETQRYNUM([FIELDNAME],"SINGLES") AS SINGLES, 
    
    GETQRYNUM([FIELDNAME],"DOUBLES") AS DOUBLES, 
    
    GETQRYNUM([FIELDNAME],"TRIPLES") AS TRIPLES, 
    
    GETQRYNUM([FIELDNAME],"QUADS") AS QUADS, 
    
    GETQRYNUM([FIELDNAME],"QUINS") AS QUINS, 
    
    GETQRYNUM([FIELDNAME],"SEXTUPLES") AS SEXTUPLES
    
    FROM FIELDLIST;
    I did this on a table that I manually created and populated with automation (one per rec). Then I took your imported excel data in an access table called TESTDATA and called this function from the query's SQL:

    Code:
    Option Compare Database
    Option Explicit
    Option Base 1
    
    Public Function GETQRYNUM(FIELDNAME As String, VARTYPE As String) As Long
    
    Dim CTR As Long 'VAR TO LOOP THROUGH FIELD INDEX
    Dim QRYCOUNT As Long 'NUMBER OF QUERIES MADE IN A RECORD
    Dim QRYMATCH As Boolean
    Dim FLDINDEX As Long 'COLUMN INDEX COMES FROM DIGIT IN FIELDNAME
    Dim DB As DAO.Database
    Dim RS As DAO.Recordset 'TABLE
    Dim QUERYTYPES(6) As String 'ARRAY FOR MATCHING QUERY TYPES (SINGLE, DOUBLE, TRIPLE, ETC...)
    
    QUERYTYPES(1) = "SINGLES"
    QUERYTYPES(2) = "DOUBLES"
    QUERYTYPES(3) = "TRIPLES"
    QUERYTYPES(4) = "QUADS"
    QUERYTYPES(5) = "QUINS"
    QUERYTYPES(6) = "SEXTUPLES"
    
    GETQRYNUM = 0 'THE COUNT IS 0 TO START
    
    Set DB = CurrentDb
    Set RS = DB.OpenRecordset("TESTDATA", dbOpenDynaset)
    
        With RS
        
            'OPEN THE ENTIRE TABLE
            .MoveLast
            .MoveFirst
            
                Do Until .EOF 'LOOP THE RECORDS
                
                    QRYCOUNT = 0
                    QRYMATCH = False
                    FLDINDEX = CLng(Right(FIELDNAME, Len(FIELDNAME) - InStr(FIELDNAME, " ")))
                    
                        If .FIELDS(FLDINDEX).Value = "-1" Then 'CHECK FOR CRITERIA SATISFIED
                            
                            For CTR = 4 To 104 'COUNT QUERIES IN THE CALL HERE
                                If .FIELDS(CTR).Value = "-1" Then
                                    QRYCOUNT = QRYCOUNT + 1
                                End If
                            Next CTR
                        
                        End If
                    
                        If QRYCOUNT > 0 Then
                            If QUERYTYPES(QRYCOUNT) = VARTYPE Then
                                QRYMATCH = True 'TYPE OF QUERY WE'RE ASKING FOR MATCHES NUMBER FOUND IN REC
                                GETQRYNUM = GETQRYNUM + 1
                            End If
                        End If
                        
                            .MoveNext
                            
                    Loop
                    
                .Close
                
            End With
    
    Set RS = Nothing
    Set DB = Nothing
      
    End Function
    Here is the result I saw (which takes way too long to load because massive calculations have to run before display):



    Certainly there is a better way for you to do this. Your criteria fields in a seperate table would be a wonderful place for you to start. That is how Access is meant to provide benefits over Excel. Without separating data like this you might as well just stick with the spreadsheet.

  3. #3
    greyscale is offline Novice
    Windows XP Access 97
    Join Date
    Aug 2010
    Posts
    5
    Adam, thank you so much for investigating this so thoroughly for me. While it seems it can be done, i will take your advice and split the criteria fields into separate tables and see how we get on with that.

    Thanks again. I would never have got there on my own

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    No problem. It was a good question to ask. Interesting one too. Good luck with it.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Last matched item in an unmatching query
    By kabaa01 in forum Queries
    Replies: 0
    Last Post: 05-19-2010, 03:46 PM
  2. Aggregate Query Returns No Values
    By Xiaoding in forum Queries
    Replies: 6
    Last Post: 03-29-2010, 02:01 PM
  3. Query: How many times does a value exist?
    By hognabbt in forum Queries
    Replies: 1
    Last Post: 01-10-2010, 01:33 PM
  4. Replies: 0
    Last Post: 08-26-2009, 11:51 AM
  5. Replies: 3
    Last Post: 02-20-2009, 02:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums