Button visibility

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have made my own switchboard, and want one of the buttons and its label to
only be visible if either a certain user is signed on, or if the user is an
admin. I would appreciate the code to do it either way as I'm not sure which
I am going to do. Thanks in advance for your help.
 
Hi Spencer,

Use the Form_Open event procedure. Something like this, where NTUserID1 and
NTUserID2 represent the NTUser ID's that these people use to log into Windows:


Private Sub Form_Open(Cancel As Integer)
On Error GoTo ProcError

Dim strUserName As String
strUserName = fOSUserName

Select Case strUserName
Case "NTUserID1", "NTUserID2" 'These people can see the buttons
Me.lblLabelName.Visible = True
Me.cmdButtonName.Visible = True
Case Else
Me.lblLabelName.Visible = False
Me.cmdButtonName.Visible = False
End Select

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure Form_Open..."
Resume ExitProc
End Sub


You can grab a copy of the fOSUserName function here:

Get Login name
http://www.mvps.org/access/api/api0008.htm

I would appreciate the code to do it either way...
The SELECT CASE part of the code would be similar if you are using ULS (User
level security) in Access. However, I tend to avoid using ULS, so you'll need
to work out how to grab the logged on user. I think it might be the
CurrentUser function, so instead of Select Case strUserName, you would have
Select Case CurrentUser, and you would not need the fOSUserName function.


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
 
I want to be able to use the name they sign into the database with. How do I
write the code to get the access name they use?
 
Hi Spencer,

The code I gave you works for identifying an individual based on their
Windows NT UserID. If you are using User Level Security withing Access, then
making a call to the CurrentUser function should do the trick. Something like
this:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo ProcError

Select Case CurrentUser
Case "admin" 'These people can see the buttons
Me.lblLabelName.Visible = True
Me.cmdButtonName.Visible = True
Case Else
Me.lblLabelName.Visible = False
Me.cmdButtonName.Visible = False
End Select

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure Form_Open..."
Resume ExitProc
End Sub


I suppose another possibility might be that whoever designed your database
came up with a home-grown form of security, with usernames and passwords
perhaps stored in a table. In that case, one would likely need to store the
logged on userid in a global variable (split database, of course, where each
user has their own copy of the front-end application), and then use this
value in the SELECT Case statement.


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
 
Back
Top