Get system username when opening MS Access

  • Thread starter incognito41
  • Start date
  • Tags
    System
In summary, the conversation is about using a VBA script to get the user's username and append it to a log table with the date and time every time someone opens a shared 2007 MS Access DB. The suggestion is to use the Environ() function and there may be problems with sandbox mode in Access 2003. The solution was found by using VBA to submit a SQL command.
  • #1
incognito41
22
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
  • #2
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
 
  • #3
thanks, i think i just need to figure out how to add a record to a table and set the value to environ("username")
 
  • #4
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:
  • #5
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
 

What is the purpose of getting the system username when opening MS Access?

Getting the system username when opening MS Access can help identify the user who is currently using the database and keep track of any changes or actions they make. This can be useful for auditing purposes or for creating personalized user experiences.

How can I get the system username when opening MS Access?

To get the system username when opening MS Access, you can use the built-in function "Environ" and specify the "USERNAME" parameter. This will return the current system username as a string.

Can I get the system username of a user who is not currently using MS Access?

No, the "Environ" function will only return the system username of the user who is currently logged in and using MS Access. It cannot retrieve the system username of other users who are not currently using the database.

Why is it important to get the accurate system username when opening MS Access?

Getting the accurate system username is important because it ensures that the correct user is identified and any changes made can be attributed to the correct person. This can help avoid confusion and maintain data integrity.

Can the system username be modified or changed?

Yes, the system username can be modified or changed by the user or by the system administrator. However, the "Environ" function will always return the current system username, so it will reflect any changes made to the username.

Similar threads

  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
254
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top