Malikx
Malikx

Reputation: 65

Show/Hide different Controls for different Users MS Access Form

I've designed a login form in MS Access. I've different user roles and want to display different controls to at different user logins. For instance, if an admin is logged in, the controls should be different and a normal user should be able to use different controls.

The vba code I've written for SignIn button on click is as follows (this code is for Login_Form):

Private Sub Btn_SignIn_Click()

IF Me.Txt_UserID.Value = "admin" AND Me.Txt_Password = "123admin"  
                                 AND Me.Cmbo_UserRole.Value = "DBA"  
THEN

    MsgBox "Welcome to RMS", vbOKOnly, "Logged in as Admin!"

    DoCmd.OpenForm "Main_Form"
    --How can I show/hide controls here at Main_Form
End If  

Main_Form has different controls, but I'm unable to access Main_Form controls inside Btn_SignIn_Click() function. So that, I might be able to show or hide controls.

Upvotes: 2

Views: 2574

Answers (2)

user8753746
user8753746

Reputation:

You can create a Procedure to validate if the user is admin or not. For example:

Private Sub Main_Form_Load(UserLevel as String)

  If UserLevel = "admin" Then
     Your code here to show controls.
  End If
End Sub

I suggest to hide the controls by default that should be used only by the Admin.

Then you can call the UserForm sending parameters as other solver in other question:

Passing parameters between forms in MS Access

Upvotes: 1

iDevlop
iDevlop

Reputation: 25272

with forms("main_form")
   !control1.visible = true
   !control2.visible = true
end with

If there are lots of such controls, you can also use

for each ctl in form_main_form
   ctl.visible = true
next ctl

Upvotes: 1

Related Questions