Help with Environ("username")

smithj83

New Member
Joined
May 26, 2004
Messages
41
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

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
MsgBox ("You are not authorized to perform this function")
End If


End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi

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

Code:
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
 
Upvote 0
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.
Code:
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
 
Upvote 0
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.
Code:
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
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.
 
Upvote 0
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.
Code:
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
 
Upvote 0
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.:)
 
Upvote 0

Forum statistics

Threads
1,225,363
Messages
6,184,519
Members
453,238
Latest member
visuvisu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top