Windows log-on

dan-cat

Registered User.
Local time
Today, 12:42
Joined
Jun 2, 2002
Messages
3,433
Hello,

Is there anyway I can program access to recognise the Windows User Name for the logged-in user. I'm running Windows NT 4.
ie On opening the db - paste the Windows User Name into a table without any interaction from the user?

Many Thanks

Dan
 
(Q) How do I retrieve the UserName with which the user is logged into the network?

(A) Paste the following code in a new module and call the function fOSUserName.


'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If ( lngX > 0 ) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
 
Wow - thanks mark - that worked straight away.
It's going to take me a while to work out what that code actually does though!
 
dan-cat said:
Wow - thanks mark - that worked straight away.
It's going to take me a while to work out what that code actually does though!

The workings of this function is very simple.

The function actually just allocates a string of upto 8 bits (1 byte) of storage (where all bits turned on = 255).
The call is as chewy has mentioned an API call...which are standard functions provided by our friends at microsoft.

The variable strUserName is passed in as a reference to the api function, thus it is modified there and a return code (Long integer) is returned back. Because the variable is passed by reference after it ends the scope inside the API function the object still exists, i.e the variable gets destroyed locally in the function but because it was passed by reference than a pointer or some memory address stores the name. The if condition than simply checks if anything was returned, i.e lngX has at least a bit turned on..if so the function returns that user name. Otherwise the string is assigned as null.

Hope this makes sense.

Jon

PS: Yay for me 1500 posts :D
 
afternoon all,

firstly i apologise for being as thick as mince....vb programming is not my thang.

this thread is exactly what i need. I need to capture the username of the user when they click a checkbox on a form. I've pasted the code from dev avlish into a module called it fOSUserName.
my problem is that i'm too damn thick to know how to pass the username to a field in my form.

anyone willing to help a numpty?

thanks in advance
 
emcf said:
afternoon all,

firstly i apologise for being as thick as mince....vb programming is not my thang.

this thread is exactly what i need. I need to capture the username of the user when they click a checkbox on a form. I've pasted the code from dev avlish into a module called it fOSUserName.
my problem is that i'm too damn thick to know how to pass the username to a field in my form.

anyone willing to help a numpty?

thanks in advance

Either right click on the field and open the properties. In the default value field type fOsUserName (or whatever you called your function).

or...

In vb code type:

Call fOSUserName

Jon
 
numpty-boy here again.....

did what you said jon, problem now is that when i enter the default field value as fOSUserName and run the form the field shows #Name?.

when i tried to use vb code access complained that it was expecting a variable not a module!

anyone know what i'm doing wrong?
 
emcf,
if you are trying to set it from the form itself try:

me.fieldname= currentuser

if your trying to pass it from somewhere else try:

forms![formname]![fieldname]=currentuser

Something else to think about if you havn't done it yet is creating a global variable to pass the CurrentUser value that the function earlier gives you.
 
And also, don't name the module the same name as the function. That's a sure way to have things go wonky.
 
This is the code that was used in one of our databases here at work. Don't know if it will help or make things worse.

_______________________________________________
Option Compare Database
Option Explicit

'********Code Start**************
'This code was originally written by Terry Kreft
' and Dev Ashish.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish & Terry Kreft
'
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006

Private Const STANDARD_RIGHTS_READ = &H20000
Private Const KEY_QUERY_VALUE = &H1&
Private Const KEY_ENUMERATE_SUB_KEYS = &H8&
Private Const KEY_NOTIFY = &H10&
Private Const Synchronize = &H100000
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
KEY_QUERY_VALUE Or _
KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY) And _
(Not Synchronize))
Private Const MAXLEN = 256
Private Const ERROR_SUCCESS = &H0&

Const REG_NONE = 0
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_DWORD_LITTLE_ENDIAN = 4
Const REG_DWORD_BIG_ENDIAN = 5
Const REG_LINK = 6
Const REG_MULTI_SZ = 7
Const REG_RESOURCE_LIST = 8

Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Declare Function apiRegOpenKeyEx Lib "advapi32.dll" _
Alias "RegOpenKeyExA" (ByVal hkey As Long, _
ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, ByRef phkResult As Long) _
As Long

