Long story short, I'm using a DAO Transaction during my unit testing.
Apparently, Dcount doesn't pickup the newly inserted records in the middle of a transaction. If I comment out the transaction code, it works fine. Ideas? :banghead:
cal is defined in this module, as a new clsCalendarEvent. Pertinent Code for clsCalendarEvent is below:
Apparently, Dcount doesn't pickup the newly inserted records in the middle of a transaction. If I comment out the transaction code, it works fine. Ideas? :banghead:
Code:
Private Sub AddEventRecordTestMethod()
On Error GoTo TestFail
Dim iCalRecCount As Integer
Dim iLogRecCount As Integer
Dim ws As DAO.Workspace
Dim db As DAO.Database
'setup transaction
Set db = CurrentDb
Set ws = DBEngine(0)
'get existing record counts
iCalRecCount = DCount("*", "tblCalendarEvents")
iLogRecCount = DCount("*", "tblLog")
'begin transaction
ws.BeginTrans
'Arrange:
cal.ColorNumber = "7ae7bf"
cal.Description = "TEST Description"
cal.StartDate = Date
cal.Summary = "TEST Summary"
cal.WorkOrder = 0
'Act:
If cal.AddEvent Then
Dim TestResult As Boolean
Dim iCalNewCount As Integer
Dim iLogNewCount As Integer
iCalNewCount = Nz(DCount("*", "tblCalendarEvents"), 0)
iLogNewCount = Nz(DCount("*", "tblLog"), 0)
If (iCalRecCount + 1) = iCalNewCount And (iLogRecCount + 1) = iLogNewCount Then
TestResult = True
Else
TestResult = False
End If
Else
Assert.Fail "cal.AddEvent Failed"
End If
'Assert:
Assert.IsTrue TestResult, "New records NOT added to tblCalendarEvents and tblLog"
Assert.Succeed
TestExit:
ws.Rollback
Set ws = Nothing
Set db = Nothing
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
'ws.Rollback
Set ws = Nothing
End Sub
cal is defined in this module, as a new clsCalendarEvent. Pertinent Code for clsCalendarEvent is below:
Code:
Private Sub AddEventRecord()
' Status: In Devlopment
' Comments:
' Params :
' Returns : Boolean
' Created : 05/21/19 10:16 GB
' Modified:
'TVCodeTools ErrorEnablerStart
On Error GoTo PROC_ERR
'TVCodeTools ErrorEnablerEnd
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim lngCalEventID As Long 'New Calendar Event ID, used to insert new record into tblLog for event
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblCalendarEvents", dbOpenDynaset, dbSeeChanges)
With rst
.AddNew
.Fields("WorkOrderID") = Me.WorkOrder
.Fields("Title") = Me.Summary
.Fields("Body") = Me.Description
.Fields("StartDate") = Me.StartDate
.Fields("AllDayEvent") = True
.Fields("CreateTime") = Now()
.Update
'pull newly created ID to log below
lngCalEventID = .Fields("ID")
.Close
End With
'change recordsets
Set rst = dbs.OpenRecordset("tblLog", dbOpenDynaset, dbSeeChanges)
With rst
.AddNew
.Fields("EventTypeID") = 1
.Fields("WorkOrderID") = Me.WorkOrder
.Fields("CalendarEventId") = lngCalEventID
.Fields("Memo") = "Added Calendar Event"
.Fields("TimeStamp") = Now()
.Update
.Close
End With
'TVCodeTools ErrorHandlerStart
PROC_EXIT:
Set rst = Nothing
dbs.Close
Set dbs = Nothing
Exit Sub
PROC_ERR:
Err.Raise Err.Number
Resume
'TVCodeTools ErrorHandlerEnd
End Sub