View Full Version : Solved: default user name
Scooter172
11-06-2011, 03:02 PM
I have a form for data entry that enters data into a table. One column in that table is labeled Entered By. In the data entry form associated with this table I want the Default Value of that entry to be the Network User name of the person conducting the entry.
Scooter172
11-07-2011, 11:56 PM
If it helps this is what I have currently, and it enters the correct name on my home computer, but when I use this same routine on the company network I get #Name? in the box
Option Compare Database
Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"getUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSUserName() As String
'Returns the Network Login Name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lnghLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
foSUerName = vbNullString
End If
End Function
I have a form for data entry that enters data into a table. One column in that table is labeled Entered By. In the data entry form associated with this table I want the Default Value of that entry to be the Network User name of the person conducting the entry.
HiTechCoach
11-09-2011, 08:38 AM
What happens if you go tot he VBA Immediate windows (Ctrl-G) and run:
? fOSUserName
HiTechCoach
11-09-2011, 08:39 AM
Also what version of Windows?
If you manually compile the VBA code do you get any errors?
Did you check to see if you have any missing references?
Scooter172
11-12-2011, 09:12 AM
Also what version of Windows?
If you manually compile the VBA code do you get any errors?
Did you check to see if you have any missing references?
This is what I found on another service. works great!
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function UserNameWindows() As String
Dim lngLen As Long
Dim strBuffer As String
Const dhcMaxUserName = 255
strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function
HiTechCoach
11-14-2011, 01:58 PM
Both sets of code you posted use the same API. At first glance they look almost identical. Not sure why you had issues with the first one.
Glad to hear that you found a resolution.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.