How to Generate a User List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    How to Generate a User List

    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.
    [CODE=vb]
    '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.Connectio n, 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:=adSchem aProviderSpecif ic, SchemaID:=conUs ers)

    'Set List Box Heading
    strUser = "Computer;UserN ame;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!txtTotalNumO fUsers = intUser 'Total # of Users

    'Set up List Box Parameters
    Me!lstUsers.Col umnCount = 4
    Me!lstUsers.Row SourceType = "Value List"
    Me!lstUsers.Col umnHeads = False
    lstUsers.RowSou rce = strUser 'populate the List Box

    'Routine cleanup chores
    Set fld = Nothing
    Set rst = Nothing
    Set cnn = Nothing[/CODE]
    OUTPUT:
    [CODE=text]
    Computer UserName Connected? Suspect?
    DEZII Admin True
    IGUANA Suzanne True
    LIZARD Paul True
    FINANCE Leonard True[/CODE]
  • kentgorrell
    New Member
    • Jul 2007
    • 11

    #2
    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?

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      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.

      Comment

      • jqwert
        New Member
        • Nov 2007
        • 1

        #4
        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.
        [CODE=vb]
        '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.Connectio n, 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:=adSchem aProviderSpecif ic, SchemaID:=conUs ers)

        'Set List Box Heading
        strUser = "Computer;UserN ame;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!txtTotalNumO fUsers = intUser 'Total # of Users

        'Set up List Box Parameters
        Me!lstUsers.Col umnCount = 4
        Me!lstUsers.Row SourceType = "Value List"
        Me!lstUsers.Col umnHeads = False
        lstUsers.RowSou rce = strUser 'populate the List Box

        'Routine cleanup chores
        Set fld = Nothing
        Set rst = Nothing
        Set cnn = Nothing[/CODE]
        OUTPUT:
        [CODE=text]
        Computer UserName Connected? Suspect?
        DEZII Admin True
        IGUANA Suzanne True
        LIZARD Paul True
        FINANCE Leonard True[/CODE]
        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.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          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 GenerateUserLis t. Copy and Paste the User List code to this Procedure as in:
            [CODE=vb]Private Sub GenerateUserLis t()
            '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.Connectio n, 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:=adSchem aProviderSpecif ic, SchemaID:=conUs ers)

            'Set List Box Heading
            strUser = "Computer;UserN ame;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!txtTotalNumO fUsers = intUser 'Total # of Users

            'Set up List Box Parameters
            Me!lstUsers.Col umnCount = 4
            Me!lstUsers.Row SourceType = "Value List"
            Me!lstUsers.Col umnHeads = False
            lstUsers.RowSou rce = strUser 'populate the List Box

            'Routine cleanup chores
            Set fld = Nothing
            Set rst = Nothing
            Set cnn = Nothing
            End Sub[/CODE]
          3. Create a List Box on this Form and name it lstUsers. Set no properties for this List Box, they will be set programmaticall y. It's Name, however, must be lstUsers.
          4. Create a Text Box on the Form and name it txtTotalNumOfUs ers.
          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:
            [CODE=vb]
            Private Sub Form_Timer()
            Call GenerateUserLis t
            End Sub
            [/CODE]
          7. Place the following code in the Form's Open() Event:
            [CODE=vb]Private Sub Form_Open(Cance l As Integer)
            Call GenerateUserLis t
            End Sub[/CODE]
          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 GenerateUserLis t).
          9. If you have any further questions whatsoever on anything that I have outlined, please feel free to ask.

          Comment

          • cford31
            New Member
            • Dec 2007
            • 1

            #6
            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

            Comment

            • Randoz
              New Member
              • May 2007
              • 27

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

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                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.

                Comment

                • Randoz
                  New Member
                  • May 2007
                  • 27

                  #9
                  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.

                  Comment

                  • patjones
                    Recognized Expert Contributor
                    • Jun 2007
                    • 931

                    #10
                    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!

                    Comment

                    • neosam
                      New Member
                      • Mar 2008
                      • 47

                      #11
                      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

                      Comment

                      • MarkH2
                        New Member
                        • May 2009
                        • 5

                        #12
                        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

                        Code:
                        'Set List Box Heading
                        strUser = "Computer;User;Connected?;Suspect?;[B]ID[/B]"
                          
                        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
                              [B]strUser = strUser & ";" & GetUserName()[/B]
                                .MoveNext
                          Loop
                        End With
                        Me!txtTotalNumOfUsers = intUser        'Total # of Users
                          
                        'Set up List Box Parameters
                        Me!lstUsers.ColumnCount = [B]5[/B]
                        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.

                        Code:
                        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.

                        Comment

                        • Stepan
                          New Member
                          • Jun 2011
                          • 1

                          #13
                          Is it possible to generate a similar list with user permissions?

                          Comment

                          • ADezii
                            Recognized Expert Expert
                            • Apr 2006
                            • 8834

                            #14
                            I'm actually on Vacation now, Stephan, but when I return I'm sure that I can drum up some Code for you that will display Permissions for each User. Is this what ou are looking for?

                            Comment

                            • Wesley Hader
                              New Member
                              • Nov 2011
                              • 30

                              #15
                              Can someone please explain how to configure this to connect to my back end database?

                              Comment

                              Working...