Disable Command Button

D

DeanT

I have a database with a logon table which contains UserID, password,
PermissionLevel

When user logs in with proper password, the main menu form is opened. On the
main menu form are several command buttons which open other forms. I want
these command buttons to be enabled/disabled depending on the user’s
permission level. Level 1 enables, level 0 disables.

How do I code to do this and where do I enter code.?

Thanks for your help.
 
M

Marshall Barton

DeanT said:
I have a database with a logon table which contains UserID, password,
PermissionLevel

When user logs in with proper password, the main menu form is opened. On the
main menu form are several command buttons which open other forms. I want
these command buttons to be enabled/disabled depending on the user’s
permission level. Level 1 enables, level 0 disables.

How do I code to do this and where do I enter code.?


Put the code ib the main menu form's Open (or Load) event
procedure. Since you didn't say where/how the permission
level value gets from the logon form to the main menu form,
I will assume it's in a global variable named
UserPermission:

Me.button1.Enabled = (UserPermission=1)
Me.button2.Enabled = (UserPermission=1)
. . .
 
D

DeanT

How do I get the PermissionLevel value from the logon table to a Global
Variable named UserPermission ?
 
J

J_Goddard via AccessMonster.com

In a code module (not one for a form), declare the UserPermission variable:

Global UserPermission as Integer



You could then use the Dlookup function in the logon form

UserPermission = DLookup("[PermissionLevel]","[Logon Table]", "[UserId] = '"
& me![UserID] & "'")

once the user has entered a valid User ID, and then use Marshall's suggested
code to enable/disable command buttons.

John



How do I get the PermissionLevel value from the logon table to a Global
Variable named UserPermission ?
[quoted text clipped - 15 lines]
Me.button2.Enabled = (UserPermission=1)
. . .
 
M

Marshall Barton

We need to know what the logon form is doing, how you are
deriving the user permission level and where you are putting
it. It might be as simple as what Dean suggested, or ???

My use of a global variable was just so I could demonstrate
how to enable/disable controls. I did not mean to imply
that it was a good way to do it. A (hidden?) text box on an
always open form would be better. You might already have
that or you might be doing someting else with the value.
 
D

DeanT

I am having trouble putting this all together:

My UserTable has following fields:

UserName Text
Password Text
Permissions Number


When I click the Logon command button on the Logon form the following
procedure executes:

Option Compare Database


Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName=" &
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"
DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Where do I insert the DLookup function you suggested and where do I insert
the Global Permission as Integer code?


Sorry for my ignorance, I’m just getting started with coding.

Thanks for your help>


Marshall Barton said:
We need to know what the logon form is doing, how you are
deriving the user permission level and where you are putting
it. It might be as simple as what Dean suggested, or ???

My use of a global variable was just so I could demonstrate
how to enable/disable controls. I did not mean to imply
that it was a good way to do it. A (hidden?) text box on an
always open form would be better. You might already have
that or you might be doing someting else with the value.
--
Marsh
MVP [MS Access]

How do I get the PermissionLevel value from the logon table to a Global
Variable named UserPermission ?
 
J

J_Goddard via AccessMonster.com

You would put the DLookup after the DoCmd.close for the login form, so that
the UserPermission global variable is set before the ComplaintForm is opened.

Can I ask what the purpose of strUser = Me.UserName & "" is?

John



I am having trouble putting this all together:

My UserTable has following fields:

UserName Text
Password Text
Permissions Number

When I click the Logon command button on the Logon form the following
procedure executes:

Option Compare Database

Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName=" &
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"
DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Where do I insert the DLookup function you suggested and where do I insert
the Global Permission as Integer code?

Sorry for my ignorance, I’m just getting started with coding.

Thanks for your help>
We need to know what the logon form is doing, how you are
deriving the user permission level and where you are putting
[quoted text clipped - 28 lines]
 
M

Marshall Barton

