?
Solved

Return Netware login with VBA

Posted on 2004-10-06
2
Medium Priority
?
388 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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 200 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 51

Accepted Solution

by:
Gustav Brock earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

762 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