如何在访问表中检查数据是否存在



我创建了一个用户形式,将数据插入访问表中。在插入数据时,我想确保插入的ID必须存在于访问表中。我已经使用了DCOUNT函数来执行此操作,但这是"类型不匹配"错误。我尝试了在互联网上找到的所有解决方案,但这里没有任何解决方案。请帮助!

我已经修改了dcount表达式,以将form变量名称放入",[],创建一个引用dcount函数的外部变量,但没有任何作用

Set conn = createobject("ADODB.connection")
set rs = createobject("ADODB.recordset")
strconn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data source = C:MyPathMyDB.accdb"
qry = "select * from employee"
with rs
.adnew
if isnumeric(ManagerID) = false then
msgbox "Invalid Manager ID"
exit sub
elseif application.worksheetfunction.dcount("Employee_ID","Employee","activ='Yes'  and Employee_ID='"  & [EmployeeForm.ManagerID] & "'") = 0 then
msgbox "Manager does not exist"
exit sub
else 
. fields("Manager_ID").value = ManagerID
end if
end with

我希望该功能能够确定员工是否存在雇员中的managerid。如果是,请继续,否则显示错误消息

由于您检查了要数字的 managerId ,我猜它的值不是文本,而 active 可能是布尔值,并且您可以单独访问 ManagerId ,请按照进行,并且标准可以读取:

"activ=True and Employee_ID=" & ManagerID & ""

dcount(您要使用的一个(是访问功能:Excel一个用于查询工作表范围。您需要查询您的访问数据库,以查看是否有记录:

例如:

sql =  "select * from employee where activ='Yes' and Employee_ID='" & _
        [EmployeeForm.ManagerID] & "'"
rs.open sql, conn
If not rs.eof then
    'got a match: add the new record and update to database
else
    msgbox "Manager not found!"
end if

最新更新