Where do you declare strUser? I would expect the permission
value to be in the same place. But if you never did declare
those variables, they won't be around later when you need
them.

Assuming you are doing what John suggested about declaring
them Public in a standard module, then assign the permission
DLookup to the variable right after you verify the password
(or any later time when you need to check it).

As an aside, you need to be aware that any enterprising
person can print out a copy of your user table to get a list
of all the passwords.
 
D

DeanT

What am I doing wrong?

Here is my coding below and when I execute the Logon procedure I get an
error msg #2467 "The expression you entered refers to an object that is
closed or doesn't exist" and the debugger highlights the 'UserPermission =
DLookup..... line.

Hereis my module coding;


Option Compare Database

Public UserPermission As Integer
Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName=" &
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"

UserPermission = DLookup("[Permission]", "[UserTable]", "[UserName] = '"
& Me![UserName] & "'")

DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Marshall Barton said:
Where do you declare strUser? I would expect the permission
value to be in the same place. But if you never did declare
those variables, they won't be around later when you need
them.

Assuming you are doing what John suggested about declaring
them Public in a standard module, then assign the permission
DLookup to the variable right after you verify the password
(or any later time when you need to check it).

As an aside, you need to be aware that any enterprising
person can print out a copy of your user table to get a list
of all the passwords.
--
Marsh
MVP [MS Access]

I am having trouble putting this all together:

My UserTable has following fields:

UserName Text
Password Text
Permissions Number


When I click the Logon command button on the Logon form the following
procedure executes:

Option Compare Database


Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName=" &
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"
DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Where do I insert the DLookup function you suggested and where do I insert
the Global Permission as Integer code?
 
D

Douglas J. Steele

See whether it makes a difference putting the DoCmd.Close after you open the
other form.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


DeanT said:
What am I doing wrong?

Here is my coding below and when I execute the Logon procedure I get an
error msg #2467 "The expression you entered refers to an object that is
closed or doesn't exist" and the debugger highlights the 'UserPermission =
DLookup..... line.

Hereis my module coding;


Option Compare Database

Public UserPermission As Integer
Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName=" &
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"

UserPermission = DLookup("[Permission]", "[UserTable]", "[UserName] =
'"
& Me![UserName] & "'")

DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Marshall Barton said:
Where do you declare strUser? I would expect the permission
value to be in the same place. But if you never did declare
those variables, they won't be around later when you need
them.

Assuming you are doing what John suggested about declaring
them Public in a standard module, then assign the permission
DLookup to the variable right after you verify the password
(or any later time when you need to check it).

As an aside, you need to be aware that any enterprising
person can print out a copy of your user table to get a list
of all the passwords.
--
Marsh
MVP [MS Access]

I am having trouble putting this all together:

My UserTable has following fields:

UserName Text
Password Text
Permissions Number


When I click the Logon command button on the Logon form the following
procedure executes:

Option Compare Database


Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName="
&
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"
DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Where do I insert the DLookup function you suggested and where do I
insert
the Global Permission as Integer code?


:

We need to know what the logon form is doing, how you are
deriving the user permission level and where you are putting
it. It might be as simple as what Dean suggested, or ???

My use of a global variable was just so I could demonstrate
how to enable/disable controls. I did not mean to imply
that it was a good way to do it. A (hidden?) text box on an
always open form would be better. You might already have
that or you might be doing someting else with the value.

DeanT wrote:
How do I get the PermissionLevel value from the logon table to a
Global
Variable named UserPermission ?

:

DeanT wrote:

I have a database with a logon table which contains UserID,
password,
PermissionLevel

When user logs in with proper password, the main menu form is
opened. On the
main menu form are several command buttons which open other forms.
I want
these command buttons to be enabled/disabled depending on the user's
permission level. Level 1 enables, level 0 disables.

How do I code to do this and where do I enter code.?


