Open a Form and hide a command object button

  • Thread starter Alex219267 via AccessMonster.com
  • Start date
A

Alex219267 via AccessMonster.com

I would like to Open a form and hide an object command button. Code that I am
using is below:

Case 2 ' level2 menu
' validate password expiry
password_period = DLookup("[password_date]",
"tbl_users", "user_id = forms!frm_main!user_id")
If password_period < Date - 30 Then
strmsg = " Your password has expired. You must
change your password"
MsgBox strmsg, vbInformation, "Expired Password"
DoCmd.OpenForm "frm_change_password", acNormal
Else
DoCmd.Close
DoCmd.OpenForm "frmMainData" (I OPEN THE FORM
HERE)

The form opens with no problems, but there is button command in the form that
I want to hide...anyone have any ideas?

Thanks in advance.
 
P

PJFry

Set the buttons Visible property to No.

Or do you only want the button hidden for some users, not for others?

PJ
 
D

DStegon via AccessMonster.com

just make sure the the commnd button does not have the focus and use

me.buttonname.visible=False

in the Form Load event put

Me.Command0.SetFocus 'or whatever other control is on the form
Me.Command1.Visible = False ' the command button you want to make invisible

if the button has the focus the code will throw and error.

I would like to Open a form and hide an object command button. Code that I am
using is below:

Case 2 ' level2 menu
' validate password expiry
password_period = DLookup("[password_date]",
"tbl_users", "user_id = forms!frm_main!user_id")
If password_period < Date - 30 Then
strmsg = " Your password has expired. You must
change your password"
MsgBox strmsg, vbInformation, "Expired Password"
DoCmd.OpenForm "frm_change_password", acNormal
Else
DoCmd.Close
DoCmd.OpenForm "frmMainData" (I OPEN THE FORM
HERE)

The form opens with no problems, but there is button command in the form that
I want to hide...anyone have any ideas?

Thanks in advance.
 
A

Alex219267 via AccessMonster.com

Yes that is correct, users with level 2 Access would not be able to see the
command object. People with Level 1 Access would be able to see the command
object.
Set the buttons Visible property to No.

Or do you only want the button hidden for some users, not for others?

PJ
I would like to Open a form and hide an object command button. Code that I am
using is below:
[quoted text clipped - 17 lines]
Thanks in advance.
 
A

Alex219267 via AccessMonster.com

To clarify I am adding the whole code:

When the user log's into my application: they click on a button object after
inputting a user name and password:

Private Sub cmdOK_Click()
On Error GoTo Err_cmdOK_Click


' Validate User ID and Password and then display menu items
display_menu


Exit_cmdOK_Click:
Exit Sub

Err_cmdOK_Click:
MsgBox Err.Description
Resume Exit_cmdOK_Click
End Sub


display_Menu is sub module I created. The code is:

Option Compare Database
Option Explicit

Sub display_menu()
On Error GoTo err_display_menu

' at this stage the userId and access level has been checked
Dim access_level As Integer
Dim finish_Date As Date
Dim port_syd As String
Dim valid_user As Integer
Dim check_user As Integer
Dim password_period As Date
Dim check_password As String
Dim strmsg As String


valid_user = 2

