Tab Control - Turn Tabs ON/OFF using VBA

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Tab Control - Turn Tabs ON/OFF using VBA

Post by santosm »

Hi All,
I want to turn some tabs on or off depending on which user is logged in. Is this possible?

Thanks,
Mark
Thanks,
Mark

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Tab Control - Turn Tabs ON/OFF using VBA

Post by santosm »

Hey, I found out by poking around!

Something like this! Where the .pages(x) is the index number of the tab control.
Me.TabCtl18.Pages(2).Visible = True

Thanks,
Mark
Thanks,
Mark

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Tab Control - Turn Tabs ON/OFF using VBA

Post by HansV »

If you mean the Windows username:

Copy the following code into a standard module:

Code: Select all

Private Declare Function WNetGetUserA Lib "mpr.dll" _
    (ByVal lpszLocalName As String, ByVal lpszUserName As String, lpcchBuffer As Long) As Long

Public Function GetUser() As String
    Dim lpUserName As String, lpnLength As Long, lResult As Long
    'Create a buffer
    lpUserName = String(256, Chr$(0))
    'Get the network user
    lResult = WNetGetUserA(vbNullString, lpUserName, 256)
    If lResult = 0 Then
        GetUser = Left$(lpUserName, InStr(1, lpUserName, Chr$(0)) - 1)
    Else
        GetUser = "-unknown-"
    End If
End Function
The GetUser function returns the Windows username of the active user.

Create an On Load event procedure similar to the following for your form:

Code: Select all

Private Sub Form_Load()
    Select Case GetUser
        Case "HansV"
            Me.Page1.Visible = False
            Me.Page4.Visible = False
        Case "SantosM"
            Me.Page2.Visible = False
            Me.Page3.Visible = False
        Case Else
            Me.Page4.Visible = False
    End Select
End Sub
where Page1 etc. are the names of the tab pages.
Best wishes,
Hans

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Tab Control - Turn Tabs ON/OFF using VBA

Post by santosm »

Hi Hans,
Thanks! I may use that later, but for now I set a variable based on user location and will use that.

Thanks,
Mark
Thanks,
Mark

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Tab Control - Turn Tabs ON/OFF using VBA

Post by Pat »

Environ$("Username") also returns the windows user name

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Tab Control - Turn Tabs ON/OFF using VBA

Post by HansV »

That is true, but environment variables can easily be changed by the user, so a user could pretend to be someone else.
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Tab Control - Turn Tabs ON/OFF using VBA

Post by Pat »

I didn't know that, thank you