Create a new module and paste this code into it. Call the module basLogin.
If you have any questions, email us at help@codeoftheweek.com
'----------------------------------------------------------------------
'
' Module Name: basLogin
' Written By: C&D Programming Corp.
' Create Date: 1/2000
' Copyright: Copyright 2000 by C&D Programming Corp. Source
' code may not be reproduced except for use in a
' compiled executable. All rights reserved. If
' you would like to reprint any or all of this
' code please email us at info@codeoftheweek.com
'
'----------------------------------------------------------------------
Option Explicit
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, ByVal lpUserName As String, _
lpnLength As Long) As Long
Public Function LoginName() As String
Dim lRet As Long
Dim lLoginName As Long
Dim sLoginName As String
' This should work in Windows 95, 98 or NT
' initialize enough space for the login name to be returned
sLoginName = Space(256)
lLoginName = Len(sLoginName)
lRet = WNetGetUser(vbNullString, sLoginName, lLoginName)
If lRet = 0 Then
' we need to remove the null at the end of the string
sLoginName = Left(sLoginName, InStr(sLoginName, vbNullChar) - 1)
Else
Err.Raise lRet, "LoginName", "Could not retrieve login name."
End If
LoginName = sLoginName
End Function