# Help with Environ("username")



## smithj83 (Mar 3, 2008)

This problem is actually in Word, not Excel. But I thought someone might still be able to help.

On the form I'm working on, at the bottom I have 2 commandbuttons and 2 texboxes. The command buttons serve as a digital signature of sorts. The goalstate is that when you click commandbutton1 it drops the following in Textbox1 (TextBox1.Value = (Environ("username") & " - " & Now())). 

Commandbutton1 and Textbox1 are designated for managers only.
Commandabutton11 and Textbox11 are designated for non-managers only.

I need something built in to make sure each party can't use the other party's buttons. (i.e. For the manager button, if (Environ("username") = one of the 6 mgr id's then allow the click. If (Environ("username") <> one of the 6 mgr id's then pop up a msgbox stating access is not allowed).

There are only 6 managers, so I tried to write the code as such, and it apparently does not work. *Remember this WORD I'm trying to do this in, not Excel.*



> Private Sub CommandButton1_Click()
> If Environ("username") = "brookss1" Or Environ("username") = "HURLEYM1" Or Environ("username") = "innamoj" Or Environ("username") = "meyersl" Or Environ("username") = "rideoum" Or Environ("username") = "tomling2" Then
> TextBox1.Value = (Environ("username") & " - " & Now())
> End If
> ...


----------



## Richard Schollar (Mar 3, 2008)

Hi

If you want to do it like this, then you need ANDs not ORs in the second If:


```
If Environ("username") <> "brookss1" And Environ("username") <> "HURLEYM1" And Environ("username") <> "innamoj" And Environ("username") <> "meyersl" And Environ("username") <> "rideoum" And Environ("username") <> "tomling2" Then
MsgBox ("You are not authorized to perform this function")
End If
```


----------



## onlyadrafter (Mar 3, 2008)

Hello,

In the second IF all the OR's should be AND's.


----------



## Norie (Mar 3, 2008)

I think Richard and onlyadrafter are right but I would also make a couple of suggestions.

1 Stick Environ("username") in a variable.

2 Use a Select Case structure.

```
Private Sub CommandButton1_Click()
Dim strName As String
    strName = Environ("username")
    
    Select Case strName
        Case "brookss1", "HURLEYM1", "innamoj", "meyersl", "rideoum", "tomling2"
            TextBox1.Value = strName & "-" & Now()
        Case Else
            MsgBox ("You are not authorized to perform this function")
    End Select
End Sub
```


----------



## dragontooth (Mar 3, 2008)

Why not on Document_Open don't you check the username and disable the button?


----------



## smithj83 (Mar 4, 2008)

Norie said:


> I think Richard and onlyadrafter are right but I would also make a couple of suggestions.
> 
> 1 Stick Environ("username") in a variable.
> 
> ...


Thic code worked perfectly for the first button!!!! How would I write it for the 2nd button? I want the 2nd button to recognize those 6 names and disallow use of the 2nd button.


----------



## dragontooth (Mar 4, 2008)

Using Norie's suggestion for the select case I would recommend that you make the button visible to the users you want to see it.

```
Private Sub UserForm_Initialize()
    Dim strName As String
    strName = Environ("username")
    Select Case strName
        Case "brookss1", "HURLEYM1", "innamoj", "meyersl", "rideoum", "tomling2"
            'managers only
            Commandbutton1.Visible = True
            Textbox1.Visible = True
            Commandbutton11.Visible = False
            Textbox11.Visible = False
        Case Else
            Commandbutton11.Visible = True
            Textbox11.Visible = True
            Commandbutton1.Visible = False
            Textbox1.Visible = False
    End Select
End Sub
```

HTH
dragontooth


----------



## Norie (Mar 4, 2008)

dragontooth

That's probably a good idea.

But I think there's one problem - as far as I can tell the OP isn't using a userform.


----------



## dragontooth (Mar 4, 2008)

LOL My bad Thanks.

Then I would use the original post I made for the Document_Open. Whatcha think?


----------

