Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Unanswered: Different checkbox based on user

    I have a simple table and a simple form that I want three different people to use.

    I capture their network ID upon opening the database.

    I want all three people to review all of the records.
    Then when they are complete, they will select a check box on the form.

    How can I display a different check box based on the user? The table has one field for each check box.

    Thanks in advance,
    Cliff

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Not sure to understand where the problem is, but you could place three checkboxes on the form and make them visible or not according to the user ID.

    Code:
    If UserID = "User1" then
        Me.Check_U1.Visible = True
        Me.Check_U2.Visible = False
        Me.Check_U3.Visible = False
    Else If UserID = "User2" then
    ..., etc.
    
    Endif
    Have a nice day!

  3. #3
    Join Date
    Mar 2010
    Posts
    6
    I'll give that a try in the morning, thank you very much. It sounds like what I am looking for.

    Not being a programmer, I suspect I am not going about this the best way possible.
    Our network does not allow us to use the built in user access that Access offers, so what I am doing is capturing the network log in in a separate form upon opening the database, minimizing that form, then I'll use the log in ID to decide which check box to show.
    Can you suggest a better way? This is a short term project with only three users, and the info I am hiding is not secure, I just want to make sure I capture who did what.

    Thanks again.

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    For knowing the user Id there is a better way. The code is in the code bank somewhere. Here is my own version:
    Code:
    Option Compare Database
    Option Explicit
    
    ' Windows API declarations
    '
    Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Public Function GetNTUser() As String
    '
    ' Retrieve the ID of the currently connected user
    '
        Dim strUserName As String
        Dim lngUserNameSize As Long
        
        strUserName = String$(255, 0)
        lngUserNameSize = Len(strUserName)
        GetUserName strUserName, lngUserNameSize
        strUserName = Left$(strUserName, lngUserNameSize - 1)
        GetNTUser = strUserName
        
    End Function
    It retrieves the Id used to open the Windows session on the computer where the program runs.

    You can create a new module and paste the code in it. When you need to retrieve the Id of the user somewhere in a program, you can use:
    Code:
    Dim MyUser as String
    MyUser = GetNTUser()
    By the way it's better to stay away from the built-in security of Access. It usually causes more trouble than it's worth.
    Have a nice day!

  5. #5
    Join Date
    Mar 2010
    Posts
    6
    Thanks, but I am afraid I am in over my head on this one.
    Can you point me to where I can learn more about how to do this? I do not know where to place the code you outlined.

    So if my form is to have three check boxes, where do I put the code to select which one to show, in an On Load event for the form?
    Also, where do I put the code to select the user? Does this go in the same event?
    Dim MyUser as String
    MyUser = GetNTUser()

    Many thanks

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You can put the code (the one in the fist Code window of my post) in a new module, as I already wrote before. The way to create a new module depends on the version of Access you are using. As you did not gave this info in your post, it's difficult to tell you precisely how to do it.

    In the form module, you can put the following code in the Sub Form_Open or in the Sub Form_Load procedure.
    Ex. (here in the Form_Open):
    Code:
    Private Sub Form_Open(Cancel As Integer)
    
        Dim UserID as String
        UserID = GetNTUser() 
        If UserID = "User1" then
            Me.Check_U1.Visible = True
            Me.Check_U2.Visible = False
            Me.Check_U3.Visible = False
        Else If UserID = "User2" then
            Me.Check_U1.Visible = False
            Me.Check_U2.Visible = True
            Me.Check_U3.Visible = False
        Else If UserID = "User3" then
            Me.Check_U1.Visible = False
            Me.Check_U2.Visible = False
            Me.Check_U3.Visible = True
        Endif
    
    End Sub
    Of course, you have to replace Check_U1, Check_U2 and Check_U3 by the name of the checkbox controls on your form and User1, User2 and User3 by the name of the three users that open the program.

    A more professional method would consist in having a table with the registered users, but the present code will be enough to begin with. Ask back when it works if you're interested in the aforementioned method.
    Have a nice day!

  7. #7
    Join Date
    Mar 2010
    Posts
    6
    Sinndho,

    I greatly appreciate your help.

    Is there supposed to be a space in Else If? I kept getting errors, so I changed it to ElseIf and the errors went away.

    For some reason your code to get the user ID won't run in my environment, I had one from Allen Browne (see attachment) that was working so I tried with that.

    If your willing to show the code for users in a table, I'll be very happy to learn from you. While I might not need it for this project, I can likely use it in the future.

    This is working now, and I really appreciate your detailed help!
    Attached Files Attached Files

Posting Permissions

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