Results 1 to 13 of 13
  1. #1
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23

    select and count record with VB

    Hello need help to find the righjt instructions for this
    From my TABLEA i have all users, birth, sex, study, job sex ecc and date of treatmntes
    Need to select all record that under a range of date i supply supply me the number of all the user according to a filter, that can be kind of job, age range,studies carriers
    And i need to have them separate, Female count and Male count
    I have developed several queries, 2 for each filter, because one is fot the F and another is for M
    Then from a table wher ei have all the queires name i read the queries name and count th number of record
    this
    Code:
    Dim DBCorrente As DAO.Database
    Dim rsRiepilogo As DAO.Recordset
    Set DBCorrente = CurrentDb
    Set rsRiepilogo = DBCorrente.OpenRecordset("Riepilogo", dbOpenDynaset)
    rsRiepilogo.MoveFirst
    Do Until rsRiepilogo.EOF
         rsRiepilogo.Edit
         rsRiepilogo!CountQuery = DCount("*", rsRiepilogo!NameQuery)
         rsRiepilogo.Update
         rsRiepilogo.MoveNext
    Loop
    rsRiepilogo.Close
    DBCorrente.Close
    i would like to have less queries, not to have 2 for each filter for m and f
    and i think i could add an IIF inside the loop to check the Sex fields and according adding to the proper field of the RIEPILOGO table
    SOmething like
    IIf ((Riepilogo.Namequery.SEX) = "m"), TotM = TotM+1, TotF=TotF+1
    instead of the DCOUNT line

    but don t work


    any help?
    thans a lot

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    There's likely a better solution, but I don't know enough about what you're doing to suggest it. To answer your question directly more like:

    TotM = DCount("*", rsRiepilogo!NameQuery, "Sex = 'M'")
    TotF = DCount("*", rsRiepilogo!NameQuery, "Sex = 'F'")

    The more common solution would involve a totals query where you group on the sex field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23
    i canceled this because of a mistake
    Last edited by stefanocps; 01-16-2019 at 11:20 AM. Reason: mistake

  4. #4
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23
    i have error
    it say element not found in this collection
    look like can t find that filed SEX in the query selected from
    rsRiepilogo!NameQuery

    i repeat, NameQuery is a variabile, queroies name are in RIEPILOGO

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Is the field Sex in every query? Based on the reply you deleted, I'd try a totals query that grouped on the two fields, the sex field and the filter field (study, job, etc). Can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23
    sex is in every query, is the main filter, after the date one
    sure i attach, yiou might find some difficulties as it is in italian, and even the code i wrote i made some variation at the name for better understanding
    if oyu open RICERCA mask associated to the button there is the code
    The 2 main tables are ANAGRAFICA and date_treattamenti
    http://stefanocapasso.net/ambulatorio good.accdb

  7. #7
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23
    also in RIEPILOGO now there are only few queries
    because i was doing tens of queries with count included...too much job for the final report

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    I was thinking of this type of thing, adapted to use the date query which doesn't work for me (does Maschere mean "Forms", making that a form reference?).

    SELECT anagrafica.[TITOLO DI STUDIO], anagrafica.SESSO, Count(anagrafica.Id) AS CountOfId
    FROM anagrafica
    GROUP BY anagrafica.[TITOLO DI STUDIO], anagrafica.SESSO;
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23
    dont understand you
    Maschere meand Forms yes
    the date is essentials
    Every 3 months we need to collect all thoise infoHow mani Female with xx study degree
    how many male with xx study degress
    how many female with yyy study degree, how many male with yyy study degree
    and so on
    then the same with job
    then the same with age range..and more thingsif you open MASCHERE RICERCHE
    everything should start form there (leave fornow the submask and the search filed, just consider the 2 date fileds and the button
    Majority of the query have already the count in it...but you see how many? not even the half and so many
    That s why somehow want to avoid the double query F and M
    if you receive input mask they are date from date to
    dd/mm/yyyy

  10. #10
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23
    the code you suggested me at the beginning seemed a good path...

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850

  12. #12
    stefanocps is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    23
    Quote Originally Posted by orange View Post
    do i need to do something about?

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    stefanocps,
    When you post the same request on more than one forum, it is considered Best Practice / Good Etiquette to
    advise the readers that you have done so, and place a link(s) to the other forum(s) within your post.

    This is the rationale for cross posting in this manner.

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

Similar Threads

  1. Replies: 4
    Last Post: 01-15-2019, 11:50 AM
  2. Replies: 5
    Last Post: 02-24-2017, 08:09 AM
  3. In Select Having Count >1?
    By aellistechsupport in forum SQL Server
    Replies: 21
    Last Post: 08-01-2016, 07:39 PM
  4. Replies: 3
    Last Post: 08-03-2012, 02:37 AM
  5. Select, Count, Where (Like) HELP!
    By nols76 in forum Queries
    Replies: 3
    Last Post: 08-24-2011, 11:36 AM

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