CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2007
    Posts
    1

    Question Access2003 - Trying to reference My.User Object

    I am trying to access the My.User object in the standard VB Library. I am trying to log the username of the person updating my access 2003 database form.

    It looks like the default library that is loaded by Access 2003 does not contain all the stardard VB objects.

    When I try to change the library, i would get an error because some of my form already reference certain VB object. I am trying to avoid re-creating my DB.

    Is there a different way to obtain the windows login info? or is there a way to import the right library so i can reference the object?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Access2003 - Trying to reference My.User Object

    Try this, as long as you posted in the VB6 forum. Otherwise, you can modify it for VBA.

    Code:
    Option Explicit
    
    Private Enum EXTENDED_NAME_FORMAT
    	NameUnknown = 0
    	NameFullyQualifiedDN = 1
    	NameSamCompatible = 2
    	NameDisplay = 3
    	NameUniqueId = 6
    	NameCanonical = 7
    	NameUserPrincipal = 8
    	NameCanonicalEx = 9
    	NameServicePrincipal = 10
    End Enum
    
    Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
    
    Private Sub Form_Load()
    	'KPD-Team 2001
    	'URL: http://www.allapi.net/
    	'E-Mail: [email protected]
    	Dim sBuffer As String, Ret As Long
    	sBuffer = String(256, 0)
    	Ret = Len(sBuffer)
    	If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
    		MsgBox "Username: " + Left$(sBuffer, Ret)
    	Else
    		MsgBox "Error while retrieving the username"
    	End If
    End Sub

    or this one, that I saw posted around here:

    Code:
    Option Explicit
    ' Originally posted by ROBDOG888
    
    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Private Function CurrentUser() As String
    	Dim strBuff As String * 255
    	Dim x As Long
    	CurrentUser = ""
    	x = GetUserName(strBuff, Len(strBuff) - 1)
    	If x > 0 Then
    		x = InStr(strBuff, vbNullChar)
    		If x > 0 Then
    			CurrentUser = UCase(Left$(strBuff, x - 1))
    		End If
    	End If
    End Function
    Private Sub Form_Load()
    MsgBox CurrentUser
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Nov 2004
    Location
    LA. California Raiders #1 AKA: Gangsta Yoda™
    Posts
    616

    Re: Access2003 - Trying to reference My.User Object

    @rlsport: You are confusing/mixing VB.NET 2005 with Access VBA code.
    In Access VBA there is no "My.User" class. That is found in VB.NET 2005.

    Are your users logging into Access with workgroup security, database password, or simply by double clicking on the mdb file in Explorer and opening it?

    This sounds like its entirely inside Access VBA but using either one of th code examples dave posted () will work but for the Windows logon user name and not an Access user logon name.
    VB/Office Guru™ (AKA: Gangsta Yoda™)
    VB Forums - Super Moderator 2001-Present

    Microsoft MVP 2006-2011

    Please use [code]your code goes in here[/code] tags when posting code.

    Senior Software Engineer MCP, BSEE, CET
    VS 2012 Premium, VS 6.0 Enterprise SP6, VSTO, Office Ultimate 2010, Windows 7 Ultimate
    Star Wars Gangsta Rap SE Reputations & Rating Posts Office Primary Interop AssembliesAdvanced VB/Office Guru™ Word SpellChecker™.NETAdvanced VB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address ListVB6/Crystal Report Ex.VB6/CR Print Setup Dialog Ex.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured