ghaphis
asked on
DCount mismatch criteria error
Hello,
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
Exit Sub
Else
If DCount("*", "DeathTable", "surveyid = " & Me.Surveyid) = 0 Then
MsgBox "Missing record in Death Table!"
Cancel = 1
Exit Sub
Else
Cancel = 0
End If
End If
End Sub
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
Exit Sub
Else
If DCount("*", "DeathTable", "surveyid = " & Me.Surveyid) = 0 Then
MsgBox "Missing record in Death Table!"
Cancel = 1
Exit Sub
Else
Cancel = 0
End If
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
typo ... extra &
If DCount("*", "DeathTable", "surveyid = " & chr(34) & Me.Surveyid & Chr(34) ) > 0
If DCount("*", "DeathTable", "surveyid = " & chr(34) & Me.Surveyid & Chr(34) ) > 0
If DCount("*", "DeathTable", "surveyid = " & &chr(34) & Me.Surveyid & Chr(34) ) > 0
mx