Put the code ib the main menu form's Open (or Load) event
procedure. Since you didn't say where/how the permission
level value gets from the logon form to the main menu form,
I will assume it's in a global variable named
UserPermission:

Me.button1.Enabled = (UserPermission=1)
Me.button2.Enabled = (UserPermission=1)
. . .
 
D

DeanT

That prevents the error message, however when I add the following to the OPen
event of the Complaintform:

Me.button1.Enabled = (userPermission=1)

It always disables the button no matter what value is in the permission
table.?????

Douglas J. Steele said:
See whether it makes a difference putting the DoCmd.Close after you open the
other form.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


DeanT said:
What am I doing wrong?

Here is my coding below and when I execute the Logon procedure I get an
error msg #2467 "The expression you entered refers to an object that is
closed or doesn't exist" and the debugger highlights the 'UserPermission =
DLookup..... line.

Hereis my module coding;


Option Compare Database

Public UserPermission As Integer
Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName=" &
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"

UserPermission = DLookup("[Permission]", "[UserTable]", "[UserName] =
'"
& Me![UserName] & "'")

DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Marshall Barton said:
Where do you declare strUser? I would expect the permission
value to be in the same place. But if you never did declare
those variables, they won't be around later when you need
them.

Assuming you are doing what John suggested about declaring
them Public in a standard module, then assign the permission
DLookup to the variable right after you verify the password
(or any later time when you need to check it).

As an aside, you need to be aware that any enterprising
person can print out a copy of your user table to get a list
of all the passwords.
--
Marsh
MVP [MS Access]


DeanT wrote:
I am having trouble putting this all together:

My UserTable has following fields:

UserName Text
Password Text
Permissions Number


When I click the Logon command button on the Logon form the following
procedure executes:

Option Compare Database


Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName="
&
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"
DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Where do I insert the DLookup function you suggested and where do I
insert
the Global Permission as Integer code?


:

We need to know what the logon form is doing, how you are
deriving the user permission level and where you are putting
it. It might be as simple as what Dean suggested, or ???

My use of a global variable was just so I could demonstrate
how to enable/disable controls. I did not mean to imply
that it was a good way to do it. A (hidden?) text box on an
always open form would be better. You might already have
that or you might be doing someting else with the value.

DeanT wrote:
How do I get the PermissionLevel value from the logon table to a
Global
Variable named UserPermission ?

:

DeanT wrote:

I have a database with a logon table which contains UserID,
password,
PermissionLevel

When user logs in with proper password, the main menu form is
opened. On the
main menu form are several command buttons which open other forms.
I want
these command buttons to be enabled/disabled depending on the user's
permission level. Level 1 enables, level 0 disables.

How do I code to do this and where do I enter code.?


Put the code ib the main menu form's Open (or Load) event
procedure. Since you didn't say where/how the permission
level value gets from the logon form to the main menu form,
I will assume it's in a global variable named
UserPermission:

Me.button1.Enabled = (UserPermission=1)
Me.button2.Enabled = (UserPermission=1)
. . .
 
D

Douglas J. Steele

That's because ComplaintForm doesn't know anything about variables defined
by the Logon form, even through you've declared them as Public. Not only
that, but you've closed the Logon form, so that variable doesn't even exist
anymore!

Try passing the value of userPermission to the ComplaintForm as the OpenArgs
parameter:

DoCmd.OpenForm "ComplaintForm", acNormal, OpenArgs:=userPermission

(There's really no need for the other parameters you had in your OpenForm
method, as they were all the defaults)

Then, in ComplaintForm, try:

Me.button1.Enabled = (Nz(Me.OpenArgs, 0) = 1)

Incidentally, if the Logon form was still open, so that the userPermission
variable still existed, you could refer to it as

Form_Logon.UserPermission

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


DeanT said:
That prevents the error message, however when I add the following to the
OPen
event of the Complaintform:

Me.button1.Enabled = (userPermission=1)

It always disables the button no matter what value is in the permission
table.?????

