find username

  • Thread starter Thread starter Robgould
  • Start date Start date
R

Robgould

Guest
This is my first post in this forum, sorry if it is not a good one. I have a database that will be shared between several users. I want the database to be able to know who is using it so that I can mark changes with their name. I don't want to have to create a whole security thing for the database, it is at work and everyone is already looged into the network. I just want to be able to determine the logon name that was used to log on to the compueter making changes. Does that make sense? Can anyone help me?
 
Try the following....

Code:
Public Declare Function GetUserName _
    Lib "advapi32.dll" Alias "GetUserNameA" _
    (ByVal lpBuffer As String, nSize As Long) As Long

Public Sub GetUserID()
On Error GoTo Err_GetUserID

    Dim lpBuffer As String * 200
    Dim iEndOfID As Integer
    Dim vlen&
    Dim retval As Long
    
    'lpBuffer = String$(200, 0)
    vlen& = 199
    retval = GetUserName(lpBuffer$, vlen)
    iEndOfID = VBA.InStr(1, lpBuffer$, VBA.Chr(0))
    If iEndOfID Then
        gUser.sID = VBA.Left(lpBuffer$, iEndOfID - 1)
    End If
    
Exit_GetUserID:
    Exit Sub
    
Err_GetUserID:
    Call ErrHandler("GetUserID routine", Err.Number, Err.Description)
    Resume Exit_GetUserID
    
End Sub

I store the results in a User Define Datatype that looks like this...

Code:
Public Type UserData
    sID As String
    sAccessLevel As String
    sUserName As String
    sAccessDescription As String
End Type

I store the network ID in the sID.

-Mike
 
Last edited:
Thanks, I will try it and let you know.
 
Searching the forum is a great way to discover and learn the answers to your Access programming questions.

Why not use the Environ() funtion?!?

It tastes great and it is less filling.

Code:
MsgBox "Network Name = " & Environ("username")
MsgBox "Computer Name = " & Environ("computername")
Search the forum for the keyword ENVIRON and my user name for I have a sample db posted somewhere that will list all of the Environ variables.
 
Last edited:
Ooops, I have a problem. I used the environ thing and it worked great. Using Access 2000 on Winxp. People that try to access the dtabase from win2k boxes still using accesss 200 get errors, and the database crashes. The debugger gives a compile error, can't find project or library. It has the word environ highlighted. Any ideas what I am doing wrong?
 
Are you missing a reference when you check the code under the Win2K boxes?

I typically type the objects and the functions...

Does the following cause an error? Assuming you declare sTest as a string..

sTest = VBA.Environ("USERNAME")
 
That worked. I made a button and added on click code like you said and it worked. is it because of the vba. part?
 
The VBA object didn't hurt.... ;)

Are any references missing when you examine the code on the Win2K boxes?
 
as soon as I changed the code to say vba.environ everywhere that it said environ it works in both xp and 2000. You guys and this forum are awesome. You have helped me in my career....I truly appreciate it!
 

Users who are viewing this thread

Back
Top Bottom