Peter Mogford
Peter Mogford

Reputation: 496

Command Button Visibitly based on Application.Username

I am looking at creating an administration button on my user-form.

Now i want it to look up against the application.Username to determine whether the button is visible or not.

My current code is:

Private Sub AdminBtn()

If Application.Username = "Peter.Mogford" Then
AdminCmd.Visible = False
Else
AdminCmd.Visible = True
End If

End Sub

I have put myself in to test it and asked for the visibility to be false but for some reason it is still showing.

I'm not sure whether it has something to do with me writing the code in the user-form instead of a module or something like that.

Please can someone offer a bit of advise.

Thanks in advance

Upvotes: 0

Views: 105

Answers (1)

bashis
bashis

Reputation: 1243

The good practice here in case you only need to update the visibility once the form is shown would be using a Form constructor.

Use it like this:

Public Class Form1

Public Sub New()
    InitializeComponent() 
    If Application.Username = "Peter.Mogford" Then
    AdminCmd.Visible = False
    Else
    AdminCmd.Visible = True
    End If
End Sub

End Class

When writing a custom form constructor, it is important to call the InitializeComponent() function, which does the controls initialization, before you call any of them.

Upvotes: 1

Related Questions