Using module to modify form

kaylachris

Registered User.
Local time
Today, 00:46
Joined
Jun 9, 2010
Messages
10
I've managed to create a form that accepts usernames and passwords and once a user enters a correct username/password the form closes and opens the mainpage of the database (simply has many cmd buttons to allow users to open reports, view/update data etc..). Each user has a different level of access (there are a total of 3 levels of access). Instead of creating a form for each level of access that has the appropriate buttons I'm hoping to use one form and simply use some form of VBA to enable/disable the buttons. I'm thinking this would be best done in a module, however, I'm not sure of how to set text box/cmd button properties from inside a module... if this is even possiable.

Should I attempt to use only one form w/ VBA to enable/disable all the buttons depending on access level or just stick w/ my current plan and make 3 seperate forms, one for each level?
 
I would use one form and use the TAG property of the controls.

For example, I had an application I worked on which had 4 levels of users - Admin, Add/Edit, Edit Only, and Read Only. I used the abbreviations of:

Admin
AE
EO
RO

I had a hidden form which remained open and had the user's security level in an unbound control which I could then reference at any time.

And then I put in the tag those that were applicable for that control. So, if the Admin User (who pretty much was on everything) and a Read Only user was to have access I would use:

Admin,RO

in the TAG property of the control. All controls were set to Invisible by default.

So using a function in a STANDARD MODULE, we can then do:

Code:
Function SetSec(frm As Form) 
   Dim ctl As Control

For Each ctl In frm.Controls
   If Len(ctl.Tag & "") > 0 Then
       ctl.Visible = (Instr(1, ctl.Tag, Forms!YourUserSecurityLevel) > 0)
   End If
Next ctl

End Function
 
I would always lean towards one form myself. You can easily manipulate the controls from a module:

Forms!FormName.ControlName.Enabled = True
 
You could have a hidden form with their access level on and 3 if statements.

If admin

make all admin functions visible and non admin non.visible.
You could then also use vba to change the height of the form and the width.

It is long winded but works.

Thanks :o
 
I too would be inclined to use one form and use a module to iterate through the controls on the form.

You can cache the user's level in the OpenArgs argument of the OpenForm method. Useful so you don't have to query a table to get their level. Downside is if the level is changed whilst the form is open then any other level-restrictions you would want to apply wouldn't take effect.

A hidden listbox filtered to show ONLY the user's level can also be used. With this, if their rights are changed whilst the form is open it will be accurate. Downside, you have to put it on every form you would want to have level restrictions applied.

Or just use DLookup() for everytime you want to check the user's level.

With respect to checking controls, Bob mentioned using Tags, just bear in mind he means using tags for ONLY those controls that you want a restriction on.
 
Thank you all for your response. I've actually used a little bit of all the input.
 
boblarson....I've tried modifying your code a bit to get it to work for me but can't. How would I go about calling this?

Code:
Function SetSec(frm As Form)
   Dim ctl As Control
For Each ctl In frm.Controls
   If Len(ctl.Tag & "") > 0 Then
       'access_level
       ctl.Visible = (InStr(1, ctl.Tag, access_level) > 0)
   End If
Next ctl
End Function

I am attempting to call it inside another module which determines the users access level, verifies username and passwords etc... The form name in which I want to adjust the controls in is called MainPage... I've tried calling it using SetSec (MainPage),SetSec ("MainPage"),SetSec ([MainPage]) all of which produced an error... Trying to use this code so that I don't have to rewrite code for multipule forms :)
 
Like this:

SetSec Me

Me is your form/report object.
 
Thanks vbaInet... put that in my On Load event and it works perfect.... so glad I don't have to write nearly as much code now. Thanks everyone for the help
 

Users who are viewing this thread

Back
Top Bottom