[英]Using the Environ function with an if statement
我正在尝试编写一个 IF 语句,它允许我根据 Environ 函数识别“用户名”。 我发现的以下代码使我能够成功识别用户名(以及 UserDomain、UserProfile 和 Windir),但我不知道如何将这些信息转换为高效的 if 语句,以便我根据情况执行操作在用户名上。 下面的代码在我的即时窗口中产生以下输出:
'35 : Environ("USERDOMAIN_ROAMINGPROFILE") = Marchese
'36 : Environ("USERNAME") = Andy LENTI
'37 : Environ("USERPROFILE") = C:\Users\Andrea LENTI
'38 : Environ("windir") = C:\Windows
简而言之,我想写一个 if 语句,如果用户名不是“Andy LENTI”,它将允许我关闭我的数据库。 有人可以帮帮我吗?
Private Sub Comando146_Click()
'函数变量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
比我原先想象的要简单得多。
Private Sub Comando147_Click()
If Environ("userprofile") = "C:\Users\Andy LENTI" Then
MsgBox Environ("username")
Else
MsgBox Environ("userprofile")
End If
结束子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.