Private Sub Command201_Click()
On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim rsDetails As DAO.Recordset
Set db = CurrentDb
Set rsDetails = db.OpenRecordset("SELECT * FROM [DDL Detail]", dbOpenDynaset, dbSeeChanges)
Do While Not Me![Daily Generation Sub].Form.Recordset.EOF
' Retrieve ReadingId from the main form record (DDL Master)
Dim ReadingIdx As Integer
ReadingIdx = Me!ReadingId
' Retrieve DDL_ID from the subform record
Dim ddlID As Integer
ddlID = Me![Daily Generation Sub].Form![ID] ' Hourly table 'DDL_ID' with your actual field name
' Retrieve values from the subform record
Dim neoValue As Double
neoValue = Nz(Me![Daily Generation Sub].Form![NEO]) ' 'NEO' in the subform
Dim EffQ As Double
EffQ = Nz(Me![Daily Generation Sub].Form![EfficiencyQ]) ' 'EfficiencyQ' uncorrected efficiency in the subform
Dim HRc As Double
HRc = Nz(Me![Daily Generation Sub].Form![HRcorr], 1) ' 'HRcorr' Heat Rate correction factor in the subform
' Perform the calculation for Efficiency
' Calculate effxValue considering null values in NEO or LHV
Dim effxValue As Variant
effxValue = Nz(Round(EffQ * HRc, 5))
' Ensure subform record is saved before updating
Me![Daily Generation Sub].Form.Dirty = False ' Save any pending changes in subform
' Refetch the record to get the latest data
rsDetails.Requery
' Update the Eff_Actual field with error handling
rsDetails.FindFirst "[ID] = " & ddlID & " AND [ReadingId] = " & ReadingIdx
If Not rsDetails.NoMatch Then
rsDetails.Edit
rsDetails![Eff_Actual] = Nz(effxValue, 0)
' Attempt update with retry mechanism
Dim updateSuccessful As Boolean
Do
On Error Resume Next
rsDetails.Update
If Err.Number = 0 Then
updateSuccessful = True
Exit Do
ElseIf Err.Number <> 3197 Then ' Not a write conflict
MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
Exit Do
End If
On Error GoTo ErrorHandler
' Wait briefly before retrying
DoEvents
Loop Until updateSuccessful
End If
Me![Daily Generation Sub].Form.Recordset.MoveNext
Loop
rsDetails.Close
Set rsDetails = Nothing
DoCmd.RunCommand acCmdSaveRecord
Me.E02_By = TempVars("EmployeeId")
MsgBox "Record Saved", vbInformation, "Thar Energy Ltd."
Exit Sub
ErrorHandler:
MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
Err.Clear ' Clear the error for further iterations
Resume Next ' Continue to the next iteration after an error
End Sub