User Logon Name

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
Back
Top