0

我创建了一个将数据插入 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 中。如果是,则继续,否则显示错误消息

4

2 回答 2

1

由于您检查ManagerID是否为数字,我猜它的值不是文本,并且active可能是一个布尔值,并且您可以自己访问ManagerID ,按原样使用它,标准可以读取:

"activ=True and Employee_ID=" & ManagerID & ""
于 2019-05-27T07:00:18.310 回答
0

Dcount(您尝试使用的那个)是一个 Access 函数: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
于 2019-05-27T05:45:15.620 回答