VBA Access - Nome do usuário e computador na rede - Network and computer User Name


sustrain_header_training_com.jpg

Ao tornar as aplicações MS Access mais profissionais, e também ao passo que vamos desenvolvendo soluções sistêmicas, que se incorporam a outras soluções, a relevância e utilização destas requer alguns cuidados mais sofisticados.

Em alguns casos pode ser necessário que registremos os nomes dos usuários que estão acessando as nossas aplicações, ou talvez desejemos desenvolver um modo de Login que utilize esses nomes de usuários na rede.

Function NetUName() As String
On Error GoTo Err_Handler

Dim lngLen As Long 
Dim strUserName As String

Const lngcMaxFieldSize As Long = 64& 

' Inicia.
Let strUserName = String$ (254, vbNullChar)
Let lngLen = 255&

' Em caso de sucesso a API retornará um valor diferente de zero.
If apiGetUserName (strUserName, lngLen) <> 0 Then

Let lngLen = lngLen - 1& 

If lngLen > lngcMaxFieldSize Then 'Tamanho Máximo do campo.
Let lngLen = lngcMaxFieldSize
End If

Let NetworkUserName = Left$ (strUserName, lngLen)
End If

Exit_Handler:

Exit Function

Err_Handler:

Call LogError(Err.Number, Err.Description, conMod & ".NetworkUserName", , False)

Resume Exit_Handler
End Function

 Option Compare Database  
 ' This code was originally written by Dev Ashish.
 ' It is not to be altered or distributed,
 ' except as part of an application.
 ' You are free to use it in any application,
 ' provided the copyright notice is left unchanged.
 ' Code Courtesy of Dev Ashish 
 Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
 "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 Function fOSUserName() As String
 ' Returns the network login name
     Dim lngLen As Long, lngX As Long
     Dim strUserName As String
     Let strUserName = String$(254, 0)
     Let lngLen = 255
     Let lngX = apiGetUserName(strUserName, lngLen)
     If lngX <> 0 Then
         Let fOSUserName = Left$(strUserName, lngLen - 1)
     Else
         Let fOSUserName = ""
     End If
 End Function
Vamos aproveitar e disponibilizar tambéma função que retorna o nome do computador utilizado no momento do log.
 Option Compare Database  
 ' This code was originally written by Dev Ashish.
 ' It is not to be altered or distributed,
 ' except as part of an application.
 ' You are free to use it in any application,
 ' provided the copyright notice is left unchanged.
 '
 ' Code Courtesy of Dev Ashish
 Private Declare Function apiGetComputerName Lib "kernel32" Alias _
 "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 Function fOSMachineName() As String
 'Returns the computername
     Dim lngLen As Long, lngX As Long
     Dim strCompName As String
     lngLen = 16
     strCompName = String$(lngLen, 0)
     lngX = apiGetComputerName(strCompName, lngLen)
     If lngX <> 0 Then
         fOSMachineName = Left$(strCompName, lngLen)
     Else
         fOSMachineName = ""
     End If
 End Function

Tags: Access, log, API, DLL, user name, apiGetUserName, apiGetComputerName

André Luiz Bernardes
A&A® - Work smart, not hard.


Nenhum comentário:

Postar um comentário

diHITT - Notícias