Wrox Programmer Forums
|
BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3
This is the forum to discuss the Wrox book Access 2007 VBA Programmer's Reference by Teresa Hennig, Rob Cooper, Geoffrey L. Griffith, Armen Stein; ISBN: 9780470047033
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 20th, 2009, 04:21 AM
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.









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