Results 1 to 6 of 6
  1. #1
    ferarbe is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2016
    Posts
    4

    How to update a field based on information from other fields from a query in VBA

    Hello all. I have known my way for many years in Access but I am novice in VBA, so I require you generous help and expertise. I guess it should be pretty simple.
    I have this query:

    proyect_id organization_id previous_counter
    272 57 0
    272 15 0
    274 28 0
    296 35 1

    and I want to create a VBA module that can create a new field (new_counter) based on the old counter and the count of organizations per project, like this:

    proyect_id organization_id previous_counter new_counter
    272 57 0 1
    272 15 0 2
    274 28 0 1
    296 35 1 2


    Is that possible? Any help would be greatly appreciated.

    Thank you very much.
    Last edited by ferarbe; 01-17-2016 at 06:40 PM. Reason: It keeps deleting the text when I post

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    providing a) you do not have the same organisation_id in more than once per projectID and b) you don't mind the order the organisations appear then something like this will do what you require

    Code:
    SELECT *, (SELECT Count(*) FROM myTable As T WHERE projectID=myTable.projectID and organisationID<=myTable.organisationID) As newcounter
    FROM myTable
    ORDER BY projectID,organisationID

  3. #3
    ferarbe is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2016
    Posts
    4
    Brilliant! Thank you very much. I am much more confortable using SQL but I hadn't figured out a way to do it with it.
    Now I just need to sum it to the the old counter when it is not 0.
    I only do not understand why the "As T" since it is not mentioned anywhere else.
    Thanks again.

  4. #4
    ferarbe is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2016
    Posts
    4
    Is it possible to make it directly an update query?

    when I try to do an update query based on the query, it says "Operation must use an updatable query". I am using this code:

    UPDATE Query3 INNER JOIN MyTable ON ([Query3].organizationID=MyTable.organizationID) AND ([Query3].projectID =MyTable.projectID) SET MyTable.[previous_counter] = [query3].[recounter];

    in which recounter is new_counter+max_counter
    and max_counter is the max previous_counter of a project.

    Do you see the proble?
    Thank you very much for your time

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    problem is that you cannot use sub queries in update queries.

    try using dcount instead

    SELECT *,DCount("*","myTable" ,"projectID=" & myTable.projectID & " and organisationID<=" & myTable.organisationID) As newcounter

  6. #6
    ferarbe is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2016
    Posts
    4
    Great! That worked perfectly. Thank you very much. I made it an update query and added DMax to calculate the newcounter:

    UPDATE MyTable SET MyTable.[counter] = DCount("*","myTable","projectID=" & [myTable].[projectID] & " and organizationID<=" & [myTable].[organizationID])+DMax("[counter]","myTable","projectID=" & [MyTable].[projectID]);

    Thanks a lot for your time.
    Cheers!

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

Similar Threads

  1. Replies: 1
    Last Post: 10-29-2012, 08:15 AM
  2. Replies: 2
    Last Post: 08-30-2012, 07:59 AM
  3. Replies: 9
    Last Post: 04-13-2012, 10:10 AM
  4. Replies: 1
    Last Post: 08-31-2011, 04:03 PM
  5. Replies: 4
    Last Post: 09-03-2009, 02:01 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