2008-08-12 32 views
9

Tôi đang làm việc trên một spec cho một phần mềm cho công ty của tôi và là một phần của hệ thống kiểm toán Tôi nghĩ rằng nó sẽ được gọn gàng nếu có một cách để lấy người dùng Active Directory hiện hành.Có cách nào để MS Access lấy người dùng Active Directory hiện tại không?

Hy vọng rằng một cái gì đó như:

Dim strUser as String 
strUser = ActiveDirectory.User() 
MsgBox "Welcome back, " & strUser 

Trả lời

6

Try this article - Tôi có một số mã tại nơi làm việc sẽ erm, làm việc nếu điều này không ...

quote liên quan:

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

Function ThisUserName() As String 
    Dim LngBufLen As Long 
    Dim strUser As String 

    strUser = String$(15, " ") 
    LngBufLen = 15 

    If GetUserName(strUser, LngBufLen) = 1 Then 
     ThisUserName = Left(strUser, LngBufLen - 1) 
    Else 
     ThisUserName = "Unknown" 
    End If 
End Function 

Function ThisComputerID() As String 
    Dim LngBufLen As Long 
    Dim strUser As String 

    strUser = String$(15, " ") 
    LngBufLen = 15 

    If GetComputerName(strUser, LngBufLen) = 1 Then 
     ThisComputerID = Left(strUser, LngBufLen) 
    Else 
     ThisComputerID = 0 
    End If 
End Function 
+0

Tôi đã bị cám dỗ để bỏ phiếu bầu cho câu trả lời chỉ có liên kết của bạn, nhưng meh, chúng ta hãy trích dẫn những điều may mắn không liên kết thối;) –

+0

Tất cả đều tốt. Là một câu trả lời khá shitty về sự phản ánh. Là những ngày đầu tiên tho, không chỉ liên kết câu trả lời mọi thứ không cứng nhắc như ngày hôm nay, trong phòng thủ của tôi;) Dù sao, cảm ơn cho chỉnh sửa. – JamesSugrue

2

Tùy thuộc vào biến môi trường vẫn hợp lệ là một ý tưởng tồi, vì chúng có thể dễ dàng được thay đổi trong phiên người dùng.

+0

Điểm rất tốt! – Yarik

2

David đã thực hiện một điểm rất tốt về nguy cơ sử dụng các biến môi trường. Tôi chỉ có thể thêm rằng có thể có các vấn đề khác với các biến môi trường. Chỉ cần nhìn vào đoạn mã thực tế này từ dự án 5 tuổi của chúng tôi:

Public Function CurrentWorkbenchUser() As String 

    ' 2004-01-05, YM: Using Application.CurrentUser for identification of 
    ' current user is very problematic (more specifically, extremely 
    ' cumbersome to set up and administer for all users). 
    ' Therefore, as a quick fix, let's use the OS-level user's 
    ' identity instead (NB: the environment variables used below must work fine 
    ' on Windows NT/2000/2003 but may not work on Windows 98/ME) 
    ' CurrentWorkbenchUser = Application.CurrentUser 
    ' 
    ' 2005-06-13, YM: Environment variables do not work in Windows 2003. 
    ' Use Windows Scripting Host (WSH) Networking object instead. 
    ' CurrentWorkbenchUser = Environ("UserDomain") & "\" & Environ("UserName") 
    ' 
    ' 2007-01-23, YM: Somewhere between 2007-01-09 and 2007-01-20, 
    ' the WshNetwork object stopped working on CONTROLLER3. 
    ' We could not find any easy way to fix that. 
    ' At the same time, it turns out that environment variables 
    ' do work on Windows 2003. 
    ' (Apparently, it was some weird configuration problem back in 2005: 
    ' we had only one Windows 2003 computer at that time and it was 
    ' Will's workstation). 
    ' 
    ' In any case, at the time of this writing, 
    ' returning to environment variables 
    ' appears to be the simplest solution to the problem on CONTROLLER3. 
    ' Dim wshn As New WshNetwork 
    ' CurrentWorkbenchUser = wshn.UserDomain & "\" & wshn.UserName 

    CurrentWorkbenchUser = Environ("USERDOMAIN") & "\" & Environ("USERNAME") 

End Function 
+0

Không có gì sai với CurrentUser() nếu bạn đang thực sự sử dụng tính năng bảo mật mức người dùng Máy bay phản lực. Mặt khác, nếu bạn không có nhu cầu về Jet ULS, thì đăng nhập Windows là một thay thế tuyệt vời (mặc dù bạn vẫn có thể phải duy trì một số loại thành viên nhóm trong ứng dụng của mình). –

3

Dưới đây là phiên bản của tôi: nó sẽ lấy bất cứ thứ gì bạn thích:

'gets firstname, lastname, fullname or username 
Public Function GetUser(Optional whatpart = "username") 
    Dim returnthis As String 
    If whatpart = "username" Then GetUser = Environ("USERNAME"): Exit Function 
    Set objSysInfo = CreateObject("ADSystemInfo") 
    Set objUser = GetObject("LDAP://" & objSysInfo.USERNAME) 
    Select Case whatpart 
     Case "fullname": returnthis = objUser.FullName 
     Case "firstname", "givenname": returnthis = objUser.givenName 
     Case "lastname": returnthis = objUser.LastName 
     Case Else: returnthis = Environ("USERNAME") 
    End Select 
    GetUser = returnthis 
End Function 

I got the original idea from Spiceworks.

Các vấn đề liên quan