天天看点

VBA中获得当前计算机的用户名

最近的项目里需要用VBA,比较郁闷,写程序越来越倒退了,用起VBA来了

VBA中获得当前计算机的用户名

需要在access中获得当前的计算机用户,用于登陆sql server数据库

' Makes sure all variables are dimensioned in each subroutine.
     Option Explicit

     ' Access the GetUserNameA function in advapi32.dll and
     ' call the function GetUserName.
     Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
     (ByVal lpBuffer As String, nSize As Long) As Long

     ' Main routine to Dimension variables, retrieve user name
     ' and display answer.
     Sub Get_User_Name()

     ' Dimension variables
     Dim lpBuff As String * 25
     Dim ret As Long, UserName As String

     ' Get the user name minus any trailing spaces found in the name.
     ret = GetUserName(lpBuff, 25)
     UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)

     ' Display the User Name
     MsgBox UserName
     End Sub
						      

继续阅读