How can I retrieve the user id of the user signed on in access. I would like to use this to mark records added or changed by user.
6 14390 NeoPa 32,556
Expert Mod 16PB
I'll try to write this all out again so that it makes sense.
I did it earlier and it fell into the bit-bucket on posting.
Basically the Registry has 2 places it stores this info - depending on the basic OS (Old Windows or NT Windows - which includes ALL later versions).
This is illustrated in the function below.
To get this to work I had to set up a separate module with OS access functions & utilities in.
This may or may not be necessary in your environment (you may already have something similar), but in case it is, I've included the code for the module (I've called it modOS) in this post.
I can't guarantee all the code in this post but it is free to use and I use it fairly heavily with no noticed side-effects so far.
If you don't want to use any of the code it should at least illustrate what is available to you.
FYI. In NT based OSs the user name is also available in the Environment as USERNAME. - 'GetUser returns the user's logged on name.
-
Public Function GetUser() As String
-
Dim strUserKey As String
-
-
If Environ("OS") = "Windows_NT" Then
-
strUserKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
-
GetUser = RegRead(conHKLM, strUserKey, "DefaultUserName")
-
Else
-
'Windows
-
strUserKey = "Network\Logon"
-
GetUser = RegRead(conHKLM, strUserKey, "username")
-
End If
-
End Function
- modOS
-
Option Compare Database
-
Option Explicit
-
-
Public Const conHKCR = &H80000000
-
Public Const conHKCU = &H80000001
-
Public Const conHKLM = &H80000002
-
Public Const conHKU = &H80000003
-
Public Const conStandardRightsAll = &H1F0000
-
Public Const conReadControl = &H20000
-
Public Const conStandardRightsRead = (conReadControl)
-
Public Const conRegSz = 1
-
Public Const conOK = 0&
-
Public Const conKeyQueryValue = &H1
-
Public Const conKeySetValue = &H2
-
Public Const conKeyCreateLink = &H20
-
Public Const conKeyCreateSubKey = &H4
-
Public Const conKeyEnumerateSubKeys = &H8
-
Public Const conKeyNotify = &H10
-
Public Const conSynchronise = &H100000
-
Public Const conRegOptionNonVolatile = 0
-
Public Const conKeyAllAccess = ((conStandardRightsAll Or _
-
conKeyQueryValue Or _
-
conKeySetValue Or _
-
conKeyCreateSubKey Or _
-
conKeyEnumerateSubKeys Or _
-
conKeyNotify Or _
-
conKeyCreateLink) And _
-
(Not conSynchronise))
-
Public Const conKeyRead = ((conReadControl Or _
-
conKeyQueryValue Or _
-
conKeyEnumerateSubKeys Or _
-
conKeyNotify) And _
-
(Not conSynchronise))
-
-
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
-
Alias "RegOpenKeyExA" (ByVal hKey As Long, _
-
ByVal lpSubKey As String, _
-
ByVal ulOptions As Long, _
-
ByVal samDesired As Long, _
-
phkResult As Long) As Long
-
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) _
-
As Long
-
Private Declare Function RegQueryValueExStr Lib "advapi32.dll" _
-
Alias "RegQueryValueExA" (ByVal hKey As Long, _
-
ByVal lpValueName As String, _
-
ByVal lpReserved As Long, _
-
lpType As Long, _
-
ByVal lpData As String, _
-
lpcbData As Long) As Long
-
-
Public Function RegRead(ByVal lngHive As Long, _
-
ByVal strKey As String, _
-
ByVal strValue As String) As Variant
-
Dim intIdx As Integer, intHK As Integer
-
Dim strWork As String
-
Dim lngRet As Long, lngLen As Long, lngHKey As Long, lngType As Long
-
-
RegRead = Null
-
strKey = strKey & Chr(0)
-
lngRet = RegOpenKeyEx(lngHive, strKey, 0, conKeyRead, lngHKey)
-
If lngRet = conOK Then
-
'Create buffer to store value
-
strWork = Space(255)
-
lngLen = 255
-
lngRet = RegQueryValueExStr(lngHKey, _
-
strValue, _
-
0&, _
-
lngType, _
-
strWork, _
-
lngLen)
-
RegRead = Left(strWork, lngLen - 1)
-
If Len(RegRead) = 254 Then RegRead = Null
-
'Close key
-
Call RegCloseKey(lngHKey)
-
End If
-
End Function
Use this code in a module. - Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
-
-
-
Public Function GetNTUser() As String
-
'Returns the network login name
-
Dim strUserName As String
-
'Create a buffer
-
strUserName = String(100, Chr$(0))
-
'Get user name
-
GetUserName strUserName, 100
-
'Strip the rest of the buffer
-
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
-
GetNTUser = strUserName
-
End Function
In your table or tables, add another field, UserID In the Before Update event of the form on which your table is based, add this code. This assumes yourDB is networked. - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Me!UserID = GetNTUser & " " & Date
-
End Sub
How can I retrieve the user id of the user signed on in access. I would like to use this to mark records added or changed by user.
There is an in built function for this in Access "currentUser()"
eg:
MsgBox("The current user is: " & CurrentUser)
NeoPa 32,556
Expert Mod 16PB
CurrentUser() will return the Access Security account used rather than the PC or Network LogOn name.
If you use this to identify a user that will work well. Unfortunately, in many environments, this will return Admin.
Dear Tanis ,
Your code has worked perfectly for me. Thank you very much!
NeoPa 32,556
Expert Mod 16PB
As this thread was started and answered on my first day as a member of The Scripts Developer Network (as was Bytes then), I'm very pleased to see that it's still able to help people, some years later.
Thank you for your post, and welcome to Bytes!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David Rasmussen |
last post by:
Some sites seem to be session driven in the sense that if I visit the
homepage and do a few clicks I can navigate anywhere I want, but if I
paste the current location into a new browser window...
|
by: Stefano |
last post by:
Hi all,
is it possibile to retrieve the current Windows User Handle from ASP code?
I would like to retrieve the correspondent in ASP for thi piece of ASP.NET
code:
WindowsIdentity id =...
|
by: Le |
last post by:
So here's my dilemma. I need an automated SQL Server (2000) job that
runs once once per hour.
This jobs should:
1) Retrieve unread email for the user "Le" from our Exchange Server,
and insert...
|
by: glady |
last post by:
Hi,
I would like to have a php form that gets input from
user and display data if they have already entered for
that particular record in the same form.
For example, if i have a form with 20...
|
by: kev |
last post by:
Hi all,
I have created a database for equipments. I have a form to register the
equipment meaning filling in all the particulars (ID, serial, type,
location etc). I have two buttons at the end...
|
by: LetMeDoIt |
last post by:
Greetings,
I'm using ASP to retrieve from MSSQL and I then populate a table.
After several months of successfull retrieves, this same code now
bombs out. It turns out that if I clear out from...
|
by: kang jia |
last post by:
hi
i am doing mailinglist currently. the code in my first page is like this :
:
<html>
<head>
<link rel="stylesheet" type="text/css" href="gallery.css" />
<script language="JavaScript">
...
|
by: Mel |
last post by:
I have 10 columns total. 3 of them are invisible. The rest are read-
only BoundFields, 3 of which are editable fields using
TemplateFields. Upon editing, I want to validate what the user enters...
|
by: maxamis4 |
last post by:
Hello folks,
Here is the backgroup. I am creating an agent that can find a user in LDAP and return the last logon date. Now i am not sure if with active directory you can user the...
|
by: Archanak |
last post by:
Hi,
I have table like this:
select * from sampletest;
| title ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |