Results 1 to 7 of 7
  1. #1
    hfreedman1957 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2017
    Posts
    35

    Update record count from one table into recordcount field in another table

    Hi,
    I have a Project table, and for each project record identified by Project_ID, there are numerous records in an Services table, relationship by Project_ID key.


    I'm hoping I can loop through Project table, and for each record, perform an OpenRecordset operation on the Services table based on a Select statement that return the set of records for that project. All I need is the count of the number of Services records returned and to update a "recordcount" field in the Project table record. Please help! I'm still a bit of a newbie for Access VBA but am usually able to look at other code, understand it, and modify it for my needs. I'm currently doing this manually by running a query with a counter and entering the count into the project table record. Any help would be appreciated! Thanks so much!

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    And if you add a few Services to a Project, the Count will be incorrect.

    Suggest you look at your relationships/design.

    1 Project may have 1 or many Services (a 1 to many set up)

    Then use form/subform construct where form relates to the 1 side and subform represents the Services for that Project.

  3. #3
    hfreedman1957 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2017
    Posts
    35
    I should have explained a little further. You are correct of course. I will always run this "job" just before I run a report I have, to ensure the record count is accurate. The report only pulls information from the project table. I want to keep it that way as the report is complex (for me) by looping through the project records and generating an email to the project manager for each project. Attached to the email is a PDF format report, which includes information for just that one project taken entirely from the project record fields, then repeating for each project. I now need each project record to include the services record count. I already have the one to many relationship set up require for all the other functionality. I hope that provide sufficient rational for my approach. Please help!

  4. #4
    hfreedman1957 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2017
    Posts
    35
    Can someone debug this SQL statement please....

    strSQL = "SELECT Project_ID, Environment_ID"
    strSQL = strSQL & " FROM tblActions"
    strSQL = strSQL & " WHERE Project_ID = '" & "rsProjects.Project_ID'" & " AND " & "Environment_ID = 1"
    Set rsActions = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

    rsProjects is from an earlier OpenRecordset statement. I get very confused with the quotes :-)
    Thanks!

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    My guess is that rsProjects.Project_ID is a number, so you do not need the single quote ' for numbers

    In fact you do not need rsProjects.Project_ID within quotes of any sort since your want Access to render the value.

    Code:
    strSQL = strSQL & " WHERE Project_ID = '" & "rsProjects.Project_ID'" & " AND " & "Environment_ID = 1"
    becomes
    Code:
    strSQL = strSQL & " WHERE Project_ID = " & rsProjects.Project_ID & " AND Environment_ID = 1"
    Suggestion though is to always do a debug.print strSQL to see/check how Access has interpreted your code before running it.

  6. #6
    hfreedman1957 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2017
    Posts
    35
    Perfect!!! Thanks so much. That put me past the error and I now have the code working perfectly, updating the counts exactly. Thanks so very vey much!

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

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

Similar Threads

  1. store detail table record count in header table
    By hfreedman1957 in forum Access
    Replies: 6
    Last Post: 06-22-2017, 10:17 AM
  2. Replies: 4
    Last Post: 02-18-2015, 11:28 AM
  3. Replies: 5
    Last Post: 12-17-2014, 09:51 PM
  4. Replies: 8
    Last Post: 10-03-2013, 08:11 AM
  5. Replies: 5
    Last Post: 11-01-2012, 09:26 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