문제

I am trying to write an IF statement which will allow me to identify the "USER NAME" according to the Environ function. The below code I have found allows me to successfully identify the Username (As well as the UserDomain, UserProfile, and windir) but i don't know how to translate this information into a productive if statement that will allow me to perform an action depending on the Username. the code below produces the following output in my immediate window:

'35 : Environ("USERDOMAIN_ROAMINGPROFILE") = Marchese
'36 : Environ("USERNAME") = Andy LENTI
'37 : Environ("USERPROFILE") = C:\Users\Andrea LENTI
'38 : Environ("windir") = C:\Windows

In short, i would like to write an if statement that will allow me to close my database if the username is NOT 'Andy LENTI'. Can someone please help me?

Private Sub Comando146_Click()

'Function VariablesEnvironnement()

Dim strEnviron As String
Dim Indx As Integer
Dim pos As Integer
Dim message As String

Indx = 1
strEnviron = Environ(Indx)
Do While strEnviron <> ""
pos = InStr(1, strEnviron, "=")
Debug.Print Indx & " : Environ(""" & Left(strEnviron, pos - 1) & """) = " & _
Right(strEnviron, Len(strEnviron) - pos)
Indx = Indx + 1
strEnviron = Environ(Indx)
Loop

MsgBox (strEnviron)

End Sub
도움이 되었습니까?

해결책

Much more simple than I had originally thought.

Private Sub Comando147_Click()

  If Environ("userprofile") = "C:\Users\Andy LENTI" Then

  MsgBox Environ("username")

  Else

  MsgBox Environ("userprofile")

  End If

End Sub

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top