Douglas J. Steele said:
See whether it makes a difference putting the DoCmd.Close after you open
the
other form.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


DeanT said:
What am I doing wrong?

Here is my coding below and when I execute the Logon procedure I get an
error msg #2467 "The expression you entered refers to an object that
is
closed or doesn't exist" and the debugger highlights the
'UserPermission =
DLookup..... line.

Hereis my module coding;


Option Compare Database

Public UserPermission As Integer
Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName="
&
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"

UserPermission = DLookup("[Permission]", "[UserTable]", "[UserName]
=
'"
& Me![UserName] & "'")

DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

:

Where do you declare strUser? I would expect the permission
value to be in the same place. But if you never did declare
those variables, they won't be around later when you need
them.

Assuming you are doing what John suggested about declaring
them Public in a standard module, then assign the permission
DLookup to the variable right after you verify the password
(or any later time when you need to check it).

As an aside, you need to be aware that any enterprising
person can print out a copy of your user table to get a list
of all the passwords.
--
Marsh
MVP [MS Access]


DeanT wrote:
I am having trouble putting this all together:

My UserTable has following fields:

UserName Text
Password Text
Permissions Number


When I click the Logon command button on the Logon form the following
procedure executes:

Option Compare Database


Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable",
"UserName="
&
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"
DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Where do I insert the DLookup function you suggested and where do I
insert
the Global Permission as Integer code?


:

We need to know what the logon form is doing, how you are
deriving the user permission level and where you are putting
it. It might be as simple as what Dean suggested, or ???

My use of a global variable was just so I could demonstrate
how to enable/disable controls. I did not mean to imply
that it was a good way to do it. A (hidden?) text box on an
always open form would be better. You might already have
that or you might be doing someting else with the value.

DeanT wrote:
How do I get the PermissionLevel value from the logon table to a
Global
Variable named UserPermission ?

:

DeanT wrote:

I have a database with a logon table which contains UserID,
password,
PermissionLevel

When user logs in with proper password, the main menu form is
opened. On the
main menu form are several command buttons which open other
forms.
I want
these command buttons to be enabled/disabled depending on the
user's
permission level. Level 1 enables, level 0 disables.

How do I code to do this and where do I enter code.?


Put the code ib the main menu form's Open (or Load) event
procedure. Since you didn't say where/how the permission
level value gets from the logon form to the main menu form,
I will assume it's in a global variable named
UserPermission:

Me.button1.Enabled = (UserPermission=1)
Me.button2.Enabled = (UserPermission=1)
. . .
 
M

Marshall Barton

DeanT said:
What am I doing wrong?

Here is my coding below and when I execute the Logon procedure I get an
error msg #2467 "The expression you entered refers to an object that is
closed or doesn't exist" and the debugger highlights the 'UserPermission =
DLookup..... line.

Hereis my module coding;


Option Compare Database

Public UserPermission As Integer
Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName=" &
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"

UserPermission = DLookup("[Permission]", "[UserTable]", "[UserName] = '"
& Me![UserName] & "'")

DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub


A form's module is a class module, not a standard module.
To create a standard module, click in the database window,
then select the Modules tab and click the New button.

The Public variables declared in a class module only exist
while the class is instantiated (e.g. its form is open).

Doug's ideas are another way to get the permission level
value to the complaint form. However, I suspect that you
will eventually need to use the user name and permission
level value in other places in your program. If so, you
will need to save those values in a safe place. A standard
module is probably the easiest for you to deal with at this
point of your experience, but it is not very safe.

As I said before, if you did not close the logon form then
keeping the variables in the logon form's class module (as
you now have it) would be better. So, I recommend that you
do not close the logon form and instead just make it
invisible:
Me.Visible = False

Then anywhere in your application you can check the
permission level by using a reference like:
Forms!Logon.UserPermission
 
D

DeanT

I tried not closing the logon form and everything worked great. Thanks for
the suggestion.

