Programatically load a form based on user login.

G

Guest

Scenario.

I initially created a switchboard using Switchboard, but based on previous
advise, I created two forms instead ; Manufacturing and Regulatory. These
forms are based on two groups that were created in Access security tool.
(These forms contain controls to other objects -,,,Forms and Reports"

What I would like is that; Once a user in the Manufacturing group logs into
the database, for example, the manufacturing form loads by default., and same
for the Regulatory group.

Another question, Once I move the orginial switchboard out of the Startup
option, Do I need to replace it with something else?
 
J

Joan Wild

You likely only need one form, just hide buttons from the CurrentUser()
based on their group membership.

However if you want to proceed with your setup, you could have a hidden form
that opens on startup. In its load event you can test which group the
current user is in and then open the appropriate form.

If faq_IsUserInGroup("Manufacturing",CurrentUser()) = True Then
DoCmd.OpenForm "Name of Manufacturing Form"
Else
DoCmd.OpenForm "Name of Regulatory Form"
End If

You do not have to have any form open on startup, however if you are using
the above, you'd have the hidden form open on startup
 
G

Guest

I am not sure what you mean by Hidden form. Once I open the database, I get
the default login prompt, based on the fact that the database has been
secured. How does the hidden form work in conjunction with the login prompt.?
 
J

Joan Wild

You would login via that login prompt. After that, the startup form would
open.

By hidden, I mean put
Me.Visible = False
in the Open event of your startup form and in the Load event test the
current user.

I still think you could manage this with one form, hiding various buttons
based on the user's membership.
 
G

Guest

I have decided to follow your advice and use one form. I just created a new
form with all the command buttons.

One Question. Where Do I declare the Function IF Faq_IsUserInGroup, in the
Open Event or the Load Event. Am I to Assume that the Open Event of the
form will only contain the (Me.Visible = False ) line.?
 
J

Joan Wild

The security FAQ:
http://support.microsoft.com/?id=207793
has the function FAQ_IsUserInGroup.

Just copy the function and paste it in a module. Save the module giving it
a name different that the function.

Since you are going with one form, you can go back to my original suggestion
of putting it in the open event of the form.

If faq_IsUserInGroup("Manufacturing",CurrentUser()) = True Then
Me!cmdSomething.Visible = True
Me!cmdWhatever.Visible = True
etc
Else
Me!cmdSomething.visible = false
Me!cmdWhatever.Visible = False
etc
End If

Substitue cmdSomething and cmdWhatever with the actual names of your command
buttons.
 
G

Guest

Sorry for sounding stupid.
But am I still creating the module or just copying the following code in the
Open Event of the form?. -

If faq_IsUserInGroup("Manufacturing",CurrentUser()) = True Then
Me!cmdSomething.Visible = True
Me!cmdWhatever.Visible = True

Else
Me!cmdSomething.visible = false
Me!cmdWhatever.Visible = False
etc
End If

Because When I tried it that way it says something about the Function not
being declared, So I am assuming that I have to do something in addition to
adding the code to the Open Event of the form.
 
J

Joan Wild

If you have an existing module, open it and paste the Function in it (if you
don't have a module then create a new one on the Modules tab).

Paste the function from the security FAQ (it's in section 22; just copy and
paste the code from 'Determine if a User is in a given Group:)

Function faq_IsUserInGroup (strGroup As String, strUser as String) As
Integer
.....
....
...

End Function

Now open your form in design view and bring up the properties sheet. In the
On Open property, click the dropdown and choose Event Procedure. Then click
on the build button(...) and put
If faq_IsUserInGroup("Manufacturing",CurrentUser()) = True then
Me!cmdSomething.Visible = True
Else
Me!cmdSomething.Visible = False
End If

Does that help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top