Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2012
    Posts
    79

    Unanswered: Enable controls based on value in tablefield

    Hi all,

    I have a pretty simple permissions setup going on my database but can't quite get it to work.

    Essentially, I have a table named Users with two fields: username, and userlevel.

    I would like to perform a IF THEN statement where:

    If userlevel = 1 then
    cmdGO.Enabled = True
    Elseif userlevel = 2 Then
    cmdGO.Enabled = False

    I don't quite understand the concept of looping through the fields of a table and how to implement that in VBA. Could someone explain it to me and provide an example? I have other applications I would like to use this for.
    Version: Access 2010

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Here's an example:
    Code:
    Sub SetUserPermission(Byval UserName as String)
     
        Const c_Criteria As String = "username= '@U'"
        
        Dim lngUserLevel as Long
    
        lngUserLevel = Nz(DLookup("userlevel", "Users", Replace(c_Criteria, "@U", UserName), 0)
        If lngUserLevel = 1 then 
            Me.cmdGO.Enabled = True
        ElseIf lngUserLevel = 2 Then
            Me.cmdGO.Enabled = False
        End If
    
    End Sub
    Note: If there are many levels, a Select Case statement would be more readable than a serie of If... ElseIf... ElseIf... End If:
    Code:
        Select Case lngUserLevel 
            Case 1:    Me.cmdGO.Enabled = True
            Case 2:    Me.cmdGO.Enabled = False
            Case 3:   '... etc.
        End Select
    Have a nice day!

  3. #3
    Join Date
    Nov 2012
    Posts
    79
    Lovely! Thanks for the help!
    Version: Access 2010

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome!
    Have a nice day!

Posting Permissions

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