An Update query still was intermittent. Called from btnCount on the Form with cboTPL. This is bound TPL in Disks table
I'm having more success now with a simpler command
Me!TPL=total:Me.Refresh
But as a learning aid I'd like to know why the recordset fails. So some lines below are remmed out to keep seeing the problem.
Cat is a ID String field in Disks and named TCat in Tracks. It's also returned from Function Thisdisk()
Disks and Tracks are the 2 tables for frmDisks and subform Tracks
TPF is a short text field in Disks
I'm not sure what else I can say to better describe this.
When I first move to the record, everything works. But if cboTPL is cleared and then btnCount clicked the first time nothing happens (except the msgbox showing the error).
Click again and it works. You can repeat this at will. Move to another record and back and it works (until cboTPL is again cleared)
Here is the full routine:
Nothing is written to the Table. rst.RecordCount is 1 and both TCount and Total contain the expected values
rst.update executes with no error. The error shows on the MsgBox line where me!TPL is indeed null. But 3 lines above that it's been assigned to total.
Code:
Private Sub btnCount_Click()
Call UpdateTPL(ThisDisk())
End Sub
Sub UpdateTPL(Cat)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim Durtime As String, min As Long, sec As Long
Dim total, hr
Dim sql As String
Dim TCount As Long
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT DISTINCTROW Tracks.TDuration FROM Tracks WHERE Tracks.TCat=" & Cat & ";")
While Not rst.EOF
Durtime = Nz(rst!TDuration, "")
min = min + Val(Left$(LTrim$(Durtime), 2))
sec = sec + Val(Right$(Durtime, 2))
rst.MoveNext
Wend
TCount = rst.RecordCount
min = min + Int(sec / 60)
sec = sec Mod 60
hr = Int(min / 60)
min = min Mod 60
Set rst = Nothing
total = Str$(hr) & ":" & Format(min, "00") & ":" & Format(sec, "00")
' sql = "Update Disks set TPL = '" & total & "' Where Cat = " & ThisDisk()
' DoCmd.RunSQL sql
' Me!TPL = total
' Me!TrackCount = TCount
Set rst = db.OpenRecordset("SELECT Disks.TPL, Disks.TrackCount FROM Disks WHERE Disks.Cat=" & Cat & ";")
total = Str$(hr) & ":" & Format(min, "00") & ":" & Format(sec, "00")
If rst.RecordCount <> 0 Then
rst.Edit
rst!TrackCount = TCount
rst!TPL = total
rst.Update
Me.Refresh
MsgBox Me.TPL
End If
Set rst = Nothing
Set db = Nothing
End Sub