PeterHing
asked on
Display Windows Username
Hey All -
I have just been looking at the following question / solution:
https://www.experts-exchange.com/questions/21582912/Automatically-enter-the-current-Logged-on-Windows-user-into-a-field.html
But cannot see where I need to put all the code within Access. I'm refering to the code mentioned by TimCottee.
Can anyone help me?
Many Thanks - Pete
I have just been looking at the following question / solution:
https://www.experts-exchange.com/questions/21582912/Automatically-enter-the-current-Logged-on-Windows-user-into-a-field.html
But cannot see where I need to put all the code within Access. I'm refering to the code mentioned by TimCottee.
Can anyone help me?
Many Thanks - Pete
You put the code in a module.
Then, to call it, you put the fucnction name into your vba code behind an event or command:
EX (for a command button on a form):
Private Sub myCommandButton_Click()
Me.myTextBox.Value = UserName
End Sub
To view/test the results, type ctrl+g to open the immediate window and type in the function name.
EX (for a command button on a form):
Private Sub myCommandButton_Click()
Me.myTextBox.Value = UserName
End Sub
To view/test the results, type ctrl+g to open the immediate window and type in the function name.
ASKER
Ok - Modules aren't really my best friend, lol.
I've now created a module (Module1) - and it has this information within it:
' Return the user's name.
Private Function UserName() As String
Const UNLEN = 256 ' Max user name length.
Dim user_name As String
Dim name_len As Long
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
ByRef nSize As Integer) As Integer
user_name = Space$(UNLEN + 1)
name_len = Len(user_name)
If GetUserName(user_name, name_len) = 0 Then
UserName = "<unknown>"
Else
UserName = Left$(user_name, name_len - 1)
End If
End Function
On the database from, I have gone to the properties box of the textbox in question. In Data > Default Value - I've put GetUserName() - But the database comes back with #Name?
I've now created a module (Module1) - and it has this information within it:
' Return the user's name.
Private Function UserName() As String
Const UNLEN = 256 ' Max user name length.
Dim user_name As String
Dim name_len As Long
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
ByRef nSize As Integer) As Integer
user_name = Space$(UNLEN + 1)
name_len = Len(user_name)
If GetUserName(user_name, name_len) = 0 Then
UserName = "<unknown>"
Else
UserName = Left$(user_name, name_len - 1)
End If
End Function
On the database from, I have gone to the properties box of the textbox in question. In Data > Default Value - I've put GetUserName() - But the database comes back with #Name?
The declarations are above the function and need to go at the top of the entire window (right below the "Option Compare Database"):
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
ByRef nSize As Integer) As Integer
' Return the user's name.
Private Function UserName() As String
Const UNLEN = 256 ' Max user name length.
Dim user_name As String
Dim name_len As Long
user_name = Space$(UNLEN + 1)
name_len = Len(user_name)
If GetUserName(user_name, name_len) = 0 Then
UserName = "<unknown>"
Else
UserName = Left$(user_name, name_len - 1)
End If
End Function
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
ByRef nSize As Integer) As Integer
' Return the user's name.
Private Function UserName() As String
Const UNLEN = 256 ' Max user name length.
Dim user_name As String
Dim name_len As Long
user_name = Space$(UNLEN + 1)
name_len = Len(user_name)
If GetUserName(user_name, name_len) = 0 Then
UserName = "<unknown>"
Else
UserName = Left$(user_name, name_len - 1)
End If
End Function
ASKER
Perfect - Thank You!
So then lastly, what do I need to put in Default Value for it to display that data?
Again - Thanks for your help on this one!
So then lastly, what do I need to put in Default Value for it to display that data?
Again - Thanks for your help on this one!
ASKER
At the moment - it currently says:
=GetUserName("«lpBuffer»", "«nSize»")
but comes back as 'Error' in the textbox
=GetUserName("«lpBuffer»",
but comes back as 'Error' in the textbox
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Amazing! Returned 'Peter Hing' which is even better than I thought!
I assumed it would give domain\username - but this is even better!
Thanks so much for your help ajkamp!
I assumed it would give domain\username - but this is even better!
Thanks so much for your help ajkamp!
No problem! I'm glad I could help out. Thanks for the points :)
//Aaron
//Aaron