Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: How to get NT User ID in Access


Message #1 by "boyce, hr" <boycehr@h...> on Sun, 25 Mar 2001 04:02:01
I would like to find a way to get the User ID on the current logged in 

user for an Access form.  I want to pass it as part of a parameter list to 

a SQL Server stored procedure I am calling using ODBC direct.



I know it would be easy to just as the user to retype it but having the 

capability to use an authenticated user ID ups the security for the app 

significantly.  



Any help would be greatly appreciated, I am stumped.

bhr



Message #2 by "Pardee, Roy E" <roy.e.pardee@l...> on Mon, 26 Mar 2001 09:27:05 -0800
If you are having your users log into Access (e.g., using workgroup

security) and their access usernames are the same as their NT usernames, you

can use the CurrentUser() function.



If that's no good, but you can count on there being an environment variable

set that holds an accurate username value, you can use the Environ()

function to get at the variable (e.g., Environ("USERNAME")).



If *that* fails you, you can use the windows script host to get at the

username.  To do so, set a reference to the windows script host object model

(Tools -> References -> Browse... and navigate over to WSHOM.ocx in your

system32 directory).  Then get at the username with code like so:



' =================================

Public Function WhoAmI() As String

Dim net As IWshNetwork

Set net = New IWshNetwork_Class



   WhoAmI = net.UserName



Set net = Nothing



End Function

' =================================



The down side here is that you'll have to make sure that the windows script

host is on all your users' machines.  I believe that WSH is part of the core

load of internet explorer 4.0 and up, so that may not be a problem...



HTH,



-Roy





-----Original Message-----

From: boyce, hr [mailto:boycehr@h...]

Sent: Saturday, March 24, 2001 8:01 PM

To: Access

Subject: [access] How to get NT User ID in Access





I would like to find a way to get the User ID on the current logged in 

user for an Access form.  I want to pass it as part of a parameter list to 

a SQL Server stored procedure I am calling using ODBC direct.



I know it would be easy to just as the user to retype it but having the 

capability to use an authenticated user ID ups the security for the app 

significantly.  



Any help would be greatly appreciated, I am stumped.

bhr



Message #3 by "Derrick Flores" <Derrick_Flores@s...> on Mon, 26 Mar 2001 16:36:24 -0600
Here's another way you can find out somethings about your user and the cpu 

he is using.  This

is a working Class Module that I have that you can copy and use for 

yourself.



Option Compare Database

Public myUser As String

Option Explicit



'*************************************************************************

'* INTERFACE                                                             *

'*************************************************************************



'Public collections

Public Drives As New Collection





'*************************************************************************

'* IMPLEMENTATION                                                        *

'*************************************************************************



'Windows structure declarations

Private Type SYSTEM_INFO

    dwOemID As Long

    dwPageSize As Long

    lpMinimumApplicationAddress As Long

    lpMaximumApplicationAddress As Long

    dwActiveProcessorMask As Long

    dwNumberOrfProcessors As Long

    dwProcessorType As Long

    dwAllocationGranularity As Long

    wProcessorLevel As Integer

    wProcessorRevision As Integer

End Type





Private Type OSVERSION_INFO

    dwOSVersionInfoSize As Long

    dwMajorVersion As Long

    dwMinorVersion As Long

    dwBuildNumber As Long

    dwPlatformId As Long

    szCSDVersion As String * 128

End Type



Private Type RECT

    x1 As Long

    y1 As Long

    x2 As Long

    y2 As Long

End Type



'Windows API function declarations

Private Declare Function SwapMouseButton Lib "User32" (ByVal bSwap As 

Long) As Long

Private Declare Function GetSystemMetrics Lib "User32" (ByVal nIndex As 

Long) As Long

Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As 

SYSTEM_INFO)

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" 

_

        (lpVersionInformation As OSVERSION_INFO) As Long

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerN

ameA" _

        (ByVal sBuffer As String, lSize As Long) As Long

Private Declare Function GetUserName Lib "advapi32" Alias "GetUserNameA" _

        (ByVal sBuffer As String, lSize As Long) As Long

Private Declare Function timeGetTime Lib "winmm" () As Long

Private Declare Function GetDesktopWindow Lib "User32" () As Long

Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd As Long, 

rectangle As RECT) As Long

Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias 

"GetLogicalDriveStringsA" _

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

 



'Windows constant declarations

Private Const SM_SWAPBUTTON =3D 23

Private Const PROCESSOR_INTEL_386 =3D 386

