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.