How to Generate a User List  | 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. -
'The User List Schema information requires this magic number. For anyone
-
'who may be interested, this number is called a GUID or Globally Unique
-
'Identifier - sorry for digressing
-
Const conUsers = "{947bb102-5d43-11d1-bdbf-00c04fb92675}"
-
-
Dim cnn As ADODB.Connection, fld As ADODB.Field, strUser As String
-
Dim rst As ADODB.Recordset, intUser As Integer, varValue As Variant
-
-
Set cnn = CurrentProject.Connection
-
Set rst = cnn.OpenSchema(Schema:=adSchemaProviderSpecific, SchemaID:=conUsers)
-
-
'Set List Box Heading
-
strUser = "Computer;UserName;Connected?;Suspect?"
-
-
With rst 'fills Recordset (rst) with User List data
-
Do Until .EOF
-
intUser = intUser + 1
-
For Each fld In .Fields
-
varValue = fld.Value
-
'Some of the return values are Null-Terminated Strings, if
-
'so strip them off
-
If InStr(varValue, vbNullChar) > 0 Then
-
varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
-
End If
-
strUser = strUser & ";" & varValue
-
Next
-
.MoveNext
-
Loop
-
End With
-
-
Me!txtTotalNumOfUsers = intUser 'Total # of Users
-
-
'Set up List Box Parameters
-
Me!lstUsers.ColumnCount = 4
-
Me!lstUsers.RowSourceType = "Value List"
-
Me!lstUsers.ColumnHeads = False
-
lstUsers.RowSource = strUser 'populate the List Box
-
-
'Routine cleanup chores
-
Set fld = Nothing
-
Set rst = Nothing
-
Set cnn = Nothing
OUTPUT: -
Computer UserName Connected? Suspect?
-
DEZII Admin True
-
IGUANA Suzanne True
-
LIZARD Paul True
-
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?
|  | 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. -
'The User List Schema information requires this magic number. For anyone
-
'who may be interested, this number is called a GUID or Globally Unique
-
'Identifier - sorry for digressing
-
Const conUsers = "{947bb102-5d43-11d1-bdbf-00c04fb92675}"
-
-
Dim cnn As ADODB.Connection, fld As ADODB.Field, strUser As String
-
Dim rst As ADODB.Recordset, intUser As Integer, varValue As Variant
-
-
Set cnn = CurrentProject.Connection
-
Set rst = cnn.OpenSchema(Schema:=adSchemaProviderSpecific, SchemaID:=conUsers)
-
-
'Set List Box Heading
-
strUser = "Computer;UserName;Connected?;Suspect?"
-
-
With rst 'fills Recordset (rst) with User List data
-
Do Until .EOF
-
intUser = intUser + 1
-
For Each fld In .Fields
-
varValue = fld.Value
-
'Some of the return values are Null-Terminated Strings, if
-
'so strip them off
-
If InStr(varValue, vbNullChar) > 0 Then
-
varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
-
End If
-
strUser = strUser & ";" & varValue
-
Next
-
.MoveNext
-
Loop
-
End With
-
-
Me!txtTotalNumOfUsers = intUser 'Total # of Users
-
-
'Set up List Box Parameters
-
Me!lstUsers.ColumnCount = 4
-
Me!lstUsers.RowSourceType = "Value List"
-
Me!lstUsers.ColumnHeads = False
-
lstUsers.RowSource = strUser 'populate the List Box
-
-
'Routine cleanup chores
-
Set fld = Nothing
-
Set rst = Nothing
-
Set cnn = Nothing
OUTPUT: -
Computer UserName Connected? Suspect?
-
DEZII Admin True
-
IGUANA Suzanne True
-
LIZARD Paul True
-
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.
|  | 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:- Create a Form dedicated solely to the display of this User List. It should not serve any other function.
- Create a Private Sub Procedure in your Form and name it GenerateUserList. Copy and Paste the User List code to this Procedure as in:
- Private Sub GenerateUserList()
-
'The User List Schema information requires this magic number. For anyone
-
'who may be interested, this number is called a GUID or Globally Unique
-
'Identifier - sorry for digressing
-
Const conUsers = "{947bb102-5d43-11d1-bdbf-00c04fb92675}"
-
-
Dim cnn As ADODB.Connection, fld As ADODB.Field, strUser As String
-
Dim rst As ADODB.Recordset, intUser As Integer, varValue As Variant
-
-
Set cnn = CurrentProject.Connection
-
Set rst = cnn.OpenSchema(Schema:=adSchemaProviderSpecific, SchemaID:=conUsers)
-
-
'Set List Box Heading
-
strUser = "Computer;UserName;Connected?;Suspect?"
-
-
With rst 'fills Recordset (rst) with User List data
-
Do Until .EOF
-
intUser = intUser + 1
-
For Each fld In .Fields
-
varValue = fld.Value
-
'Some of the return values are Null-Terminated Strings, if
-
'so strip them off
-
If InStr(varValue, vbNullChar) > 0 Then
-
varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
-
End If
-
strUser = strUser & ";" & varValue
-
Next
-
.MoveNext
-
Loop
-
End With
-
-
Me!txtTotalNumOfUsers = intUser 'Total # of Users
-
-
'Set up List Box Parameters
-
Me!lstUsers.ColumnCount = 4
-
Me!lstUsers.RowSourceType = "Value List"
-
Me!lstUsers.ColumnHeads = False
-
lstUsers.RowSource = strUser 'populate the List Box
-
-
'Routine cleanup chores
-
Set fld = Nothing
-
Set rst = Nothing
-
Set cnn = Nothing
-
End Sub
- 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.
- Create a Text Box on the Form and name it txtTotalNumOfUsers.
- Set the Form's Timer Interval to 10000 (10 seconds). This can be readjusted later if you so desire.
- Place the following code in the Form's Timer() Event:
-
Private Sub Form_Timer()
-
Call GenerateUserList
-
End Sub
-
- Place the following code in the Form's Open() Event:
- Private Sub Form_Open(Cancel As Integer)
-
Call GenerateUserList
-
End Sub
- 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).
- 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"?
|  | 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.
|  | 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 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 - 'Set List Box Heading
-
strUser = "Computer;User;Connected?;Suspect?;ID"
-
-
With rst 'fills Recordset (rst) with User List data
-
Do Until .EOF
-
intUser = intUser + 1
-
For Each fld In .Fields
-
varValue = fld.Value
-
'Some of the return values are Null-Terminated Strings, if
-
'so strip them off
-
If InStr(varValue, vbNullChar) > 0 Then
-
varValue = Left(varValue, InStr(varValue, vbNullChar) - 1)
-
End If
-
strUser = strUser & ";" & varValue
-
Next
-
strUser = strUser & ";" & GetUserName()
-
.MoveNext
-
Loop
-
End With
-
Me!txtTotalNumOfUsers = intUser 'Total # of Users
-
-
'Set up List Box Parameters
-
Me!lstUsers.ColumnCount = 5
-
Me!lstUsers.RowSourceType = "Value List"
-
Me!lstUsers.ColumnHeads = True
-
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. - Public Function GetUserName()
-
-
' Buffer size for the return string.
-
Const lpnLength As Integer = 255
-
-
' Get return buffer space.
-
Dim status As Integer
-
-
-
' For getting user information.
-
Dim lpName, lpUserName As String
-
-
' Assign the buffer size constant to lpUserName.
-
lpUserName = Space$(lpnLength + 1)
-
-
' Get the log-on name of the person using product.
-
status = WNetGetUser(lpName, lpUserName, lpnLength)
-
-
' See whether error occurred.
-
If status = NoError Then
-
' This line removes the null character. Strings in C are null-
-
' terminated. Strings in Visual Basic are not null-terminated.
-
' The null character must be removed from the C strings to be used
-
' cleanly in Visual Basic.
-
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
-
GetUserName = lpUserName
-
Exit Function
-
Else
-
-
' An error occurred.
-
MsgBox "Unable to get the name."
-
Exit Function
-
End
-
End If
-
-
' Display the name of the person logged on to the machine.
-
MsgBox "The person logged on this machine is: " & lpUserName
-
End Function
Hope that helps.
|  | Similar Microsoft Access / VBA bytes | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|