Private Const PROCESSOR_INTEL_486 =3D 486

Private Const PROCESSOR_INTEL_PENTIUM =3D 586

Private Const PROCESSOR_MIPS_R4000 =3D 4000

Private Const PROCESSOR_ALPHA_21064 =3D 21064

Private Const PROCESSOR_ARCHITECTURE_INTEL =3D 0

Private Const PROCESSOR_ARCHITECTURE_MIPS =3D 1

Private Const PROCESSOR_ARCHITECTURE_ALPHA =3D 2

Private Const PROCESSOR_ARCHITECTURE_PPC =3D 3

Private Const PROCESSOR_ARCHITECTURE_UNKNOWN =3D &HFFFF

Private Const VER_PLATFORM_WIN32s =3D 0

Private Const VER_PLATFORM_WIN32_WINDOWS =3D 1

Private Const VER_PLATFORM_WIN32_NT =3D 2



'Private variable declarations

Private strAccessDir As String

Private strComputerName As String

Private strUserName As String

Private typSysInfo As SYSTEM_INFO

Private typOSVersion As OSVERSION_INFO

Private lngMemoryInstalled As Long

Private lngID As Long



Private Sub Class_Initialize()

'**************************************************************************

***

'* NAME:    CLASS_INITIALIZE

'* PURPOSE: To initialize variables when MyComputer object is instantiated

'* ACCEPTS: Nothing

'* RETURNS: Nothing

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   This procedure also populates the Disks collection

'**************************************************************************

***



Dim strDriveString As String

Dim intDriveLength As Integer

Dim strDrive() As String

Dim intDriveCount As Integer

Dim intPos As Integer

Dim intCounter As Integer

Dim lngBuffSize As Long

'Dim typDrive As Drive



'Get OS version information

typOSVersion.dwOSVersionInfoSize =3D Len(typOSVersion)

GetVersionEx typOSVersion



'Get system information (e.g. processor type)

GetSystemInfo typSysInfo



'Get Access directory

strAccessDir =3D SysCmd(acSysCmdAccessDir)



'Get computer name

strComputerName =3D Space$(255)

lngBuffSize =3D Len(strComputerName)

If GetComputerName(strComputerName, lngBuffSize) Then

    strComputerName =3D Left$(strComputerName, lngBuffSize)

End If



'Get user name

strUserName =3D Space$(255)

lngBuffSize =3D Len(strUserName)

If GetUserName(strUserName, lngBuffSize) Then

    strUserName =3D Left$(strUserName, lngBuffSize - 1)

    myUser =3D strUserName

End If





End Sub



Public Property Get AccessDir() As String

'**************************************************************************

***

'* NAME:    ACCESSDIR

'* PURPOSE: To allow the AccessDir property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the directory in which Access is installed

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***



AccessDir =3D strAccessDir



End Property



Public Property Get ButtonsSwapped() As Boolean

'**************************************************************************

***

'* NAME:    BUTTONSSWAPPED

'* PURPOSE: To allow the ButtonsSwapped property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: False if the mouse is configured for left-handed use

'*          True (non-zero) otherwise

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***



ButtonsSwapped =3D GetSystemMetrics(SM_SWAPBUTTON)



End Property



Public Property Let ButtonsSwapped(bSwap As Boolean)

'**************************************************************************

***

'* NAME:    BUTTONSSWAPPED

'* PURPOSE: To allow the ButtonsSwapped property to be set

'* ACCEPTS: True (non-zero) if the mouse is to be configured for left-hande

d use

'           False if the mouse is to be configured for (default) right-hand

ed use

'* RETURNS: Nothing

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***



Dim lngSwap As Long



lngSwap =3D CLng(bSwap)

Call SwapMouseButton(lngSwap)



End Property



Public Sub SwapButtons(bSwap As Boolean)

'**************************************************************************

***

'* NAME:    SWAPBUTTONS

'* PURPOSE: To expose the SwapButtons method

'* ACCEPTS: True (non-zero) if the mouse is to be configured for left-hande

d use

'           False if the mouse is to be configured for (default) right-hand

ed use

'* RETURNS: Nothing

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   This provides identical functionality to the ButtonsSwapped 

property

'**************************************************************************

***



SwapMouseButton bSwap



End Sub



Public Function SwapButtonCheck(bSwap As Boolean) As Boolean

'**************************************************************************

***

'* NAME:    SWAPBUTTONCHECK

'* PURPOSE: To allow the ButtonsSwapped property to be set and determine 

whether

