Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable buttons if no security access 1

Status
Not open for further replies.

chrisaroundtown

Technical User
Jan 9, 2003
122
AU
Hi,

I have a main form which has a whole bunch of command buttons on it. One paricular button is to open up a form where some data can be edited. I want some users to access this form and others to not. I can restrict their access to the form through security. If a user does not have access to the form they are still able to press the button on the main form and a message comes up saying they don't have access.

Is there a way to disable the button if the user doesn't have access to open up the form of that button?

Thanks
Chris.
 
When in design mode, select form (a square where rulers should cross). In properties window go to event procedure 'on open' and go to code constructor.
If the button name is Command1, the code looks like:

[tt]Private Sub Form_Open(Cancel As Integer)
Me.Command1.Enabled = False
End Sub[/tt]

Of course, Enabled property can vary depending on access rights.
 
Is there a way to make a command button enabled if the user has access to the form that will open but disabled if the user does not have access to view the form that will open?

Example - On opening the database PortalFRM opens. PortalFRM has a command button on it called Rewiewbtn. Clicking Reviewbtn opens up ReviewFRM. In user permissions the user macfreak has Open/Run access to PortalFRM but no access to view ReviewFRM. At the moment if macfreak clicks Reviewbtn on PortalFRM they receive a msgbox saying 'You do not have permissions to open this form etc.....'. Instead I want Reviewbtn on PortalFRM to be disabled so it cannot be clicked on at all. If macfreaks user permissions were changed to allow Open/Run on ReviewFRM then Reviewbtn on PortalFRM should be enabled.

Thanks
Chris
 
Combo is correct...You might need a query to check for permissions, you will need someway to confirm/deny permissions.


Private Sub Form_Open(Cancel As Integer)
if Permissions = Yes then
Me.Command1.Enabled = true
Else
Me.Command1.Enabled = False
end if
End Sub


Better yet...

Private Sub Form_Open(Cancel As Integer)
if Permissions = Yes then
Me.Command1.visible = true
Else
Me.Command1.visible = False
end if
End Sub

Of course this code goes on the form u are in , not the one going to...


Jeremy
WZ
 
Here's a routine to check if you have permissions to access an object. The example below checks to see if the user has access rights to the table that the form is bound to.
Code:
Private Sub Form_Load()

    Dim varPerm As Variant
    
    varPerm = GetAllPermissions(CurrentUser, "Tables", "tblNameOfTable")

    If ((varPerm And dbSecReplaceData) <> dbSecReplaceData) Then
        MsgBox &quot;Warning!  Insufficient Privileges.&quot; & vbCrLf & vbCrLf & &quot;You are not allowed to modify the data on this form.  You may view the data, but you are not allowed to change it.&quot; & vbCrLf & vbCrLf & &quot;If you want to modify the data, contact your System Administrator.&quot;, vbInformation
    End If

End Sub

'+********************************************************************************************
'*
'$  Function:   GetAllPermissions
'*
'*  Author:     FancyPrairie
'*
'*  Date:       December, 1998
'*              (Converted for Access 2000, October, 2001)
'*
'*  Purpose:    Retrieve the &quot;ALL&quot; of the security permissions assigned to a user or group.
'*
'*  Inputs:     UserGrpName - name of a user or group account
'*              ObjClass    - name of an object container        (i.e. &quot;Forms&quot;)
'*              ObjName     - name of an object document         (i.e. &quot;frmUserProfile&quot;)
'*
'* Returns:     Value of Permissions property or error number that was generated.
'-********************************************************************************************
'
Option Compare Database
Option Explicit

Public Function GetAllPermissions(UserGrpName$, ObjClass$, ObjName$, Optional varDBS As Variant) As Variant
 
'********************************
'*  Declaration Specifications  *
'********************************

    Dim dbs As Database
    Dim DOC As Document
    
    Dim lngErr As Variant
    
    On Error GoTo ErrHandler
    
'*****************************
'*  Get Document variable.   *
'*****************************

    If (IsMissing(varDBS)) Then
        Set dbs = CurrentDb
    Else
        Set dbs = varDBS
    End If
    
    Set DOC = dbs.Containers(ObjClass).Documents(ObjName)    'set the DOC variable to the object specified in the arguments
 
'*****************************************************************************
'*  Set the UserName property of the document to the user or group you want  *
'*  to obtain the permissions for.                                           *
'*****************************************************************************

    DOC.username = UserGrpName
 
'********************************
'*  Get the permissions value.  *
'********************************

    GetAllPermissions = DOC.AllPermissions
 
'********************
'*  Exit Procedure  *
'********************
        
ExitProcedure:

    dbs.Close
    
    Exit Function

'****************************
'*  Error Recovery Section  *
'****************************
        
ErrHandler:

    Msgbox Err.Number & vbCrLf & Err.Description
    
    Resume ExitProcedure

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top