Douglas J. Steele said:
That's because ComplaintForm doesn't know anything about variables defined
by the Logon form, even through you've declared them as Public. Not only
that, but you've closed the Logon form, so that variable doesn't even exist
anymore!

Try passing the value of userPermission to the ComplaintForm as the OpenArgs
parameter:

DoCmd.OpenForm "ComplaintForm", acNormal, OpenArgs:=userPermission

(There's really no need for the other parameters you had in your OpenForm
method, as they were all the defaults)

Then, in ComplaintForm, try:

Me.button1.Enabled = (Nz(Me.OpenArgs, 0) = 1)

Incidentally, if the Logon form was still open, so that the userPermission
variable still existed, you could refer to it as

Form_Logon.UserPermission

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


DeanT said:
That prevents the error message, however when I add the following to the
OPen
event of the Complaintform:

Me.button1.Enabled = (userPermission=1)

It always disables the button no matter what value is in the permission
table.?????

Douglas J. Steele said:
See whether it makes a difference putting the DoCmd.Close after you open
the
other form.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


What am I doing wrong?

Here is my coding below and when I execute the Logon procedure I get an
error msg #2467 "The expression you entered refers to an object that
is
closed or doesn't exist" and the debugger highlights the
'UserPermission =
DLookup..... line.

Hereis my module coding;


Option Compare Database

Public UserPermission As Integer
Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable", "UserName="
&
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"

UserPermission = DLookup("[Permission]", "[UserTable]", "[UserName]
=
'"
& Me![UserName] & "'")

DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

:

Where do you declare strUser? I would expect the permission
value to be in the same place. But if you never did declare
those variables, they won't be around later when you need
them.

Assuming you are doing what John suggested about declaring
them Public in a standard module, then assign the permission
DLookup to the variable right after you verify the password
(or any later time when you need to check it).

As an aside, you need to be aware that any enterprising
person can print out a copy of your user table to get a list
of all the passwords.
--
Marsh
MVP [MS Access]


DeanT wrote:
I am having trouble putting this all together:

My UserTable has following fields:

UserName Text
Password Text
Permissions Number


When I click the Logon command button on the Logon form the following
procedure executes:

Option Compare Database


Private Sub Command4_Click()
strUser = Me.UserName & ""

If StrComp(Me.Password, Nz(DLookup("Password", "UserTable",
"UserName="
&
"""" & strUser & """"), ""), 0) = 0 Then
DoCmd.Close acForm, "Logon"
DoCmd.OpenForm "ComplaintForm", acNormal, "", "", , acNormal
Else
MsgBox "Invalid Username or Password"
End If

End Sub

Where do I insert the DLookup function you suggested and where do I
insert
the Global Permission as Integer code?


:

We need to know what the logon form is doing, how you are
deriving the user permission level and where you are putting
it. It might be as simple as what Dean suggested, or ???

My use of a global variable was just so I could demonstrate
how to enable/disable controls. I did not mean to imply
that it was a good way to do it. A (hidden?) text box on an
always open form would be better. You might already have
that or you might be doing someting else with the value.

DeanT wrote:
How do I get the PermissionLevel value from the logon table to a
Global
Variable named UserPermission ?

:

DeanT wrote:

I have a database with a logon table which contains UserID,
password,
PermissionLevel

When user logs in with proper password, the main menu form is
opened. On the
main menu form are several command buttons which open other
forms.
I want
these command buttons to be enabled/disabled depending on the
user's
permission level. Level 1 enables, level 0 disables.

How do I code to do this and where do I enter code.?


Put the code ib the main menu form's Open (or Load) event
procedure. Since you didn't say where/how the permission
level value gets from the logon form to the main menu form,
I will assume it's in a global variable named
UserPermission:

Me.button1.Enabled = (UserPermission=1)
Me.button2.Enabled = (UserPermission=1)
. . .
 

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