Problems w/ Sample Code on LogIn Name

SodaJim

Registered User.
Local time
Today, 16:22
Joined
Jan 12, 2003
Messages
51
Hello,

Having a little difficulty on sample code I'm attempting to implement after searching the archives...

I'm utilizing the following code:
-------------------------------
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
'Returns the network login name

Dim lngLen As Long, lngX As Long
Dim strUserName As String

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = ""
End If
End Function
-------------------------------

The current textbox has this as the ControlSource:
=fOSUserName()

Unfortunately, I receive a compile error (Only comments may appear after End Sub, End Function or End Property) which debugs to select the function below...

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Any direction appreciated,
Jim
 
Seen this several times and is usually caused by having an extra 'End Function' or 'End Sub' in your function or form code.

It's easily done when copy and pasting large chunks of code.

Run 'Compile form the vb window and the offending line should get highlighted. Simply remove it.
 
Jim,

I'd put all of that code in a Module and
declare the function as Public.

Then in the OnOpen event of your form I'd
put:

UserName = fOSUserName()

Then do whatever based on UserName. You
might even make gblUserName make it global
for your application.

Wayne
 
Hello Wayne,

The function is in a module and I've replaced "Private" w/ "Public" with the same results...

I also took Kevin's advice and added an "End Function" at the end of the first declared function in the sample code to no avail...

Any other suggestions as to why this code isn't functioning...?
Our network is using Novell, could this be a problem for the api call...?

Thanks again,
Jim
 
I use the same function on a Novell server with no problems.

Have you run a 'Complie All Modules' from the vb window like I said?
 
Hello Kevin,

Yes, I compiled the DB and received the same error:
Unfortunately, I receive a compile error (Only comments may appear after End Sub, End Function or End Property) which debugs to select the function below...

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Does this have to be in a seperate module all by itself...? As it is now, I have added this code to an existing module and for some reason Access 2K draws a line under the function listed above as if it's part of the function above in my module...?

Any ideas welcome...

Thanks again Kevin!

Jim
 
Jim,

Can you post a DB with just that module? Remove tables,
forms, etc.

Wayne
 
No, it doesn't have to go in a seperate module.

I think you are missing an 'End Function' in one of your fucntions in the module.

Check through it all and make sure that all function have a dividing line between them.

The line highlighted.....

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _

should be in the 'General' Section of the module. right at the beginning.
 
Last edited:
Hello Kevin & Wayne,

Attached is the module as a txt file...

I didn't see any missing "End Function" statements needed but question the "Private Declare Function..." which seems to be giving me trouble doesn't have a closing statement...?

Again, Thanks for you time on this mess!
Jim
 

Attachments

Took me literrally 5 seconds to fix it.

LIke I said in my last post.....

Move.....

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

To the GENERAL DECLARATIONS section.
(Aplogies if I wasn't very clear before)

Worked for me
 
Last edited:
Hello Kevin,

Replaced the module with the one you supplied and received a compile error, Can't find project or library...

Access 2K opened the VB window and selected the following text:

String$

from this line:

strUserName = String$(254, 0)

Possibly, I don't have a Library installed...?

Kevin, on a side note, I did place this code just as you have done previously as you suggested, but since I received an error, I didn't mention that... Not until I replaced everything with your modified module.
 
In the vb window select Tools, References.

Make sure DAO 3.6 is selected.
 
Hello Kevin,

I'll give that a try when I get back to work...
Thanks for your patience!
 
Getting frustrated...

Hello again,

DAO 3.6 is already checked but I did notice the following check box:

Missing: Microsoft VBScript Regular Expressions 1.0

Additionally, I now receive an error(Can't find Project or Library) for the use of "Format" in the following code that highlights the above Missing... reference:

---------------------------------------
Dim myTimeStamp As String

myTimeStamp = Format(Now(), "Long Date") & " " & Format(Time(), "Long Time")
---------------------------------------

I beginning to become very frustrated since this has turned into more than my original issue about capturing the LogIn User Name...

Any follow-ups appreciated...
 
Try unticking it.

It's not one of the 'usual' ones, although it might be in a higher version of Access, which I don't use. I use A97 & A2K btw.
 

Users who are viewing this thread

Back
Top Bottom