DCOUNT 函数显示错误数据类型不匹配



运行代码时,出现以下错误:

条件表达式中的数据类型不匹配。

我正在尝试检查表 tblMain 中是否存在具有自动增量字段 ID 的记录。

Dim myId as Long
Dim tableName as string
myId = 1145589
tableName = "Main"

注意:以上部分只是为了展示这些变量类型和价值是什么。 它们在我的代码中实际上并没有像这样呈现。

If Not DCount("Id", "tbl" & tableName, "Id='" & myId & "'") = 1 Then
    Err.Raise 540, "This record does not exist."
End If

我尝试运行以下查询,它很好:

SELECT Count(Id) FROM tblMain WHERE Id = 1145589

代码有什么问题?

A Long 不得引用:

If Not DCount("*", "tbl" & tableName, "Id=" & myId & "") = 1 Then
    Err.Raise 540, "This record does not exist."
End If

myId 变量周围只有一个刻度,但它是数字,所以你不需要它们。

"Id='" & myId & "'"

应该是

"Id=" & myId 

以下是完整的更新代码:

If Not DCount("Id", "tbl" & tableName, "Id=" & myId) = 1 Then
    Err.Raise 540, "This record does not exist."
End If

相关内容

最新更新