views:

190

answers:

1

HI,

I have an Access application, in which I have an employee table. The employees are part of several different levels in the organization. The orgranization has 1 GM, 5 department heads, and under each department head are several supervisors, and under those supervisors are the workers.

Depending on the position of the employee, they will only have access to records of those under them.

I wanted to represent the organization in a table with some sort of level system. The problem I saw with that was that there are many ppl on the same level (for example suervisors) but they shouldn't have access to the records of a supervisor in another department. How should I approach this problem?

Thanks!

+2  A: 

One common way of keeping this kind of hierarchical data in a database uses only a single table, with fields something like this:

  1. userId (primary key)
  2. userName
  3. supervisorId (self-referential "foreign key", refers to another userId in this same table)
  4. positionCode (could be simple like 1=lakey, 2=supervisor; or a foreign key pointing to another table of positions and such)
  5. ...whatever else you need to store for each employee...

Then your app uses SQL queries to figure out permissions. To figure out the employees that supervisor 'X' (whose userId is '3', for example) is allowed to see, you query for all employees where supervisorId=3.

If you want higher-up bosses to be able to see everyone underneath them, the easiest way is just to do a recursive search. I.e. query for everyone that reports to this big boss, and for each of them query who reports to them, all the way down the tree.

Does that make sense? You let the database do the work of sorting through all the users, because computers are good at that kind of thing.

I put the positionCode in this example in case you wanted some people to have different permissions... for example, you might have a code '99' for HR employees which have the right to see the list of all employees.


Maybe I'll let some other people try to explain it better...

ewall
Can you help me a little with the SQL query? Do you think something like this will work? SELECT * FROM tblemployee WHERE userId IN (SELECT userId FROM tblemployee WHERE SupervisorId=3)
zohair
Not sure what you're going for with the double-decker query there.To get all the employees that report to the Supervisor who's userId is 3, you only need "SELECT * FROM tblemployee WHERE SupervisorId=3".
ewall
If you are trying to get all employees underneath a high-level manager... I don't believe that MS Access can do real recursive queries in SQL; but you can use VBA code, which I can't really write out for you here. But the idea is this: make a function called GetAllReports, give it the id of the supervisor you want to check, have it do the following: (1) query for all ids where their supervisor is the id you gave it, (2) for each id it found, run _itself_ again for that id and keep the results, and (3) return all the ids collected from the initial query and from each time it ran GetAllReports.
ewall