Results 1 to 4 of 4
  1. #1
    liuc74 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Oct 2018
    Posts
    2

    CurrentDB.Execute issue - changes are not applied

    Hi,


    I'm not very skilled with Access and a problem with Currentdb.Execute gave me an headache.

    This is the scenario:
    A user select some employees checking them on a continuos form binded to a temp table (tbl_check) that I fill before the form is opened.
    When he click the save button, I insert the selected employees (only the ID Field) into another table (tbl_Afferenza) which maps the N:M relationship between employee and office tables.
    After that I need to update the tbl_Ufficio (office table)table setting it's NumImpiegati (nr of employee)field with the number of employee associated to that office (count on tbl_Afferenza table).

    The problem is that the first insert works , but the subsequent update does not.

    This is the code i wrote:


    Code:
    Function SaveRecord() As Boolean
    
    
    Dim strSql As String
    Dim blnTrans As Boolean
    
    
        On Error GoTo errSaveRecord
            
        Form.Refresh
        
        BeginTrans
        blnTrans = True
        
        With CurrentDb
    
    
            'Insert the selected employee (flag_check=true) into tbl_Affrenza (N:M relationship)
    
    
            strSql = "INSERT INTO tbl_Afferenza (MATRICOLA, IDUFFICIO) " & _
            "SELECT STRID, " & txtHiddenIdUfficio & _
            " FROM tbl_Check " & _
            " WHERE flag_check=true"
        
            .Execute strSql, dbFailOnError
            
                 
            ' Update the tbl_Ufficio's NumImpiegati field with the number of employee associated to the office
            strSql = "UPDATE TBL_UFFICIO " & _
            "SET NUMIMPIEGATI = DCOUNT(""MATRICOLA"",""TBL_AFFERENZA"",""IDUFFICIO=" & txtHiddenIdUfficio & """) " & _
            "WHERE ID=" & txtHiddenIdUfficio
           
            .Execute strSql, dbFailOnError
            
            
        End With
        SaveRecord = True
        CommitTrans
        blnTrans = False
    
    
    okSaveRecord:
      
    
    
        Exit Function
    errSaveRecord:
    
    
        SaveRecord = 0
        If blnTrans = True Then
            blnTrans = False
            Rollback
        End If
    
    
    End Function

    The DCOUNT function doesn't find the record just added.

    I tried with and without transaction.
    I also tried to use CurrentDB.Execute each time and, as you can see, the With CurrentDB block.
    I don't know how to solve it.

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Because you are using a transaction , the records you insert aren't committed before you try your record count update.

    You need to set a db reference for each update, at the moment you are doing the whole thing within one "db session", which is why the updated record doesn't appear.

    Personally, I wouldn't store this value , simply use a totals query to return the current staff per office, and display it. It will always be up to date and accurate.
    Secondly why all the transactional stuff anyway ? Unless you have a super flakey network I don't think it's adding anything, as you aren't asking the user to confirm to commit the changes?

    You could probably also achieve this with a multiselect list box of employees, and avoid the temp table all together?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    liuc74 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Oct 2018
    Posts
    2
    Thank you for answer.
    As I said, I'm not familiar with Access. I will take a look at multiselect list box.

    I'm not sure that the problem is the transaction because I tried also without transaction and also with db reference for each update.
    Moreover, I have updated the code adding this lines between the insert and the update
    Code:
     Set rs = .OpenRecordset("Select Count(Matricola) as NumImpiegati From tbl_Afferenza Where IdUfficio=" & txtHiddenIdUfficio)
     lngNR = rs!NumImpiegati
    The update is slightly modified
    Code:
    strSql = "UPDATE TBL_UFFICIO " & _" SET NUMIMPIEGATI = " & lngNR & _
    " WHERE ID=" & txtHiddenIdUfficio
            
    .Execute strSql, dbFailOnError
    And now it works even within transaction.

  4. #4
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Okay, that's a bit strange. But if it's working then all is good.
    I still wouldn't use the transaction functions if you don't really need them.

    I would also set a Db variable and use that, just a personal preference, some timing tests have shown it's quicker on certain recordset operations on large datasets.
    It also allows you to get the number of records affected, which you can't do as easily with CurrentDb.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. RunSQL vs. QueryDef.Execute vs. CurrentDB.Execute
    By caubetiep1337 in forum Queries
    Replies: 8
    Last Post: 12-16-2015, 05:35 PM
  2. CurrentDB.Execute insert working just once
    By krausr79 in forum Access
    Replies: 4
    Last Post: 10-07-2015, 12:04 PM
  3. Error in CurrentDb.Execute
    By Stefan Moser in forum Access
    Replies: 5
    Last Post: 04-17-2015, 01:38 PM
  4. vba code help - Currentdb.execute
    By joycesolomon in forum Programming
    Replies: 5
    Last Post: 08-11-2014, 11:25 AM
  5. Update sql using currentdb.execute
    By laavista in forum Access
    Replies: 13
    Last Post: 08-15-2011, 03:51 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