PDA

View Full Version : [SOLVED] Populate a textbox with the username



Adonaioc
12-05-2008, 10:02 AM
I have the code


Private Sub UserForm_Initialize()
TxtUser.Text = Worksheets("Input").Range("A1").Value
End Sub

I tried to adapt it by making it


= Environ("Username")

but to no avail
I tried adding a functing and calling it in the control source and could not get that to work either...

Any tips would be appriciated

lucas
12-05-2008, 10:07 AM
Maybe:


Option Explicit
'api call for obtaining the username
Private Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long)

Public Function WindowsUserName() As String
' Function to extract the name:
Dim szBuffer As String * 100
Dim lBufferLen As Long
lBufferLen = 100
If CBool(GetUserName(szBuffer, lBufferLen)) Then
WindowsUserName = Left$(szBuffer, lBufferLen - 1)
Else
WindowsUserName = CStr(Empty)
End If
End Function

Sub GetTheNameENVIRON()
' Deliver the name via msgbox
' Same as the api version,
MsgBox "Environ username is: " & Environ("USERNAME")
End Sub

Kenneth Hobs
12-05-2008, 10:07 AM
For their Excel name, use:

Application.Username

Adonaioc
12-05-2008, 10:11 AM
But how to I get that into the textbox on my userform?

Kenneth Hobs
12-05-2008, 10:19 AM
Private Sub UserForm_Initialize()
TextBox1.Value = Application.UserName
End Sub

lucas
12-05-2008, 10:24 AM
put this part in a standard module:


Option Explicit
'api call for obtaining the username
Private Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long)

Public Function WindowsUserName() As String
' Function to extract the name:
Dim szBuffer As String * 100
Dim lBufferLen As Long
lBufferLen = 100
If CBool(GetUserName(szBuffer, lBufferLen)) Then
WindowsUserName = Left$(szBuffer, lBufferLen - 1)
Else
WindowsUserName = CStr(Empty)
End If

put this in your userform code:


Private Sub UserForm_Initialize()
TxtUser.Text = "Environ username is: " & Environ("USERNAME")
End Sub


see attached...

Bob Phillips
12-05-2008, 10:25 AM
Adonaic,

I would suggest that you never use Application.Username. It is liable to change, and doesn't really signify anything reliable. Far better to use lucas' recommendation, Environ("Username"),

Adonaioc
12-05-2008, 11:32 AM
Thank you Lucas your code works, I swear I tried that (I found it in the KB) but I must have had something wrong.

Application.Username only gave me the name under who excel is registered too.

Was my mistake having the private declare function in my form and not in a module?

Thanks again

lucas
12-05-2008, 11:36 AM
yes, the function needs to be in a standard module.

Adonaioc
12-05-2008, 12:53 PM
I got it up and running and it works great on my computer but when i try and open it from my coworkers computer it highlights environ in my userform code and say project or library could not be found.

Any idea why?

mdmackillop
12-05-2008, 01:20 PM
Check Tools/References for any "Missing" items. Remove the check against them.

Bob Phillips
12-05-2008, 01:20 PM
Sounds like they have a missing reference.

In the VBIDE, goto Tools>rfeferences, and if there is an item with MISSING marked alongside, uncheck that item.