Thread: WinUserName
View Single Post
  #1 (permalink)  
Old August 20th, 2009, 04:21 AM
oemar00 oemar00 is offline
Registered User
 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Post WinUserName

I coded the WinUserName function to display the Windows UserName in Access Forms as mentioned on the book page 1079 and it work. I am using Access 2007.

There are two problems, one is does not allow me to define the Global constant ERRORMOREDATA, and ERR_SUCCESS so I have been using 234 and 0 in its place.

Another problem is I want to define the Function just once as the book said and use it in different Access Forms that I am using from the MainMenu Form, but it didn't work and I keep getting "Compile error, Sub or Function not defined." However it work if make a copy of the code into each forms.

Any solution, or is there something that I missed. Seems like it does not recognize global or Public as it should.

The Main Form opens all other forms.

The field name in the form is txtUserName.

The field is populated on Current Forms events with the following statement:
txtUserName = WinUserName()

Here is the code:
Code:
Private Declare Function WNetGetUser Lib "mpr" Alias _
  "WNetGetUserA" (ByVal lpName As String, _
  ByVal lpUserName As String, lpnLength As Long) As Long

Function WinUserName() As String
Dim lUserNameLen As Long
Dim stTmp As String
Dim lReturn As Long
Do
   ' Set Up the Buffer
   stTmp = String$(lUserNameLen, vbNullChar)
   lReturn = WNetGetUser(vbNullString, stTmp, lUserNameLen)
' Continue looping until the call succeeds or the buffer is full
Loop Until lReturn <> 234

If lReturn = 0 Then
    WinUserName = Left$(stTmp, InStr(1, stTmp, vbNullChar, vbBinaryCompare) - 1)
End If
End Function
And here is th code that calls the Function:

Code:
Private Sub Form_Current()
txtUserName = WinUserName()
End Sub

Thank you,

oemar00

Last edited by oemar00; August 20th, 2009 at 07:05 AM.. Reason: Missing statements.
Reply With Quote