【问题标题】:Using the Environ function with an if statement使用带有 if 语句的 Environ 函数
【发布时间】:2014-05-10 05:50:31
【问题描述】:

我正在尝试编写一个 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

【问题讨论】:

    标签: ms-access vba ms-access-2010 dao


    【解决方案1】:

    比我最初想象的要简单得多。

    Private Sub Comando147_Click()
    
      If Environ("userprofile") = "C:\Users\Andy LENTI" Then
    
      MsgBox Environ("username")
    
      Else
    
      MsgBox Environ("userprofile")
    
      End If
    

    结束子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      相关资源
      最近更新 更多