Get system username when opening MS Access

  • Thread starter Thread starter incognito41
  • Start date Start date
  • Tags Tags
    System
Click For Summary
A user seeks assistance with running a VBA script in a shared 2007 MS Access database that logs the username, date, and time each time the database is opened. The solution involves using the Environ() function to retrieve the username. Concerns about sandbox mode in Access 2003 are noted, but the focus remains on Access 2007. A sample function, Run_Log, is provided, which constructs an SQL command to insert the username and current date and time into a log table. The user successfully implements this by creating a macro that triggers the script on database load, demonstrating a practical approach to logging user activity in Access.
incognito41
Messages
22
Reaction score
0
We have a shared 2007 MS Access DB here at work and I would like to run a script that runs everytime someone opens the file. I would like the VBA script to get the user's username and append a log table with that info and with the date and time. I tried looking this up have only found uses for IE. Can anyone help with this? any help is greatly appreciated.


thanks,
 
Technology news on Phys.org
You can use the Environ() function.
Code:
[String] = Environ("UserName")

I've heard there are problems with sandbox mode in Access 2003, I haven't tried it with 2007 so not sure. You might want to look into this.

Sample Code:
Code:
Function RetrieveUser()
YourString = Environ("UserName")
End Function
 
thanks, i think i just need to figure out how to add a record to a table and set the value to environ("username")
 
What part are you stuck with?

I have just done something very similar today so may be able to help you with exactly what you are looking for.
 
Last edited:
I figured it out. I used VBA to submit a SQL command since there seems to be more help for SQL than there is for VBA. I created a macro with the script below and then call it ON LOAD. Thanks for the help!

Function Run_Log()

Dim mySQL As String
Dim Name As String
Name = Environ("UserName")
mySQL = "INSERT INTO tbl_user_log ( Username, _Date )"
mySQL = mySQL & " VALUES ('" & Name & "' , Date() &' ' & time() )"

DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
DoCmd.SetWarnings True

End Function
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
3K