简体   繁体   English

在MS Access中选择查询,使用VBA模块返回意外结果

[英]Select query in MS Access returning unexpected results with VBA module

I have a table in my database named Employees including these 4 fields: Manager_Name1 and Manager_Name2 and the corresponding manager departments, Department1, and Department2. 我的数据库中有一个名为Employees的表,其中包括以下4个字段:Manager_Name1和Manager_Name2以及相应的经理部门Department1和Department2。 Each employee in the table can have up to 2 managers. 表格中的每个员工最多可以有2位经理。 With exception of Manager_Name1 and Department1 fields, the remaining fields in the table can be NULL. 除Manager_Name1和Department1字段外,表中的其余字段可以为NULL。

In a separate table named Master, I have a master table of all managers in the company with their respective departments with fields named ManagerID and DepartmentID. 在一个单独的名为Master的表中,我有一个公司中所有经理及其各自部门的主表,这些字段的名称分别为ManagerID和DepartmentID。 I need to validate that that the Manager_Name : department combination is valid - that the combination is present in the Master table of all managers. 我需要验证Manager_Name:department组合是否有效-该组合是否存在于所有经理的Master表中。

A snippet of my VBA Code follows: 我的VBA代码的摘要如下:

Public Function validateManagers(manager1 As string, department1 as String, Optional manager2 as String = VbNull, Optional department2 as String = vbNull)

dim db As DAO.database
dim rs As DAO.Recordset
dim sqlString1 As String
dim sqlString2 As String
set db = CurrentDb

sqlString1 = "SELECT [ManagerID] FROM [Master] WHERE [ManagerID] LIKE ""*" & manager1 & "*"" And [DepartmentID] Like ""*" & department1 & "*"""

set rs = dbs.OpenRecordset(sqlString1)

If Not rs.EOF Then
    validateManagers = "Valid manager"
Else
    validateManagers = manager1 & "is not a valid manager for department" & department1
    Exit Function
End If

rs.close
set rs = Nothing

A similar if...then structure as above is used to validate manager2 and department2, substituting in manager2 and department2 in the sql string and substituting sqlString2 for sqlString1 in the parameter of db.OpenRecordset. 使用与上述类似的if ... then结构来验证manager2和department2,用sql字符串中的manager2和department2代替,并用db.OpenRecordset参数中的sqlString1替换sqlString2。

I am calling this VBA function in a MS Access query. 我在MS Access查询中调用此VBA函数。 In combining the two checks using a IIF clause, I get unexpected results for the query 在使用IIF子句合并两个检查时, 我得到了查询的意外结果

Sql statement follows: Sql语句如下:

SELECT IIF([Manager_Name2] Is Not Null,validateManagers([Manager_Name1],[department1],[Manager_Name2],[department2]),  
IIF([Manager_Name2] Is Null And [ManagerName1] Is Not Null,validateManagers([ManagerName1], [department1]))) AS Validate_Managers, *     
  FROM Employees;

Using debug.print in VBA, I see that ManagerID and DepartmentID are both 1??? 在VBA中使用debug.print,我看到ManagerID和DepartmentID均为1 ??? There are no trailing or leading spaces in any fields. 在任何字段中都没有尾随或前导空格。 VBA compiles fine with option explicit flag turned on. VBA编译正常,并且启用了选项显式标志。 All fields in both tables are of the field type text. 两个表中的所有字段均为字段类型文本。 Fields names are verified as existing both tables, Employees and Master, with no spaces in between words. 字段名称被验证为既存在于表中,又存在于雇员和主表中,单词之间没有空格。

How can I fix the error in my code? 如何解决代码中的错误? Thanks for any help? 谢谢你的帮助?

As you have: 如您所愿:

manager1 As string, _
department1 as String, _
Optional manager2 as String = VbNull, _
Optional department2 as String = vbNull)

none of these will ever be Null. 这些都不是空的。 Further, vbNull is numeric 1 which makes no sense here. 此外,vbNull为数字1,在这里没有意义。

Also, you would not use Like for exact comparison but: 另外,您不会将Like用于精确比较,但:

sqlString1 = "SELECT [ManagerID] FROM [Master] WHERE [ManagerID] = '" & manager1 & "' And [DepartmentID] = '" & department1 & "'"

Even worse, tell your teacher, that the whole concept is moot, as you would design your table schema with proper referential integrity between the fields holding the manager ids and department ids. 告诉老师,更糟糕的是,整个概念是没有意义的,因为您将在具有经理ID和部门ID的字段之间设计具有适当参照完整性的表模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM