Connecting Tech Pros Worldwide Forums | Help | Site Map

How to Generate a User List

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#1   Jul 30 '07
For this Tip, we will show you an extremely handy, multi-user, feature of Jet that allows you to manage Users more effectively. You can create a special, provider-specific Recordset in ADO that supplies information about the current Users in the Database. This is accomplished by using the OpenSchema() Method of the Connection Object, which will fill a recordset with varied sets of different Database Schema information. The output of this OpenSchema() Method will be written to a List Box (lstUsers) with the pre-defined headings of Computer, UserName, Connected?, and Suspect?.

I intentionally decided to display this Tip at this specific point in time because it is intimately related to the prior Tip, Passive Shutdown. They are both extremely helpful, multi-user, features that Jet 4 exposes, and they will greatly assist in the implementation of certain administrative tasks such as: Updates, Backups, etc. There will be certain instances when you, as the administrator of a multi-user Database, will want to be 100% sure that no Users are currently logged on to your back end database. Through the use of Passive Shutdown, when no additional Users will be allowed to log on, and the User List, which will display any currently logged on Users, you will be 100% guaranteed that no currently logged on individual will interfere with a critical process.

The code segment below will demonstrate how you can display this User List in a List Box on a Form. There are ample comments scattered through the code, but if you need any further explanation on any specific area, please feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. 'The User List Schema information requires this magic number. For anyone
  2. 'who may be interested, this number is called a GUID or Globally Unique
  3. 'Identifier - sorry for digressing
  4. Const conUsers = "{947bb102-5d43-11d1-bdbf-00c04fb92675}"
  5.  
  6. Dim cnn As ADODB.Connection, fld As ADODB.Field, strUser As String
  7. Dim rst As ADODB.Recordset, intUser As Integer, varValue As Variant
  8.  
  9. Set cnn = CurrentProject.Connection
  10. Set rst = cnn.OpenSchema(Schema:=adSchemaProviderSpecific, SchemaID:=conUsers)
  11.  
  12. 'Set List Box Heading
  13. strUser = "Computer;UserName;Connected?;Suspect?"
  14.  
  15. With rst    'fills Recordset (rst) with User List data
  16.   Do Until .EOF
  17.     intUser = intUser + 1
  18.       For Each fld In .Fields
  19.         varValue = fld.Value
  20.           'Some of the return values are Null-Terminated Strings, if
  21.           'so strip them off
  22.           If InStr(varValue, vbNullChar) > 0 Then
  23.             varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
  24.           End If
  25.           strUser = strUser & ";" & varValue
  26.       Next
  27.         .MoveNext
  28.   Loop
  29. End With
  30.  
  31. Me!txtTotalNumOfUsers = intUser        'Total # of Users
  32.  
  33. 'Set up List Box Parameters
  34. Me!lstUsers.ColumnCount = 4
  35. Me!lstUsers.RowSourceType = "Value List"
  36. Me!lstUsers.ColumnHeads = False
  37.   lstUsers.RowSource = strUser       'populate the List Box
  38.  
  39. 'Routine cleanup chores
  40. Set fld = Nothing
  41. Set rst = Nothing
  42. Set cnn = Nothing
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Computer     UserName  Connected?  Suspect?
  2. DEZII          Admin       True
  3. IGUANA           Suzanne     True
  4. LIZARD           Paul         True
  5. FINANCE            Leonard     True



Newbie
 
Join Date: Jul 2007
Posts: 11
#2   Jul 31 '07

re: How to Generate a User List


Does this only list users who are using the same front end MDB? Or will it list users who are using separate front end MDBs linked to the same backend MDB?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#3   Jul 31 '07

re: How to Generate a User List


Quote:

Originally Posted by kentgorrell

Does this only list users who are using the same front end MDB? Or will it list users who are using separate front end MDBs linked to the same backend MDB?

In a Multiuser Environment, it will generate a list of Current Users who are logged on to the Back End Database from separate Front End MDBs linked to the same backend. This would be the most practical application.
Newbie
 
Join Date: Nov 2007
Posts: 1
#4   Nov 3 '07

re: How to Generate a User List


Quote:

Originally Posted by ADezii

For this Tip, we will show you an extremely handy, multi-user, feature of Jet that allows you to manage Users more effectively. You can create a special, provider-specific Recordset in ADO that supplies information about the current Users in the Database. This is accomplished by using the OpenSchema() Method of the Connection Object, which will fill a recordset with varied sets of different Database Schema information. The output of this OpenSchema() Method will be written to a List Box (lstUsers) with the pre-defined headings of Computer, UserName, Connected?, and Suspect?.

I intentionally decided to display this Tip at this specific point in time because it is intimately related to the prior Tip, Passive Shutdown. They are both extremely helpful, multi-user, features that Jet 4 exposes, and they will greatly assist in the implementation of certain administrative tasks such as: Updates, Backups, etc. There will be certain instances when you, as the administrator of a multi-user Database, will want to be 100% sure that no Users are currently logged on to your back end database. Through the use of Passive Shutdown, when no additional Users will be allowed to log on, and the User List, which will display any currently logged on Users, you will be 100% guaranteed that no currently logged on individual will interfere with a critical process.

The code segment below will demonstrate how you can display this User List in a List Box on a Form. There are ample comments scattered through the code, but if you need any further explanation on any specific area, please feel free to ask.

Expand|Select|Wrap|Line Numbers
  1. 'The User List Schema information requires this magic number. For anyone
  2. 'who may be interested, this number is called a GUID or Globally Unique
  3. 'Identifier - sorry for digressing
  4. Const conUsers = "{947bb102-5d43-11d1-bdbf-00c04fb92675}"
  5.  
  6. Dim cnn As ADODB.Connection, fld As ADODB.Field, strUser As String
  7. Dim rst As ADODB.Recordset, intUser As Integer, varValue As Variant
  8.  
  9. Set cnn = CurrentProject.Connection
  10. Set rst = cnn.OpenSchema(Schema:=adSchemaProviderSpecific, SchemaID:=conUsers)
  11.  
  12. 'Set List Box Heading
  13. strUser = "Computer;UserName;Connected?;Suspect?"
  14.  
  15. With rst    'fills Recordset (rst) with User List data
  16.   Do Until .EOF
  17.     intUser = intUser + 1
  18.       For Each fld In .Fields
  19.         varValue = fld.Value
  20.           'Some of the return values are Null-Terminated Strings, if
  21.           'so strip them off
  22.           If InStr(varValue, vbNullChar) > 0 Then
  23.             varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
  24.           End If
  25.           strUser = strUser & ";" & varValue
  26.       Next
  27.         .MoveNext
  28.   Loop
  29. End With
  30.  
  31. Me!txtTotalNumOfUsers = intUser        'Total # of Users
  32.  
  33. 'Set up List Box Parameters
  34. Me!lstUsers.ColumnCount = 4
  35. Me!lstUsers.RowSourceType = "Value List"
  36. Me!lstUsers.ColumnHeads = False
  37.   lstUsers.RowSource = strUser       'populate the List Box
  38.  
  39. 'Routine cleanup chores
  40. Set fld = Nothing
  41. Set rst = Nothing
  42. Set cnn = Nothing
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Computer     UserName  Connected?  Suspect?
  2. DEZII          Admin       True
  3. IGUANA           Suzanne     True
  4. LIZARD           Paul         True
  5. FINANCE            Leonard     True

Thank you for supplying the code above, can you give me some more information regarding the basics of setting it up. Do you need to place the code into a module? from which a list box named "1stUsers" placed on a form displays data from another table with the pre-defined headings of Computer, UserName, Connected?, and Suspect? Thanks you for any assistance you may be able to provide.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#5   Nov 3 '07

re: How to Generate a User List


Quote:

Originally Posted by jqwert

Thank you for supplying the code above, can you give me some more information regarding the basics of setting it up. Do you need to place the code into a module? from which a list box named "1stUsers" placed on a form displays data from another table with the pre-defined headings of Computer, UserName, Connected?, and Suspect? Thanks you for any assistance you may be able to provide.

