473,503 Members | 12,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Functions for results of preceeding queries

3 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT Numéro, Nom, Supérieur
  2. FROM tblEmployés
  3. 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.
Jun 17 '07 #1
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.
Jun 17 '07 #2
Cougar50
3 New Member
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...
Jun 17 '07 #3
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

Expand|Select|Wrap|Line Numbers
  1. Public Sub RetrieveEmployes(lngNumero As Long, _
  2. intStartLevel As Integer, _
  3. intEndLevel As Integer)
  4. Dim strSQL As String
  5. Dim RS As New ADODB.Recordset
  6.  
  7. With RS
  8. .Source = "SELECT Numero, Nom, Superieur FROM tblEmployes;"
  9. .ActiveConnection = CurrentProject.Connection
  10. .CursorType = adOpenStatic
  11. .LockType = adLockOptimistic
  12. .Filter = "Superieur = " & lngNumero
  13. .Open
  14.  
  15. End With
  16.  
  17. With DoCmd
  18. .SetWarnings False
  19. .RunSQL "DELETE tblTarget.* FROM tblTarget;"
  20. LevelDown 1, intStartLevel, intEndLevel, RS
  21. .SetWarnings True
  22. End With
  23.  
  24. Set RS = Nothing
  25. End Sub
  26. Private Sub LevelDown(ByVal intLevel As Integer, _
  27. ByVal intStartLevel As Integer, _
  28. ByVal intEndLevel As Integer, _
  29. ByRef RS As ADODB.Recordset)
  30.  
  31. Dim rsClone As ADODB.Recordset
  32.  
  33. With RS
  34.  
  35. While Not .EOF
  36.  
  37. If intLevel >= intStartLevel And intLevel <= intEndLevel Then _
  38. DoCmd.RunSQL "INSERT INTO tblTarget ( Nom, Numero, [Level] )" & _
  39. " SELECT '" & ![Nom] & "', " & ![Numero] & ", " & _
  40. intLevel & ";"
  41.  
  42. Set rsClone = .Clone
  43. rsClone.Filter = "Superieur=" & ![Numero]
  44. LevelDown intLevel + 1, intStartLevel, intEndLevel, rsClone
  45.  
  46. .MoveNext
  47. Wend
  48.  
  49. End With
  50.  
  51. Set rsClone = Nothing
  52.  
  53. End Sub
  54.  
  55.  

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.
Jun 18 '07 #4
Cougar50
3 New Member
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

Expand|Select|Wrap|Line Numbers
  1. Public Sub RetrieveEmployes(lngNumero As Long, _
  2. intStartLevel As Integer, _
  3. intEndLevel As Integer)
  4. Dim strSQL As String
  5. Dim RS As New ADODB.Recordset
  6.  
  7. With RS
  8. .Source = "SELECT Numero, Nom, Superieur FROM tblEmployes;"
  9. .ActiveConnection = CurrentProject.Connection
  10. .CursorType = adOpenStatic
  11. .LockType = adLockOptimistic
  12. .Filter = "Superieur = " & lngNumero
  13. .Open
  14.  
  15. End With
  16.  
  17. With DoCmd
  18. .SetWarnings False
  19. .RunSQL "DELETE tblTarget.* FROM tblTarget;"
  20. LevelDown 1, intStartLevel, intEndLevel, RS
  21. .SetWarnings True
  22. End With
  23.  
  24. Set RS = Nothing
  25. End Sub
  26. Private Sub LevelDown(ByVal intLevel As Integer, _
  27. ByVal intStartLevel As Integer, _
  28. ByVal intEndLevel As Integer, _
  29. ByRef RS As ADODB.Recordset)
  30.  
  31. Dim rsClone As ADODB.Recordset
  32.  
  33. With RS
  34.  
  35. While Not .EOF
  36.  
  37. If intLevel >= intStartLevel And intLevel <= intEndLevel Then _
  38. DoCmd.RunSQL "INSERT INTO tblTarget ( Nom, Numero, [Level] )" & _
  39. " SELECT '" & ![Nom] & "', " & ![Numero] & ", " & _
  40. intLevel & ";"
  41.  
  42. Set rsClone = .Clone
  43. rsClone.Filter = "Superieur=" & ![Numero]
  44. LevelDown intLevel + 1, intStartLevel, intEndLevel, rsClone
  45.  
  46. .MoveNext
  47. Wend
  48.  
  49. End With
  50.  
  51. Set rsClone = Nothing
  52.  
  53. End Sub
  54.  
  55.  

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...
Jun 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
12876
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 ...
3
1805
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...
3
1791
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...
8
2951
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...
22
3769
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...
16
55357
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...
9
3294
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...
1
2662
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...
1
19382
MMcCarthy
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 ...
2
2460
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...
0
7193
marktang
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,...
0
7264
Oralloy
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,...
0
7316
jinu1996
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...
1
6975
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...
0
7449
tracyyun
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...
0
5562
agi2029
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,...
0
4666
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...
0
1495
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 ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.