Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Portland, Maine, USA
    Posts
    173
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Enabling and disabling buttons by user level (XP/SP2)

    I'm trying to disable some buttons when my database first opens. I created a function below to accomplish this.

    <pre>Public Function DisableAddButtons()
    On Error GoTo DisableAddButtons_Error
    Dim grp As Group
    Dim strUser As String
    strUser = CurrentUser()

    For Each grp In DBEngine.Workspaces(0).Users(strUser).Groups

    If grp.Name = "GasbReadOnlyUsers" Then
    If Forms!frmSwitchBoard!cmdAdd.Enabled = True Then
    Forms!frmSwitchBoard!cmdAdd.Enabled = False
    End If
    If Forms!frmAddEditAss!cmdAdd.Enabled = True Then
    Forms!frmAddEditAss!cmdAdd.Enabled = False
    End If
    Exit For
    End If
    Next

    'Set grp = Nothing
    Exit_DisableAddButtons:
    Exit Function
    DisableAddButtons_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ")" _
    & "in procedure DisableAddButtons of Module basUtilities" _
    , vbExclamation, "Gasb Error"
    Resume Exit_DisableAddButtons
    End Function</pre>



    But I get an 'Error 13 Type mismatch'. When I go into dubug it's on the first line of the for loop.

    Can anyone see what I'm doing wrong?
    Thanks,
    Don

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Enabling and disabling buttons by user level (XP/SP2)

    There may be confusion about libraries. Try declaring grp explicitly as a DAO object:

    Dim grp As DAO.Group

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Portland, Maine, USA
    Posts
    173
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enabling and disabling buttons by user level (XP/SP2)

    That was exactly it! Thanks!
    I didn't have any references to the DAO object model. Since I have only been using ADO, I think I will find the ADO aquivelent for my code. It's probably not a good idea to make a reference to DAO just for a couple of functions...am I right? There must be an aquivelent in ADO that I can use, but I haven't looked into it just yet.

    Thanks for your help once again Hans!
    Don

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Enabling and disabling buttons by user level (XP/SP2)

    If you don't want to use DAO, you can use ADOX (Microsoft ADO Ext. 2.7 for DDL and Security) library for security-related functions. Sample code using ADOX:

    Private Sub DisableButtons(ByRef strGrp As String)
    On Error GoTo Err_Handler

    Dim cnn As ADODB.Connection
    Dim cat As New ADOX.Catalog
    Dim grp As ADOX.Group
    Dim usr As ADOX.User
    Dim strMsg As String

    Set cnn = CurrentProject.Connection
    cat.ActiveConnection = cnn
    Set usr = cat.Users(CurrentUser())

    For Each grp In usr.Groups
    If grp.NAME = strGrp Then
    Me.Button1.Enabled = Not Me.Button1.Enabled
    Exit For
    End If
    Next

    Exit_Sub:
    Set cnn = Nothing
    Set cat = Nothing
    Set grp = Nothing
    Set usr = Nothing
    Exit Sub
    Err_Handler:
    Select Case Err.Number
    Case 0
    Resume Next
    Case Else
    strMsg = "Error No " & Err.Number & ": " & Err.Description
    Beep
    MsgBox strMsg, vbExclamation, "ERROR MESSAGE"
    Resume Exit_Sub
    End Select

    End Sub

    Note that this is a Private Sub, not Public function, called from the form's OnOpen event (this is the application's startup form):

    Private Sub Form_Open(Cancel As Integer)

    ' Other code here...
    DisableButtons "NonAdminUsers"

    End Sub

    I'm not sure why you wouldn't run this code from the form itself, it would simplify the syntax. Also note, the If...Else statement can be greatly simplified. In above example, Button1 is enabled by default; if user opening app is a lowly non-Admin user, the button is disabled. To use ADOX methods, set a reference to the ADOX library (C:Program FilesCommon FilesSystemADOmsadox.dll) in References dialog, should be listed as "Microsoft ADO Ext. 2.7 for DDL and Security" - if using XP version number should be 2.7, if using A2K the version number will be lower (2.1 or 2.5). See ADO Help files for more info on using ADO/ADOX properties & methods.

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •