Get system username when opening MS Access

  • Thread starter Thread starter incognito41
  • Start date Start date
  • Tags Tags
    System
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,
 
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
3K
  • · 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
3K
Replies
4
Views
4K