'*          this results in a changed mouse configuration

'* ACCEPTS: True (non-zero) if the mouse is to be configured for left-hande

d use

'           False if the mouse is to be configured for (default) right-hand

ed use

'* RETURNS: True if the mouse configuration changes as a result of this 

call;

'*          False otherwise

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***



Dim bNewState As Boolean



bNewState =3D SwapMouseButton(bSwap)

If bNewState =3D bSwap Then

    SwapButtonCheck =3D False

Else

    SwapButtonCheck =3D True

End If



End Function



Public Property Get CPUArchitecture() As String

'**************************************************************************

***

'* NAME:    CPUARCHITECTURE

'* PURPOSE: To allow the CPUArchitecture property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the type of processor installed

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   FOr Win95, this will always be "Intel"

'**************************************************************************

***



Select Case typSysInfo.dwOemID

    Case PROCESSOR_ARCHITECTURE_INTEL

        CPUArchitecture =3D "Intel"

    Case PROCESSOR_ARCHITECTURE_MIPS

        CPUArchitecture =3D "MIPS"

    Case PROCESSOR_ARCHITECTURE_ALPHA

        CPUArchitecture =3D "Alpha"

    Case PROCESSOR_ARCHITECTURE_PPC

        CPUArchitecture =3D "PowerPC"

    Case PROCESSOR_ARCHITECTURE_UNKNOWN

        CPUArchitecture =3D "Unknown"

End Select



End Property



Public Property Get OSName() As String

'**************************************************************************

***

'* NAME:    OSNAME

'* PURPOSE: To allow the OSName property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the name of the operating system installed

'*          e.g. Windows NT 4.0 Build 1381

'*               ~~~~~~~~~~

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   Further information is in the OSVersion and OSBuild properties

'**************************************************************************

***



Select Case typOSVersion.dwPlatformId

    Case VER_PLATFORM_WIN32_WINDOWS

       OSName =3D "Windows 95"

    Case VER_PLATFORM_WIN32_NT

       OSName =3D "Windows NT"

End Select



End Property



Public Property Get OSVersion() As String

'**************************************************************************

***

'* NAME:    OSVERSION

'* PURPOSE: To allow the OSVersion property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the version of the operating system 

installed

'*          e.g. Windows NT 4.0 Build 1381

'*                          ~~~

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   Further information is in the OSVersion and OSBuild properties

'**************************************************************************

***



OSVersion =3D typOSVersion.dwMajorVersion & "." & typOSVersion.dwMinorVersi

on



End Property



Public Property Get OSBuild() As String

'**************************************************************************

***

'* NAME:    OSBUILD

'* PURPOSE: To allow the OSBuild property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the build of the operating system 

installed

'*          e.g. Windows NT 4.0 Build 1381

'*                                    ~~~~

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   Further information is in the OSVersion and OSBuild properties

'**************************************************************************

***



OSBuild =3D typOSVersion.dwBuildNumber And &HFFFF&



End Property



Public Property Get CPULevel() As String

'**************************************************************************

***

'* NAME:    CPULEVEL

'* PURPOSE: To allow the CPULevel property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the level of CPU installed

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   If the operating system is Win95, this value is found in the

'           dwProcessorType element returned by GetSystemInfo. If WinNT,

'           the value is in the wProcessorLevel element, and values 

returned

'           are dependent on processor architecture

'**************************************************************************

***



Select Case typOSVersion.dwPlatformId

    Case VER_PLATFORM_WIN32_WINDOWS

        Select Case typSysInfo.dwProcessorType

            Case PROCESSOR_INTEL_386

                CPULevel =3D "386"

            Case PROCESSOR_INTEL_486

                CPULevel =3D "486"

            Case PROCESSOR_INTEL_PENTIUM

                CPULevel =3D "Pentium"

        End Select

    Case VER_PLATFORM_WIN32_NT

        Select Case typSysInfo.dwOemID

            Case PROCESSOR_ARCHITECTURE_INTEL

                Select Case typSysInfo.wProcessorLevel

                    Case 3

                        CPULevel =3D "386"

                    Case 4

                        CPULevel =3D "486"

                    Case 5

                        CPULevel =3D "Pentium"

                End Select

            Case PROCESSOR_ARCHITECTURE_MIPS

                Select Case typSysInfo.wProcessorLevel

                    Case 4

                        CPULevel =3D "R4000"

                End Select

            Case PROCESSOR_ARCHITECTURE_ALPHA

                Select Case typSysInfo.wProcessorLevel

                    Case 21064

                        CPULevel =3D "21064"

                    Case 21066

                        CPULevel =3D "21066"

                    Case 21164

                        CPULevel =3D "21164"

                End Select

            Case PROCESSOR_ARCHITECTURE_PPC

                Select Case typSysInfo.wProcessorLevel

                    Case 1

                        CPULevel =3D "601"

                    Case 3

                        CPULevel =3D "603"

                    Case 4

                        CPULevel =3D "604"

                    Case 6

                        CPULevel =3D "603+"

                    Case 9

                        CPULevel =3D "604+"

                    Case 20

                        CPULevel =3D "620"

                End Select

            Case PROCESSOR_ARCHITECTURE_UNKNOWN

                CPULevel =3D "Unknown"

        End Select

