Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: User name


Message #1 by "Stephen Carpenter" <stvc69@n...> on Wed, 12 Dec 2001 20:58:43
Using Win NT - I would like to be able to display the users name when 

they open my database.  Users log on to network with an id.  Can someone 

help me how to do this - thanks.
Message #2 by Walt Morgan <wmorgan@s...> on Wed, 12 Dec 2001 14:59:46 -0600
This was provided by Roy Pardee in an earlier message:







I can think of three ways to programatically find out the userid, for use in

sql inserts, default values, etc..



The easiest only applies of you're using a secured database--the

CurrentUser() function returns the ID of the current user.



The second easiest requires that all your users have an appropriate

environment variable set, in which case you can find it out with the

Environ() function, e.g. strUserName = Environ("USERNAME").



The third easiest requires that you set a reference to the windows script

host object model (wshom.ocx) and use code like so:



Dim Network As IWshRuntimeLibrary.WshNetwork

Dim strUserName As String



   Set Network = New IWshRuntimeLibrary.WshNetwork

   strUserName = Network.UserName



There are probably also API calls, but I don't know what they are.



HTH,



-Roy



Roy Pardee

Programmer/Analyst

SWFPAC Lockheed Martin IT

(xxx) xxx-xxxx







Message #3 by "Hamilton, Tom" <hamiltot@s...> on Wed, 12 Dec 2001 13:58:00 -0800
Add these lines to the declaration section:



Declare Function GetComputerName& Lib "kernel32.dll" _

                 Alias "GetComputerNameA" (ByVal lpBuffer As String, _

                 nSize As Long)                                         '

Network CPU Name



Declare Function GetUserName& Lib "advapi32.dll" _

                 Alias "GetUserNameA" (ByVal lpBuffer As String, _

                 nSize As Long)                                         '

Network login name



Public Const MAX_COMPUTERNAME_LENGTH = 15                   ' Varies with

platform OS



Then use these functions to return the Network User log in, or the second

function which returns  the network Computer ID which can be useful also. 



Function sGetUser() As String                             ' Returns current

network logon

'   Usage:      ="Network User Name: " & sGetUser()

'

    Dim s$, cnt&, dl&, sWho As String

    cnt& = 199

    s$ = String$(200, 0)

    dl& = GetUserName(s$, cnt)

    sGetUser = Left$(s$, cnt - 1)

End Function





Function sGetComputer()                                   ' Returns Computer

Name on the network 

'   Usage:      ="Computer Name: " & sGetComputer()

    Dim s$

    s$ = String$(MAX_COMPUTERNAME_LENGTH + 1, 0)

    Dim dl&

    Dim sz&

    sz& = MAX_COMPUTERNAME_LENGTH + 1

    dl& = GetComputerName(s$, sz)

    sGetComputer = Left$(s$, sz)

End Function







Tom Hamilton

T_Systems, Inc

Database Programmer

(xxx) xxx-xxxx



>>> Stephen Carpenter 12/12/01 12:58PM >>>

Using Win NT - I would like to be able to display the users name when 

they open my database.  Users log on to network with an id.  Can someone 

help me how to do this - thanks.






Message #4 by "Paul McLaren" <paulmcl@t...> on Wed, 12 Dec 2001 23:41:25
> Using Win NT - I would like to be able to display the users name when 

> they open my database.  Users log on to network with an id.  Can someone 

> help me how to do this - thanks.



Another API call is as follows, use it on the security form or whatever 

one your database opens first:



Option Compare Database

Option Explicit

Private Declare Function GetUserName Lib "advapi32.dll" 

Alias "GetUserNameA"

(ByVal lpBuffer As String, nSize As Long) As Long

Dim CurrentUser As String



Property Get LanUser()

LanUser = CurrentUser

End Property



Private Sub Form_Open(Cancel As Integer)

On Error GoTo trap



Dim CurrentUser As String

CurrentUser = Space(30)

GetUserName CurrentUser, 30



The current logged on username is passed to the variable CurrentUser and 

can be used to populate your form as required.



Regards



Paul McLaren



Message #5 by "Stephen Carpenter" <stvc69@n...> on Thu, 13 Dec 2001 20:03:34
> Using Win NT - I would like to be able to display the users name when 

> they open my database.  Users log on to network with an id.  Can 

someone 

> help me how to do this - thanks.



Big massive thanks to Walt, Tom and Paul for helping me out!!!

  Return to Index