Private Declare Function apiRegCloseKey Lib "advapi32.dll" _
Alias "RegCloseKey" (ByVal hkey As Long) As Long

Private Declare Function apiRegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hkey As Long, _
ByVal lpValueName As String, ByVal lpReserved As Long, _
ByRef lpType As Long, lpData As Any, _
ByRef lpcbData As Long) As Long

Private Declare Function apiRegQueryInfoKey Lib "advapi32.dll" _
Alias "RegQueryInfoKeyA" (ByVal hkey As Long, _
ByVal lpClass As String, ByRef lpcbClass As Long, _
ByVal lpReserved As Long, ByRef lpcSubKeys As Long, _
ByRef lpcbMaxSubKeyLen As Long, _
ByRef lpcbMaxClassLen As Long, _
ByRef lpcValues As Long, _
ByRef lpcbMaxValueNameLen As Long, _
ByRef lpcbMaxValueLen As Long, _
ByRef lpcbSecurityDescriptor As Long, _
ByRef lpftLastWriteTime As FILETIME) As Long

Function fReturnRegKeyValue(ByVal lngKeyToGet As Long, _
ByVal strKeyName As String, _
ByVal strValueName As String) _
As String
Dim lnghKey As Long
Dim strClassName As String
Dim lngClassLen As Long
Dim lngReserved As Long
Dim lngSubKeys As Long
Dim lngMaxSubKeyLen As Long
Dim lngMaxClassLen As Long
Dim lngValues As Long
Dim lngMaxValueNameLen As Long
Dim lngMaxValueLen As Long
Dim lngSecurity As Long
Dim ftLastWrite As FILETIME
Dim lngType As Long
Dim lngData As Long
Dim lngTmp As Long
Dim strRet As String
Dim varRet As Variant
Dim lngRet As Long

On Error GoTo fReturnRegKeyValue_Err

'Open the key first
lngTmp = apiRegOpenKeyEx(lngKeyToGet, _
strKeyName, 0&, KEY_READ, lnghKey)

'Are we ok?
If Not (lngTmp = ERROR_SUCCESS) Then Err.Raise _
lngTmp + vbObjectError

lngReserved = 0&
strClassName = String$(MAXLEN, 0): lngClassLen = MAXLEN

'Get boundary values
lngTmp = apiRegQueryInfoKey(lnghKey, strClassName, _
lngClassLen, lngReserved, lngSubKeys, lngMaxSubKeyLen, _
lngMaxClassLen, lngValues, lngMaxValueNameLen, _
lngMaxValueLen, lngSecurity, ftLastWrite)

'How we doin?
If Not (lngTmp = ERROR_SUCCESS) Then Err.Raise _
lngTmp + vbObjectError

'Now grab the value for the key
strRet = String$(MAXLEN - 1, 0)
lngTmp = apiRegQueryValueEx(lnghKey, strValueName, _
lngReserved, lngType, ByVal strRet, lngData)
Select Case lngType
Case REG_SZ
lngTmp = apiRegQueryValueEx(lnghKey, strValueName, _
lngReserved, lngType, ByVal strRet, lngData)
varRet = Left(strRet, lngData - 1)
Case REG_DWORD
lngTmp = apiRegQueryValueEx(lnghKey, strValueName, _
lngReserved, lngType, lngRet, lngData)
varRet = lngRet
Case REG_BINARY
lngTmp = apiRegQueryValueEx(lnghKey, strValueName, _
lngReserved, lngType, ByVal strRet, lngData)
varRet = Left(strRet, lngData)
End Select

'All quiet on the western front?
If Not (lngTmp = ERROR_SUCCESS) Then Err.Raise _
lngTmp + vbObjectError

fReturnRegKeyValue_Exit:
fReturnRegKeyValue = varRet
lngTmp = apiRegCloseKey(lnghKey)
Exit Function
fReturnRegKeyValue_Err:
varRet = "Error: Key or Value Not Found."
Resume fReturnRegKeyValue_Exit
End Function

'********Code End**************
 

Users who are viewing this thread

Back
Top Bottom