End Select



End Property



Public Property Get ComputerName() As String

'**************************************************************************

***

'* NAME:    COMPUTERNAME

'* PURPOSE: To allow the ComputerName property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the name of the computer

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***



ComputerName =3D strComputerName



End Property



Public Property Get UserName() As String

'**************************************************************************

***

'* NAME:    USERNAME

'* PURPOSE: To allow the UserName property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the name of the currently logged-on user

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***



UserName =3D strUserName



End Property



Public Property Get WinStarted() As Date

'**************************************************************************

***

'* NAME:    WINSTARTED

'* PURPOSE: To allow the WinStarted property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A date indicating when Windows was last started

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:   This value will not be accurate once the threshold of 

timeGetTime

'           has been exceeded (around 47 days)

'**************************************************************************

***



WinStarted =3D DateAdd("s", timeGetTime() / -1000, Now)



End Property



Public Property Get ScreenResolution() As String

'**************************************************************************

***

'* NAME:    SCREENRESOLUTION

'* PURPOSE: To allow the ScreenResolution property to be inspected

'* ACCEPTS: Nothing

'* RETURNS: A string indicating the current screen resolution (e.g. 

640x480)

'* AUTHOR:  Rob Smith

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***



Dim rct As RECT

Dim hWnd As Long

Dim lngReturn As Long



hWnd =3D GetDesktopWindow()

lngReturn =3D GetWindowRect(hWnd, rct)



ScreenResolution =3D (rct.x2 - rct.x1) & "x" & (rct.y2 - rct.y1)





End Property



Public Property Get ID() As Long

'**************************************************************************

***

'* NAME:    ID

'* PURPOSE: To return the unique ID of this class instance

'* ACCEPTS: Nothing

'* RETURNS: A long representing a unique(-ish?) ID

'* AUTHOR:  David Sussman

'* DATE:    22/06/97

'* NOTES:

'**************************************************************************

***

   

    ID =3D lngID



End Property











>>> "Pardee, Roy E" <roy.e.pardee@l...> 03/26 11:27 AM >>>

If you are having your users log into Access (e.g., using workgroup

security) and their access usernames are the same as their NT usernames, 

you

can use the CurrentUser() function.



If that's no good, but you can count on there being an environment 

variable

set that holds an accurate username value, you can use the Environ()

function to get at the variable (e.g., Environ("USERNAME")).



If *that* fails you, you can use the windows script host to get at the

username.  To do so, set a reference to the windows script host object 

model

(Tools -> References -> Browse... and navigate over to WSHOM.ocx in your

system32 directory).  Then get at the username with code like so:



' =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

=3D=3D=3D=3D=3D=3D=3D=3D=3D

Public Function WhoAmI() As String

Dim net As IWshNetwork

Set net =3D New IWshNetwork_Class



   WhoAmI =3D net.UserName



Set net =3D Nothing



End Function

' =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

=3D=3D=3D=3D=3D=3D=3D=3D=3D



The down side here is that you'll have to make sure that the windows 

script

host is on all your users' machines.  I believe that WSH is part of the 

core

load of internet explorer 4.0 and up, so that may not be a problem...



HTH,



-Roy





-----Original Message-----

From: boyce, hr [mailto:boycehr@h...]

Sent: Saturday, March 24, 2001 8:01 PM

To: Access

Subject: [access] How to get NT User ID in Access





I would like to find a way to get the User ID on the current logged in

user for an Access form.  I want to pass it as part of a parameter list 

to

a SQL Server stored procedure I am calling using ODBC direct.



I know it would be easy to just as the user to retype it but having the

capability to use an authenticated user ID ups the security for the app

significantly. 



Any help would be greatly appreciated, I am stumped.

bhr




  Return to Index