Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Return Netware login with VBA

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

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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