none
Cmd Button visible determined by Win Login Name RRS feed

  • Question

  • Hello,

    I have a Access 2010 database on a Win7 machine. I'm using the VBA below to obtain the Windows user name. The Windows user name appears in a text box named txtWinUserID in a form named frmMain. The form has a command button named cmdWIP where the visible property is set to No. The database also has a table named DbUsers. The table has the following fields: UserName; WinLogin; WIP. I want the cmdWIP to be visible by comparing the txtWinUserID on the frmMain to the WinLogin field in DbUsers table where the WIP field is equal to Y. Can someone provide me with the VB to perform this?

    Public Function getUserName() As String
        getUserName = Environ("USERNAME")
    End Function
    

    Thank you for your help.

    Kevin

    Thursday, May 31, 2018 1:03 PM

Answers

  • Hi Kevin,

    It sort of does. I thought the WIP field was a Yes/No (checkbox) field. If it's not, and it's a Text field instead, then we can adjust the code to something more like:

    Me.cmdWIP.Visible = Nz(DLookup("WIP","DbUsers","WinLogin='" & getUserName() & "'"),"N")="Y"

    Also, I assumed the WinLogin field contains the same information as what you get with getUserName() (or the user's network login name).

    Hope it helps...


    • Edited by .theDBguy Thursday, May 31, 2018 3:22 PM
    • Marked as answer by KevinATF Thursday, May 31, 2018 3:25 PM
    Thursday, May 31, 2018 3:22 PM
  • Hi DBguy,

    I adjusted my code as follows and seems to work:

    Private Sub Form_Open(Cancel As Integer)
        If DLookup("WIP", "DbUsers", "WinLogin='" & getUserName() & "'") = "Y" Then
            Me.cmdWIP.Visible = True
        Else
            Me.cmdWIP.Visible = False
        End If
    End Sub

    Thanks for your help with this project!

    Kevin

    • Marked as answer by KevinATF Thursday, May 31, 2018 3:25 PM
    Thursday, May 31, 2018 3:25 PM

All replies

  • Hi Kevin,

    In the Open event of frmMain, you could try something like:

    Me.cmdWIP.Visible = DLookup("WIP","DbUsers","WinLogin='" & getUserName() & "'")

    Hope it helps...

    Thursday, May 31, 2018 2:51 PM
  • Hi DBguy,

    Thanks for your suggestion. I have a concern. The DbUsers table contain users that should not have the cmdWIP command button visible. Only those users with a "Y" in the WIP field should have the cmdWIP command button visible.  Does your suggestion take this into account?

    Thanks, Kevin


    Thursday, May 31, 2018 3:18 PM
  • Hi Kevin,

    It sort of does. I thought the WIP field was a Yes/No (checkbox) field. If it's not, and it's a Text field instead, then we can adjust the code to something more like:

    Me.cmdWIP.Visible = Nz(DLookup("WIP","DbUsers","WinLogin='" & getUserName() & "'"),"N")="Y"

    Also, I assumed the WinLogin field contains the same information as what you get with getUserName() (or the user's network login name).

    Hope it helps...


    • Edited by .theDBguy Thursday, May 31, 2018 3:22 PM
    • Marked as answer by KevinATF Thursday, May 31, 2018 3:25 PM
    Thursday, May 31, 2018 3:22 PM
  • Hi DBguy,

    I adjusted my code as follows and seems to work:

    Private Sub Form_Open(Cancel As Integer)
        If DLookup("WIP", "DbUsers", "WinLogin='" & getUserName() & "'") = "Y" Then
            Me.cmdWIP.Visible = True
        Else
            Me.cmdWIP.Visible = False
        End If
    End Sub

    Thanks for your help with this project!

    Kevin

    • Marked as answer by KevinATF Thursday, May 31, 2018 3:25 PM
    Thursday, May 31, 2018 3:25 PM
  • Hi Kevin,

    You're welcome. Glad to hear you got it to work. Good luck with your project.

    Thursday, May 31, 2018 3:38 PM