' **********************************************
' validate user_id
' **********************************************
check_user = DCount("[user_id]", "tbl_users", "user_id=forms!frm_main!
user_id")
If check_user = 1 Then
valid_user = 2
Else
valid_user = 0
End If

' **********************************************
' validate password
' **********************************************
If valid_user = 2 Then
check_password = DLookup("[passwords]", "tbl_users", "user_id=forms!
frm_main!user_id")
If UCase(check_password) = UCase(Forms!frm_main!password) Then
valid_user = 2
Else
valid_user = 1
End If

End If

' **********************************************
' validate access_level
' **********************************************
If valid_user = 2 Then
access_level = DLookup("[access_level]", "tbl_users", "user_id=forms!
frm_main!user_id")
End If

Select Case valid_user

Case 0, 1
strmsg = " Access Denied" & _
vbCrLf & " Contact your IT Specialist if the problem
persists. "
MsgBox strmsg, vbInformation, "INVALID USER ID or PASSWORD"

' DoCmd.Quit

Case 2
Select Case access_level
Case 1 ' level1 menu
' validate password expiry
password_period = DLookup("[password_date]",
"tbl_users", "user_id = forms!frm_main!user_id")
If password_period < Date - 30 Then
strmsg = " Your password has expired. You must
change your password"
MsgBox strmsg, vbInformation, "Expired Password"
DoCmd.OpenForm "frm_change_password", acNormal
Else
DoCmd.Close
DoCmd.OpenForm "frmSwitchBoard"


End If

Case 2 ' level2 menu
' validate password expiry
password_period = DLookup("[password_date]",
"tbl_users", "user_id = forms!frm_main!user_id")
If password_period < Date - 30 Then
strmsg = " Your password has expired. You must
change your password"
MsgBox strmsg, vbInformation, "Expired Password"
DoCmd.OpenForm "frm_change_password", acNormal
Else
DoCmd.Close
DoCmd.OpenForm "frmMainData"

ONCE "frmMainData" (A FORM) Opens with no problem, however, the form contains
a button object that I don't want a user with LEVEL 2 access to see. The
button control name is btntomainform

When I enter:

Me.btnmainform.visible=false after: DoCmd.OpenForm"frmMainData" as
illustrated above and run my application by having a user with level 2 access
log in, I get an error message: COMPILE ERROR INVALID USE OF ME KEYWORD ???
?




just make sure the the commnd button does not have the focus and use

me.buttonname.visible=False

in the Form Load event put

Me.Command0.SetFocus 'or whatever other control is on the form
Me.Command1.Visible = False ' the command button you want to make invisible

if the button has the focus the code will throw and error.
I would like to Open a form and hide an object command button. Code that I am
using is below:
[quoted text clipped - 17 lines]
Thanks in advance.
 
A

Alex219267 via AccessMonster.com

Okay I think I got it:

If password_period < Date - 30 Then
strmsg = " Your password has expired. You must
change your password"
MsgBox strmsg, vbInformation, "Expired Password"
DoCmd.OpenForm "frm_change_password", acNormal
Else
DoCmd.Close
DoCmd.OpenForm "frmMainData"


End IF

Then I added this: forms!frmMainData!btnTomainForm.visible=False

And it seems to work good!!! When a user with level 2 access logs in, the
object is hidden!!

Am so happy!!! I have been breaking my head all day with this!!

Thank you everyone!!!



To clarify I am adding the whole code:

When the user log's into my application: they click on a button object after
inputting a user name and password:

Private Sub cmdOK_Click()
On Error GoTo Err_cmdOK_Click


' Validate User ID and Password and then display menu items
display_menu


Exit_cmdOK_Click:
Exit Sub

Err_cmdOK_Click:
MsgBox Err.Description
Resume Exit_cmdOK_Click
End Sub

display_Menu is sub module I created. The code is:

Option Compare Database
Option Explicit

Sub display_menu()
On Error GoTo err_display_menu

' at this stage the userId and access level has been checked
Dim access_level As Integer
Dim finish_Date As Date
Dim port_syd As String
Dim valid_user As Integer
Dim check_user As Integer
Dim password_period As Date
Dim check_password As String
Dim strmsg As String

valid_user = 2

' **********************************************
' validate user_id
' **********************************************
check_user = DCount("[user_id]", "tbl_users", "user_id=forms!frm_main!
user_id")
If check_user = 1 Then
valid_user = 2
Else
valid_user = 0
End If

' **********************************************
' validate password
' **********************************************
If valid_user = 2 Then
check_password = DLookup("[passwords]", "tbl_users", "user_id=forms!
frm_main!user_id")
If UCase(check_password) = UCase(Forms!frm_main!password) Then
valid_user = 2
Else
valid_user = 1
End If

End If

' **********************************************
' validate access_level
' **********************************************
If valid_user = 2 Then
access_level = DLookup("[access_level]", "tbl_users", "user_id=forms!
frm_main!user_id")
End If

Select Case valid_user

Case 0, 1
strmsg = " Access Denied" & _
vbCrLf & " Contact your IT Specialist if the problem
persists. "
MsgBox strmsg, vbInformation, "INVALID USER ID or PASSWORD"

' DoCmd.Quit

Case 2
Select Case access_level
Case 1 ' level1 menu
' validate password expiry
password_period = DLookup("[password_date]",
"tbl_users", "user_id = forms!frm_main!user_id")
If password_period < Date - 30 Then
strmsg = " Your password has expired. You must
change your password"
MsgBox strmsg, vbInformation, "Expired Password"
DoCmd.OpenForm "frm_change_password", acNormal
Else
DoCmd.Close
DoCmd.OpenForm "frmSwitchBoard"


End If

Case 2 ' level2 menu
' validate password expiry
password_period = DLookup("[password_date]",
"tbl_users", "user_id = forms!frm_main!user_id")
If password_period < Date - 30 Then
strmsg = " Your password has expired. You must
change your password"
MsgBox strmsg, vbInformation, "Expired Password"
DoCmd.OpenForm "frm_change_password", acNormal
Else
DoCmd.Close
DoCmd.OpenForm "frmMainData"

ONCE "frmMainData" (A FORM) Opens with no problem, however, the form contains
a button object that I don't want a user with LEVEL 2 access to see. The
button control name is btntomainform

When I enter:

Me.btnmainform.visible=false after: DoCmd.OpenForm"frmMainData" as
illustrated above and run my application by having a user with level 2 access
log in, I get an error message: COMPILE ERROR INVALID USE OF ME KEYWORD ???
?
just make sure the the commnd button does not have the focus and use
[quoted text clipped - 12 lines]
 

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