p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 (http://p2p.wrox.com/forumdisplay.php?f=329)
-   -   WinUserName (http://p2p.wrox.com/showthread.php?t=75803)

oemar00 August 20th, 2009 04:21 AM

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:

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
  ' 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:


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

Thank you,


All times are GMT -4. The time now is 05:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.