有没有办法让MS Access获取当前的Active Directory用户?

时间:2008-08-12 17:03:28

标签: ms-access active-directory

我正在为我的公司制作一个软件规范,作为审计系统的一部分,我认为如果有办法获取当前的Active Directory用户,它会很简洁。

希望如下:

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

4 个答案:

答案 0 :(得分:6)

Try this article - 我会在工作中使用一些代码,如果不这样做的话......

相关引用:

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

答案 1 :(得分:3)

这是我的版本:它会抓取你喜欢的任何内容:

<?php

$dbh = new PDO(
  'mysql:dbname=myDatabase;host=myHost', 
  'myUsername', 'myPassword');

$stmt = $dbh->prepare('SELECT * FROM SearchPG WHERE PGName LIKE ? ');
$stmt->bindParam(1, $searchtext, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
    print_r($row);
}

I got the original idea from Spiceworks

答案 2 :(得分:2)

David对使用环境变量的风险提出了非常好的观点。我只能补充说环境变量可能存在其他问题。只需看看我们这个有5年历史的项目中的实际代码片段:

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

答案 3 :(得分:2)

根据环境变量保持有效是一个坏主意,因为它们可以在用户会话中轻松更改。