简体   繁体   中英

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. Each employee in the table can have up to 2 managers. With exception of Manager_Name1 and Department1 fields, the remaining fields in the table can be 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. 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.

A snippet of my VBA Code follows:

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.

I am calling this VBA function in a MS Access query. In combining the two checks using a IIF clause, I get unexpected results for the query

Sql statement follows:

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??? There are no trailing or leading spaces in any fields. VBA compiles fine with option explicit flag turned on. 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.

Also, you would not use Like for exact comparison but:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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