Get system username when opening MS Access

  • Thread starter Thread starter incognito41
  • Start date Start date
  • Tags Tags
    System
Click For Summary

Discussion Overview

The discussion revolves around how to retrieve the system username in a shared MS Access 2007 database and log it into a table each time the database is opened. The focus is on using VBA scripting to achieve this functionality.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks assistance in creating a VBA script to log the user's username along with the date and time when the database is opened.
  • Another participant suggests using the Environ() function to retrieve the username, noting potential issues with sandbox mode in earlier versions of Access.
  • A participant expresses the need to learn how to add a record to a table using the retrieved username.
  • One participant offers to help based on their recent experience with a similar task.
  • A later reply indicates that the original poster successfully implemented a solution using VBA to execute a SQL command to log the username and timestamp into a table.

Areas of Agreement / Disagreement

Participants generally agree on the approach of using the Environ() function to retrieve the username, but there is no consensus on the potential issues with sandbox mode in Access 2003 and its applicability to Access 2007.

Contextual Notes

Some limitations include the lack of detailed discussion on error handling in the VBA code and the specific configuration of the Access database that may affect the execution of the script.

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
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · 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
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
4K