Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

Network User Name

This is a two part question:-)

Part 1: First I am using this code to find out what computeris logged into the database:
********************************************
Function ShowUserRosterMultipleUsers() As String
    Dim cn As New ADODB.Connection
    Dim cn2 As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim i, j As Long

    cn.Provider = "Microsoft.Jet.OLEDB.4.0"
    cn.Open "Data Source=X:\CrdtAdmin\BIFiles\DTS-Deal Tracking.mde"

    cn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=X:\CrdtAdmin\BIFiles\DTS-Deal Tracking.mde"

    ' The user roster is exposed as a provider-specific schema rowset
    ' in the Jet 4 OLE DB provider.  You have to use a GUID to
    ' reference the schema, as provider-specific schemas are not
    ' listed in ADO's type library for schema rowsets

    Set rs = cn.OpenSchema(adSchemaProviderSpecific, _
    , "{947bb102-5d43-11d1-bdbf-00c04fb92675}")

    'Output the list of all users in the current database.

   ShowUserRosterMultipleUsers = rs.Fields(0).Name & "," & rs.Fields(1).Name & "," & rs.Fields(2).Name & "," & rs.Fields(3).Name & vbCrLf

    While Not rs.EOF
        ShowUserRosterMultipleUsers = ShowUserRosterMultipleUsers & rs.Fields(0) & "," & rs.Fields(1) & "," & rs.Fields(2) & "," & rs.Fields(3) & vbCrLf
        rs.MoveNext
    Wend
    'Need to type in ShowUserRosterMultipleUsers to show list of users
   rs.Close
   Set rs = Nothing
End Function

*******************************************************************

Right now this is just dumped into a one text box.  How do I clean it up and divide it up into separate colums.

Part 2:  How do I go about pulling the network ID? in the same manner as above?

0
garland29
Asked:
garland29
  • 2
2 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Use this funciton/API call for Part 2.  Can't help you with Part 1.

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function fn_get_user_name() As String

'Returns the network ID of the person that logged into the machine that is running this Access application:
'This is not the same as the Access security user.

'Visual Basic Source Code Library, The Waite group, Chapter 12.5

On Error GoTo err_fn_get_user_name

Dim str_user_name As String, lng_buffer_size As Long, lng_ret_code As Long, lng_null_character_position As Long

str_user_name = Space(80)
lng_buffer_size = Len(str_user_name)

lng_ret_code = GetUserName(str_user_name, lng_buffer_size)

lng_null_character_position = InStr(str_user_name, Chr(0))
If lng_null_character_position > 0 Then
    str_user_name = left(str_user_name, lng_null_character_position - 1)
Else
    str_user_name = " "
End If

fn_get_user_name = UCase(str_user_name)

exit_fn_get_user_name:
    Exit Function
   
err_fn_get_user_name:
    fn_get_user_name = "Not able to get user name."
    Resume exit_fn_get_user_name
   
End Function
0
 
garland29Author Commented:
Jim,  Does this have to be in the database file that is running?  It's a MDE file and I want to be the only one that can see this.   How do I point it to the database that I want to get the username?
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You will need to dump the above VBA code into a code module.   I haven't tried this code to detect users from a specific database, only the one the code is in, so you'll have to fiture that part out.

Also, if you are dealing with an .mde, then you'll have to find the .mdb it was created from, put the code in there, then compile it as an .mde, and redeploy.
0
 
kemp_aCommented:
This a a HUGE question!

The reason your userlist is a string of data is because of the line:
ShowUserRosterMultipleUsers = ShowUserRosterMultipleUsers & rs.Fields(0) & "," & rs.Fields(1) & "," & rs.Fields(2) & "," & rs.Fields(3) & vbCrLf

Your Function only returns a string delimeted by carriage returns!

You can re-parse the returned string in your front end code if you like, or return a collection/array, but without modifying the current code re-parse the string is better!

Part 2:
What do you mean by "network id", is it the MAC or IP address of the connection? If so, you will have to determine the FieldName/number of the connection in the rs dataset!

So your output string looks like:
ShowUserRosterMultipleUsers = ShowUserRosterMultipleUsers & rs.Fields(0) & "," & rs.Fields(1) & "," & rs.Fields(2) & "," & rs.Fields(3) & "," & rs.fields("Connection") & vbCrLf

NOTE: the "," & rs.fields("Connection") additional field to output for the string to determin the Connection

If the Connection is not available in this recordset, then you will have to try and determine it some other way

Good luck!



0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now