简体   繁体   English

使用带有 if 语句的 Environ 函数

[英]Using the Environ function with an if statement

I am trying to write an IF statement which will allow me to identify the "USER NAME" according to the Environ function.我正在尝试编写一个 IF 语句,它允许我根据 Environ 函数识别“用户名”。 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.我发现的以下代码使我能够成功识别用户名(以及 UserDomain、UserProfile 和 Windir),但我不知道如何将这些信息转换为高效的 if 语句,以便我根据情况执行操作在用户名上。 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'.简而言之,我想写一个 if 语句,如果用户名不是“Andy LENTI”,它将允许我关闭我的数据库。 Can someone please help me?有人可以帮帮我吗?

Private Sub Comando146_Click()

'Function VariablesEnvironnement() '函数变量Environnement()

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM