Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 09:11 AM.


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