我创建了一个将数据插入 Access 表的用户表单。在插入数据时,我想确保插入的 ID 必须存在于 Access 表中。我已经使用 DCOUNT 函数来执行此操作,但这会呈现“类型不匹配”错误。我已经尝试了在互联网上找到的所有解决方案,但这里没有任何效果。请帮忙!
我修改了 DCOUNT 表达式,将表单变量名放入 '', [],创建了一个引用 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
我希望该函数确定 Employeeform.ManagerID 是否存在于 Employee_ID 中。如果是,则继续,否则显示错误消息