Solved

Return Netware login with VBA

Posted on 2004-10-06
2
369 Views
Last Modified: 2012-06-21
Is there a way to return the users' netware login through VBA?  I found the Environ() command that returns the windows login, but I need the netware login.  Thank you!
0
Comment
Question by:drache21
2 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 50 total points
ID: 12239133
Use this function / API call...

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

Public Function fn_get_user_name() As String

'Returns the network ID of the person that logged into the machine that is running this Access application:
'This is not the same as the Access security user.

'Visual Basic Source Code Library, The Waite group, Chapter 12.5

On Error GoTo err_fn_get_user_name

Dim str_user_name As String, lng_buffer_size As Long, lng_ret_code As Long, lng_null_character_position As Long

str_user_name = Space(80)
lng_buffer_size = Len(str_user_name)

lng_ret_code = GetUserName(str_user_name, lng_buffer_size)

lng_null_character_position = InStr(str_user_name, Chr(0))
If lng_null_character_position > 0 Then
    str_user_name = Left(str_user_name, lng_null_character_position - 1)
Else
    str_user_name = " "
End If

fn_get_user_name = UCase(str_user_name)

exit_fn_get_user_name:
    Exit Function
   
err_fn_get_user_name:
    fn_get_user_name = "Not able to get user name."
    Resume exit_fn_get_user_name
   
End Function

0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 50 total points
ID: 12241035
For NT and beyond, the quick and dirty method is:

  strNetWareLoginName = Environ("NWUSERNAME")

A better/safer method it to use the API to the NDS/eDirectory.
Here is code to copy and paste:

<code>

Option Compare Database
Option Explicit

Private Declare Function NWCallsInit Lib "calwin32" ( _
  ByRef reserved1 As Byte, _
  ByRef reserved2 As Byte) _
  As Long
 
Private Declare Function NWDSCreateContextHandle Lib "netwin32" ( _
  ByRef newHandle As Long) _
  As Long
 
Private Declare Function NWDSWhoAmI Lib "netwin32" ( _
  ByVal context As Long, _
  ByVal objectName As String) _
  As Long
 
Private Declare Function NWDSFreeContext Lib "netwin32" ( _
  ByVal context As Long) _
  As Long
'

Public Function GetNetwareUserName() As String

' Retrieve name of NetWare user currently logged in.
' 2002-03-04. Cactus Data ApS, CPH.

  ' Length of buffer for login name.
  Const clngLenBuffer As Long = 255
  ' Header to strip from retrieved user info.
  Const cstrHeader    As String = "CN="
 
  Dim lngCCode        As Long
  Dim lngNdsHandle    As Long
  Dim lngUserInfo     As Long
  Dim strUserName     As String
 
  On Error GoTo Err_GetNetwareUserName
 
  ' Dimension buffer for user name.
  strUserName = Space(clngLenBuffer)
 
  ' Initialize NetWare client.
  lngCCode = NWCallsInit(0, 0)
  If lngCCode = 0 Then
    ' Obtain handle to NDS/eDirectory.
    lngCCode = NWDSCreateContextHandle(lngNdsHandle)
    If lngCCode = 0 Then
      ' Retrieve current user name.
      lngCCode = NWDSWhoAmI(lngNdsHandle, strUserName)
      If lngCCode = 0 Then
        ' Trim string from buffer.
        lngUserInfo = InStr(strUserName, vbNullChar) - 1
        If lngUserInfo > 0 Then
          strUserName = Left(strUserName, lngUserInfo)
          strUserName = Mid(strUserName, 1 + Len(cstrHeader))
        End If
      End If
      ' Release handle to NDS/eDirectory.
      lngCCode = NWDSFreeContext(lngNdsHandle)
    End If
  End If

  GetNetwareUserName = strUserName

Exit_GetNetwareUserName:
  Exit Function

Err_GetNetwareUserName:
  MsgBox "Error " & Err.Number & ": " & Err.description, _
    vbCritical, _
    Err.Source
  Resume Exit_GetNetwareUserName
 
End Function

</code>

/gustav
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now