Hiding Fields depending on user access levels

  • Thread starter Thread starter Gareth Edmondson
  • Start date Start date
G

Gareth Edmondson

Hi,

I have created a database which users can log into. Usernames and passwords
are stored in a table as is a boolean field showing whether or not the user
is an administrator. It is crude, but it works for this purpose - the table
is called tbl_users.

When I open a form (frm_mainoptions) I need help telling access what the
level of user is so that certain fields are only show on the form if the
user is an administrator. So along the lines of (simple pseudocode):

open frm_mainoptions
read user_level_access
if administrator then show all fields
else
show normal user fields
end if
end

I did design two seperate forms and got these to open based on security
levels, but now want to develop my skills further by only using the one
window.

Your knowledge is appreciated. I am a newbie to this forum, so if it is the
wrong place for this question, please feel free to point me at an FAQ or ask
to post elsewhere.

best wishes

Gareth Edmondson
 
The code could be:


Dim strForm As String

strForm = "MainForm"
DoCmd.OpenForm strForm

If DLookup("IsAdmin", "tblSecurity") = True Then

' show extra contorls on form:

Forms(strForm)("BirthDay").Visible = True
Forms(strForm)("Weight").visble = True

Else
' hide the fields
Forms(strForm)("BirthDay").Visible = False
Forms(strForm)("Weight").visble = False

End If
 
Hi Gareth,

The way I generally do this type of thing is by using the tag property of
the control. The tag property isn't used by Access so you can assign any
values to it. First decide what you want to use as keywords for this
property. For example a tag of "Admin" might be assigned to the controls
that are available for Administrators. Then you can loop through the
controls collection and using the tag property, change the visible property
of the control accordingly. Combined with your logic for determining whether
someone is an administrator:

dim fAdmin as boolean

fAdmin=IsUserAdmin()
'
dim ctl as control
for each ctl in me.controls
if ctl.tag="Admin" then
if fAdmin then
ctl.visible=true
else
ctl.visible=false
endif
endif
next ctl
set ctl=nothing

I didn't give you the code for the IsUserAdmin function - give that a go on
your own and post back if you need help with it. Also, you didn't mention
whether or not you are using Access User Level security. I highly recommend
it - you can secure the db and use the builtin methods for managing users,
usergroups and passwords. You can still use logic like that above but it is
easier because you can easily determine whether a user is a member of a
particular group.
 
Back
Top