counting entries with update query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pezz
    New Member
    • Feb 2007
    • 1

    counting entries with update query

    Helly my Experts,
    i am working with access 2000 on windows 2000. I have a database with many tables. I would like to count the entries in one of this tables and show the totals in a field in the main tables.
    I try to be more practical:
    i am an epidemiologist and i have different labs (main table) each with different patients (second table). In the first table i would like access to create a new field and in thius field show me how many patients i have per lab. Was i sufficiently clear? Thank you very much.
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Originally posted by pezz
    Helly my Experts,
    i am working with access 2000 on windows 2000. I have a database with many tables. I would like to count the entries in one of this tables and show the totals in a field in the main tables.
    I try to be more practical:
    i am an epidemiologist and i have different labs (main table) each with different patients (second table). In the first table i would like access to create a new field and in thius field show me how many patients i have per lab. Was i sufficiently clear? Thank you very much.
    In the second patient table have you got a foreign key to the primary key of the first lab table e.g. LabID

    Assuming you do you could try setting the default value of the PatientCount field in the patient table to the following:

    Code:
    DCount("[PatientID]", "tblPatients", "[LabID]=" & [LabID])
    I'm not sure if this will work but it's worth a try. I had to guess at the field and table names so substitute your own.

    Mary

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      Originally posted by mmccarthy
      In the second patient table have you got a foreign key to the primary key of the first lab table e.g. LabID

      Assuming you do you could try setting the default value of the PatientCount field in the patient table to the following:

      Code:
      DCount("[PatientID]", "tblPatients", "[LabID]=" & [LabID])
      I'm not sure if this will work but it's worth a try. I had to guess at the field and table names so substitute your own.

      Mary
      If you just need the results in an UPDATE query.

      Code:
      UPDATE tblLabs SET PatientCount = DCount("[PatientID]", "tblPatients", "[LabID]=" & [LabID]);

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        Mary didn't say this explicitly, but it is a better idea generally, to calculate this value on the fly rather than to keep it stored in your table. See Normalisation and Table structures for an explanation of why.

        Comment

        Working...