I have a table with employee number, employee name and superior number (which is the employee number of the superior).
I have to find for who on employee is the superior: - SELECT Numéro, Nom, Supérieur
-
FROM tblEmployés
-
WHERE Supérieur =10
pretty easy..
but then I have to find who are the employees of the employees of employee number 10,
then who are the employees of the employees of the employees of employee number 10 ,
and finally the employees that are not superiors of anyone...
Can someone help...as fast as possible please.
4 1227 FishVal 2,653
Recognized Expert Specialist
I have a table with employee number, employee name and superior number (which is the employee number of the superior).
I have to find for who on employee is the superior:
SELECT Numéro, Nom, Supérieur
FROM tblEmployés
WHERE Supérieur =10
pretty easy..
but then I have to find who are the employees of the employees of employee number 10,
then who are the employees of the employees of the employees of employee number 10 ,
and finally the employees that are not superiors of anyone...
Can someone help...as fast as possible please.
Hi!
You have a self-referencing table where [Numéro] is a primary key, and [Supérieur] is based on it foreign key. Am I right?
If so I will be glad to suggest you example of VB function to recurcievely retrive employees.
If not so plz clarify table-to-table relationships.
P.S. By the way, you can visually browse them in Datasheet/Subdatasheet table view.
yes you are right, it is only one table... self referencing... there is no link to anything else, any other tables... the primary key is numéro, it is unique and it goes from 1 to 10 552, the second column is the name, and the third column is the supérieur, which is the employee (numéro) who is the supérieur for every employee (can be similar or different from one employee to another).
please give me ur example for the 3 queries i had in the earlier post.
thanks a lot...
FishVal 2,653
Recognized Expert Specialist
yes you are right, it is only one table... self referencing... there is no link to anything else, any other tables... the primary key is numéro, it is unique and it goes from 1 to 10 552, the second column is the name, and the third column is the supérieur, which is the employee (numéro) who is the supérieur for every employee (can be similar or different from one employee to another).
please give me ur example for the 3 queries i had in the earlier post.
thanks a lot...
Well I can suggest you the following
1. Create table "tblTarget" with the following fields
Nom (Text type)
Numero (Long number type)
Level (Integer number type)
2. Place the code below to a public module -
Public Sub RetrieveEmployes(lngNumero As Long, _
-
intStartLevel As Integer, _
-
intEndLevel As Integer)
-
Dim strSQL As String
-
Dim RS As New ADODB.Recordset
-
-
With RS
-
.Source = "SELECT Numero, Nom, Superieur FROM tblEmployes;"
-
.ActiveConnection = CurrentProject.Connection
-
.CursorType = adOpenStatic
-
.LockType = adLockOptimistic
-
.Filter = "Superieur = " & lngNumero
-
.Open
-
-
End With
-
-
With DoCmd
-
.SetWarnings False
-
.RunSQL "DELETE tblTarget.* FROM tblTarget;"
-
LevelDown 1, intStartLevel, intEndLevel, RS
-
.SetWarnings True
-
End With
-
-
Set RS = Nothing
-
End Sub
-
Private Sub LevelDown(ByVal intLevel As Integer, _
-
ByVal intStartLevel As Integer, _
-
ByVal intEndLevel As Integer, _
-
ByRef RS As ADODB.Recordset)
-
-
Dim rsClone As ADODB.Recordset
-
-
With RS
-
-
While Not .EOF
-
-
If intLevel >= intStartLevel And intLevel <= intEndLevel Then _
-
DoCmd.RunSQL "INSERT INTO tblTarget ( Nom, Numero, [Level] )" & _
-
" SELECT '" & ![Nom] & "', " & ![Numero] & ", " & _
-
intLevel & ";"
-
-
Set rsClone = .Clone
-
rsClone.Filter = "Superieur=" & ![Numero]
-
LevelDown intLevel + 1, intStartLevel, intEndLevel, rsClone
-
-
.MoveNext
-
Wend
-
-
End With
-
-
Set rsClone = Nothing
-
-
End Sub
-
-
3. Now you can call RetrieveEmployes using the following syntax
RetrieveEmployes Numero, StartLevel, EndLevel
it will write to table "tblTarget" employee name, number and level of subordination for all employes of employee having number Numero with subordination level from StartLevel, EndLevel
Example
RetrieveEmployes 10, 3, 3
will retrieve employes of employes of employes of employee 10
RetrieveEmployes 10, 1, 2
will retrieve (employes and employes of employes) of employee 10
It works fine with small table I've tried. With large table it may be time consuming.
Well I can suggest you the following
1. Create table "tblTarget" with the following fields
Nom (Text type)
Numero (Long number type)
Level (Integer number type)
2. Place the code below to a public module -
Public Sub RetrieveEmployes(lngNumero As Long, _
-
intStartLevel As Integer, _
-
intEndLevel As Integer)
-
Dim strSQL As String
-
Dim RS As New ADODB.Recordset
-
-
With RS
-
.Source = "SELECT Numero, Nom, Superieur FROM tblEmployes;"
-
.ActiveConnection = CurrentProject.Connection
-
.CursorType = adOpenStatic
-
.LockType = adLockOptimistic
-
.Filter = "Superieur = " & lngNumero
-
.Open
-
-
End With
-
-
With DoCmd
-
.SetWarnings False
-
.RunSQL "DELETE tblTarget.* FROM tblTarget;"
-
LevelDown 1, intStartLevel, intEndLevel, RS
-
.SetWarnings True
-
End With
-
-
Set RS = Nothing
-
End Sub
-
Private Sub LevelDown(ByVal intLevel As Integer, _
-
ByVal intStartLevel As Integer, _
-
ByVal intEndLevel As Integer, _
-
ByRef RS As ADODB.Recordset)
-
-
Dim rsClone As ADODB.Recordset
-
-
With RS
-
-
While Not .EOF
-
-
If intLevel >= intStartLevel And intLevel <= intEndLevel Then _
-
DoCmd.RunSQL "INSERT INTO tblTarget ( Nom, Numero, [Level] )" & _
-
" SELECT '" & ![Nom] & "', " & ![Numero] & ", " & _
-
intLevel & ";"
-
-
Set rsClone = .Clone
-
rsClone.Filter = "Superieur=" & ![Numero]
-
LevelDown intLevel + 1, intStartLevel, intEndLevel, rsClone
-
-
.MoveNext
-
Wend
-
-
End With
-
-
Set rsClone = Nothing
-
-
End Sub
-
-
3. Now you can call RetrieveEmployes using the following syntax
RetrieveEmployes Numero, StartLevel, EndLevel
it will write to table "tblTarget" employee name, number and level of subordination for all employes of employee having number Numero with subordination level from StartLevel, EndLevel
Example
RetrieveEmployes 10, 3, 3
will retrieve employes of employes of employes of employee 10
RetrieveEmployes 10, 1, 2
will retrieve (employes and employes of employes) of employee 10
It works fine with small table I've tried. With large table it may be time consuming.
Thanks a lot... it works... my table is 10 552 units but still works fine... Thanks...
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Matt O'Donnell |
last post by:
Does anyone know how I can 'join' the results of
one SQL query to the bottom of another?
Eg. I have two queries:
1. SELECT Name, Surname FROM People WHERE Surname = Smith
NAME ...
|
by: Mika |
last post by:
Is it possible to perform functions on a recordset (rather than on the
source database), e.g. COUNT, MAX etc
The reason I would like to do this is because I have a hugely complex view
which...
|
by: Gargamil |
last post by:
I've written a function to calculate a seriies of parameters based upon some
variables. I'd like to be able to return all the parameters to a query.
Now all the parameters are interrelated and...
|
by: silly |
last post by:
/* hello, I have some fairly naive queries here related to optimising code!
I know the first answer is 'don't' but leave that to one side for the
moment.
1) I'm looking for constructive comments...
|
by: TC |
last post by:
I have an Access database application with a lot of custom row
functions written in VBA. In other words, a lot of queries contain
calculated fields which use functions defined in the modules.
I...
| |
by: Rico |
last post by:
I'm moving some queries out of an Access front end and creating views out of
them in SQL Server 2005 express. In some of the numeric fields, I use nz
quite often, ( i.e. nz(,0)) to return a zero...
|
by: billmiami2 |
last post by:
I was playing around with the new SQL 2005 CLR functionality and
remembered this discussion that I had with Erland Sommarskog concerning
performance of scalar UDFs some time ago (See "Calling...
|
by: bgreenspan |
last post by:
Hi Everyone, I'm back for some more expert help. Here's what I am
doing and what I tried.
My database has entries with Contract Names and Expiry Dates, among
other fields. I have a form...
|
by: MMcCarthy |
last post by:
Access has a number of built-in functions which can be generally used in queries or VBA code. Some of the more common ones are:
Note: anything in square brackets is optional
Date Functions
...
|
by: Ken Fine |
last post by:
I have a database of lat/long positions. What I want to be able to do is to
be able to filter that database based on a selection by the user. In other
words the user picks point X and I want to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |