DCount mismatch criteria error
Posted on 2010-09-01
I have a form with a tab control and two subforms inside separate tabs. The subforms save data into two different tables. The data outside the subform saves data to a third table.
I am using DCount code so that it checks whether the last subform has been filled before the form unloads. If the surveyid (primary key) from the main form is found in the subform's table, then it should allow the form to unload. If there is no match in the table, the form should not unload.
The code below executes correctly when there are no rows in the table, but when it finds matching rows in the table I get the error "Data type mismatch in criteria expression". Is there something wrong with my code?
Private Sub form_unload(Cancel As Integer)
If IsNull(Me.Surveyid) Then Exit Sub
If DCount("*", "DeathTable", "surveyid = " & Me.Surveyid) > 0 Then
Cancel = 0
If DCount("*", "DeathTable", "surveyid = " & Me.Surveyid) = 0 Then
MsgBox "Missing record in Death Table!"
Cancel = 1
Cancel = 0