User Logon Name

G

Guest

Hi all, I have tried to use this (as recommended in other posts, I have
deleted the comments only for this posting).
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 = vbNullString
End If
End Function

But i get an Error - Compile Error- Only comments may appear after end sub,
end Function, end property.
The compilation stops after the declare function.
I know that its simple and I am missing it - please help.

Thanks
 
B

Brendan Reynolds

The Declare statement has to go at the top of the module. You can put it
either before or after the Option Compare and Option Explicit statements,
but it must come before any sub or function declaration. For example this is
OK ...

Option Compare Database
Option Explicit

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

Sub TestSub
'...
End Sub

.... but this is *not* OK ...

Option Compare Database
Option Explicit

Sub TestSub
'...
End Sub

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

Guest

Thanks for your help here it was great.

Brendan Reynolds said:
The Declare statement has to go at the top of the module. You can put it
either before or after the Option Compare and Option Explicit statements,
but it must come before any sub or function declaration. For example this is
OK ...

Option Compare Database
Option Explicit

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

Sub TestSub
'...
End Sub

.... but this is *not* OK ...

Option Compare Database
Option Explicit

Sub TestSub
'...
End Sub

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top