This is exactly how I would incorporate this functionality in my Database:
  1. Create a Form dedicated solely to the display of this User List. It should not serve any other function.
  2. Create a Private Sub Procedure in your Form and name it GenerateUserList. Copy and Paste the User List code to this Procedure as in:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub GenerateUserList()
    2. 'The User List Schema information requires this magic number. For anyone
    3. 'who may be interested, this number is called a GUID or Globally Unique
    4. 'Identifier - sorry for digressing
    5. Const conUsers = "{947bb102-5d43-11d1-bdbf-00c04fb92675}"
    6.  
    7. Dim cnn As ADODB.Connection, fld As ADODB.Field, strUser As String
    8. Dim rst As ADODB.Recordset, intUser As Integer, varValue As Variant
    9.  
    10. Set cnn = CurrentProject.Connection
    11. Set rst = cnn.OpenSchema(Schema:=adSchemaProviderSpecific, SchemaID:=conUsers)
    12.  
    13. 'Set List Box Heading
    14. strUser = "Computer;UserName;Connected?;Suspect?"
    15.  
    16. With rst    'fills Recordset (rst) with User List data
    17.   Do Until .EOF
    18.     intUser = intUser + 1
    19.       For Each fld In .Fields
    20.         varValue = fld.Value
    21.           'Some of the return values are Null-Terminated Strings, if
    22.           'so strip them off
    23.           If InStr(varValue, vbNullChar) > 0 Then
    24.             varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
    25.           End If
    26.           strUser = strUser & ";" & varValue
    27.       Next
    28.         .MoveNext
    29.   Loop
    30. End With
    31.  
    32. Me!txtTotalNumOfUsers = intUser        'Total # of Users
    33.  
    34. 'Set up List Box Parameters
    35. Me!lstUsers.ColumnCount = 4
    36. Me!lstUsers.RowSourceType = "Value List"
    37. Me!lstUsers.ColumnHeads = False
    38.   lstUsers.RowSource = strUser       'populate the List Box
    39.  
    40. 'Routine cleanup chores
    41. Set fld = Nothing
    42. Set rst = Nothing
    43. Set cnn = Nothing
    44. End Sub
  3. Create a List Box on this Form and name it lstUsers. Set no properties for this List Box, they will be set programmatically. It's Name, however, must be lstUsers.
  4. Create a Text Box on the Form and name it txtTotalNumOfUsers.
  5. Set the Form's Timer Interval to 10000 (10 seconds). This can be readjusted later if you so desire.
  6. Place the following code in the Form's Timer() Event:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Timer()
    2.   Call GenerateUserList
    3. End Sub
    4.  
  7. Place the following code in the Form's Open() Event:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Open(Cancel As Integer)
    2.   Call GenerateUserList
    3. End Sub
  8. When you initially Open the Form the List Box will be populate with all the Users currently logged on to the Database and this List will be Refreshed every 10 seconds (Timer Interval = 10000 / Timer() Event call to GenerateUserList).
  9. If you have any further questions whatsoever on anything that I have outlined, please feel free to ask.
Newbie
 
Join Date: Dec 2007
Posts: 1
#6   Dec 6 '07

re: How to Generate a User List


I was having trouble implementing this on my front end but then I figured it out! It has to be on the back end for this to work.

Thanks,
Chris
Newbie
 
Join Date: May 2007
Posts: 27
#7   Dec 20 '07

re: How to Generate a User List


This works great, thanks. But, why are all the UserNames "Admin"?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#8   Dec 20 '07

re: How to Generate a User List


Quote:

Originally Posted by Randoz

This works great, thanks. But, why are all the UserNames "Admin"?

If you do not have a Security System in place all Users are defined as Admin with no Password. I'm assuming that this is your case.
Newbie
 
Join Date: May 2007
Posts: 27
#9   Dec 21 '07

re: How to Generate a User List


Quote:

Originally Posted by ADezii

If you do not have a Security System in place all Users are defined as Admin with no Password. I'm assuming that this is your case.

