Solved

Return Netware login with VBA

Posted on 2004-10-06
2
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

736 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