Get system username when opening MS Access

  • Thread starter Thread starter incognito41
  • Start date Start date
  • Tags Tags
    System
AI Thread 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top