Navigation form: limit access to tabs (1 Viewer)

diS

Registered User.
Local time
Today, 01:30
Joined
Jan 16, 2015
Messages
17
I have navigation form with 2 tabs I'd like to limit access to users with certain type of account.

There are 2 tables in database that contains user names and types of accounts (tblSecurityLevel and tblUser), and they are used to set user name, login and security level.
It works this way: when you open form, it checks if PC name corresponds to name in UserLogin field (tblUser), if it does it gives appropriate access level depending on set user security, if PC name is not on the list user will be logged as guest.

So far, I used this code and it works great for limiting access to 1 tab:

Code:
Private Sub Form_Load()
Dim UserLogin As String
Dim userLevel As Integer
UserLogin = Environ("Username")
Me.TxtLogin = UserLogin
If IsNull(DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" & Me.TxtLogin & "'")) Then
    Me.TxtLogin = "Guest"
Else
userLevel = DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" & Me.TxtLogin & "'")
If userLevel = 1 Then
    Me.DodajPredmet.Enabled = True
Else
    Me.DodajPredmet.Enabled = False
End If
End If
End Sub


Is there a way to expand the code and add second tab (or even third) with this table structure (I'd like to be able to keep login which 1st checks for pc name).
I hope I've been clear in explaining the problem.

Any help is appreciated!
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:30
Joined
Feb 19, 2013
Messages
16,607
please indent you code - it is otherwise difficult to follow and will discourage people from helping. You get points for using the code tags - they will preserve the spacing - but no points for no indentation.

Code:
Private Sub Form_Load()
Dim UserLogin As String
Dim userLevel As Integer
 
    UserLogin = Environ("Username")
    Me.TxtLogin = UserLogin
    If IsNull(DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" & Me.TxtLogin & "'")) Then
        Me.TxtLogin = "Guest"
    Else
        userLevel = DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" & Me.TxtLogin & "'")
        If userLevel = 1 Then
            Me.DodajPredmet.Enabled = True
        Else
            Me.DodajPredmet.Enabled = False
        End If
    End If
 
End Sub
I'm not sure I understand your question - why is

Code:
 If userLevel = 1 Then
    Me.DodajPredmet.Enabled = True
    [COLOR=red]Me.anothertab.Enabled = True
[/COLOR]Else
    Me.DodajPredmet.Enabled = False
    [COLOR=red]Me.anothertab.Enabled = False[/COLOR]
 End If
not suitable?
 

diS

Registered User.
Local time
Today, 01:30
Joined
Jan 16, 2015
Messages
17
Sorry about code look, I am pretty newbie with coding.. but I'll keep that in mind next time.

Your suggestion works great, thank you very much!

Also, is there a way to set default tab to be opened when navigation form is open?
e.g. we have 3 tabs on navigation form, they are displayed in this order: Tab1, Tab2 and Tab3.

Is there a way to display Tab3 on start (and not Tab1 which Access does as default)?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:30
Joined
Feb 19, 2013
Messages
16,607
not sure, but I would think it is possible

You probably need to set the focus to tab3 which you can do in the form open event. It will be something like

me.tab3.setfocus
 

diS

Registered User.
Local time
Today, 01:30
Joined
Jan 16, 2015
Messages
17
I finally get some time for this..

Setfocus method on open event:
forms![Navigation Form].form.[SubFormName].setfocus

just (as it is written) set focus on subform tab, it does not open the subform.

Got it working with:

Code:
Private Sub Form_Open(Cancel As Integer)
        DoCmd.BrowseTo acBrowseToForm, "frmPregledPredmeta", "Navigation Form.NavigationSubForm", , , acFormEdit
End Sub
 

cstickman

Registered User.
Local time
Yesterday, 18:30
Joined
Nov 10, 2014
Messages
109
Hi CJ and DiS

I would like to achieve the same thing as DiS on my navigation form. The problem is I do not know how the tables are laid out to match them. Here is what I have:
tbluser:
userlogin
Username
Security - either Admin/Guest

I would like to have Admin to have all tabs and then Guest only have two tabs. I figured out how to enable the other tabs. How do I change the code around to work with that code? Thanks
 

Users who are viewing this thread

Top Bottom