Yes, that is the case, thanks for the help. At least I can get the computer name.
zepphead80's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: New York City
Posts: 152
#10   Feb 14 '08

re: How to Generate a User List


This works great, except that in addition to getting "Admin" for all the UserNames, I get something like "C05-4235" under Computer.

We use Novell NetWare Services to log in to our computers. Is there anyway that I can get it to display the Novell user name in the list box?

Thanks!
Member
 
Join Date: Mar 2008
Location: Bangalore, India
Posts: 47
#11   Apr 22 '08

re: How to Generate a User List


Hi there,

i created a macro wher in it pulls the data from an excel sheet (a report).

i got the data, but the problem is the output is scattered..

can anyone gimme a code to get the outputs aligned?

thanks
Newbie
 
Join Date: May 2009
Location: UK
Posts: 5
#12   May 5 '09

re: How to Generate a User List


Quote:

Originally Posted by zepphead80 View Post

This works great, except that in addition to getting "Admin" for all the UserNames, I get something like "C05-4235" under Computer.

We use Novell NetWare Services to log in to our computers. Is there anyway that I can get it to display the Novell user name in the list box?

Thanks!


Don't know if you still need this, but my project is the same, and with a bit of fiddling, I've got it sort of working. Huge thanks to ADezii for the original code.

The bits in bold are additional

Expand|Select|Wrap|Line Numbers
  1. 'Set List Box Heading
  2. strUser = "Computer;User;Connected?;Suspect?;ID"
  3.  
  4. With rst    'fills Recordset (rst) with User List data
  5.   Do Until .EOF
  6.     intUser = intUser + 1
  7.       For Each fld In .Fields
  8.         varValue = fld.Value
  9.           'Some of the return values are Null-Terminated Strings, if
  10.           'so strip them off
  11.           If InStr(varValue, vbNullChar) > 0 Then
  12.             varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
  13.           End If
  14.           strUser = strUser & ";" & varValue
  15.                 Next
  16.       strUser = strUser & ";" & GetUserName()
  17.         .MoveNext
  18.   Loop
  19. End With
  20. Me!txtTotalNumOfUsers = intUser        'Total # of Users
  21.  
  22. 'Set up List Box Parameters
  23. Me!lstUsers.ColumnCount = 5
  24. Me!lstUsers.RowSourceType = "Value List"
  25. Me!lstUsers.ColumnHeads = True
  26.   lstUsers.RowSource = strUser     'populate the List Box
It does leave it slightly messy with the Admin user still being displayed, but that doesn't matter for my purposes, so I've left it in.

The GetUserName() function I found elsewhere on the web, but not sure where, so can't acknowledge the original creator, however, it's not mine.

Expand|Select|Wrap|Line Numbers
  1. Public Function GetUserName()
  2.  
  3.       ' Buffer size for the return string.
  4.       Const lpnLength As Integer = 255
  5.  
  6.       ' Get return buffer space.
  7.       Dim status As Integer
  8.  
  9.  
  10.       ' For getting user information.
  11.       Dim lpName, lpUserName As String
  12.  
  13.       ' Assign the buffer size constant to lpUserName.
  14.       lpUserName = Space$(lpnLength + 1)
  15.  
  16.       ' Get the log-on name of the person using product.
  17.       status = WNetGetUser(lpName, lpUserName, lpnLength)
  18.  
  19.       ' See whether error occurred.
  20.       If status = NoError Then
  21.          ' This line removes the null character. Strings in C are null-
  22.          ' terminated. Strings in Visual Basic are not null-terminated.
  23.          ' The null character must be removed from the C strings to be used
  24.          ' cleanly in Visual Basic.
  25.          lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
  26.         GetUserName = lpUserName
  27.         Exit Function
  28.       Else
  29.  
  30.          ' An error occurred.
  31.          MsgBox "Unable to get the name."
  32.          Exit Function
  33.          End
  34.       End If
  35.  
  36.       ' Display the name of the person logged on to the machine.
  37.       MsgBox "The person logged on this machine is: " & lpUserName
  38.    End Function
Hope that helps.
Reply