Search code examples
vbams-accesscontrolsshow-hide

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.


Solution

  • 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