If..Me problem

  • Thread starter Thread starter DubboPete
  • Start date Start date
D

DubboPete

Hi all,

I have a form for user administration.
Only managers ([userlevel] = 2) and administrators ([userlevel] = 3) can
access this page.

By means of a combo box selection I can display any user's details on the
form, including users with a [userlevel] greater than the person actually
viewing the details.

On the form I have all user details, including one particular field called
[userlevel]

I have another field called [text30] which does a Dlookup and finds the
current user's [userlevel].

To change a person's [userlevel] I have an option group [Frame11], and that
works fine and dandy too. These are the three options:
User - value=1
Manager - value=2
Administrator - value=3

I click on one of the three options, and the [userlevel] is changed.

What I am trying to do is stop anyone with a [userlevel] of 2 from altering
and administrator's (3) access level. I thought I had the code worked out,
but it works in the opposite direction! When I am logged in as Manager
(userlevel]=2) I can change an administrator's rights from 3 to 2 or 1.
But when I try and move him back up to 3, I get the error message described
below in the code!

Question: where is the code going wrong please?
Here's the code:

<snip>
If (Me.[Text30].Value < Me.[userlevel].Value) Then
MsgBox "Naughty Person! You will now explode.", vbInformation
Exit Sub
Else
Me.[userlevel].Value = Frame11.Value
End If
</snip>

tia

Goat-Herder
 
You are checking your current user's level (and therefore capacity to change
another userlevel) against the userlevel of the user who's level you are
trying to change, not against the predefined values. You need to change
this:
If (Me.[Text30].Value < Me.[userlevel].Value) Then
to
If (Me.[Text30].Value >= 2) Then

HTH,

Rob
 
Hi Rob

The current user's [userlevel] needs to be checked against the [userlevel]
of the person they are trying to delete, and if the value is < then it needs
to stop there and prompt the msgsbox.

What you suggested allows a current userlevel 2 to change userlevel 3 to 2,
and then prompts the msgbox! It still changes the value.... Similarly,
changing it back from 2 to 3 also prompts the msgbox, but changes the value
anyway!

Can you suggest how to stop the value changing, and then exit the sub,
thereby leaving the original value intact?

cheers
DubboPete

Rob Parker said:
You are checking your current user's level (and therefore capacity to
change
another userlevel) against the userlevel of the user who's level you are
trying to change, not against the predefined values. You need to change
this:
If (Me.[Text30].Value < Me.[userlevel].Value) Then
to
If (Me.[Text30].Value >= 2) Then

HTH,

Rob


DubboPete said:
Hi all,

I have a form for user administration.
Only managers ([userlevel] = 2) and administrators ([userlevel] = 3) can
access this page.

By means of a combo box selection I can display any user's details on the
form, including users with a [userlevel] greater than the person actually
viewing the details.

On the form I have all user details, including one particular field
called
[userlevel]

I have another field called [text30] which does a Dlookup and finds the
current user's [userlevel].

To change a person's [userlevel] I have an option group [Frame11], and that
works fine and dandy too. These are the three options:
User - value=1
Manager - value=2
Administrator - value=3

I click on one of the three options, and the [userlevel] is changed.

What I am trying to do is stop anyone with a [userlevel] of 2 from altering
and administrator's (3) access level. I thought I had the code worked out,
but it works in the opposite direction! When I am logged in as Manager
(userlevel]=2) I can change an administrator's rights from 3 to 2 or 1.
But when I try and move him back up to 3, I get the error message described
below in the code!

Question: where is the code going wrong please?
Here's the code:

<snip>
If (Me.[Text30].Value < Me.[userlevel].Value) Then
MsgBox "Naughty Person! You will now explode.", vbInformation
Exit Sub
Else
Me.[userlevel].Value = Frame11.Value
End If
</snip>

tia

Goat-Herder
 
Hi,
First, have you stepped through the code to see what the values actually are?
Next, try converting to integers:

If (CInt(Me.Text30) < CInt(Me.userlevel)) Then
 
DubboPete said:
Hi all,

I have a form for user administration.
Only managers ([userlevel] = 2) and administrators ([userlevel] = 3) can
access this page.

By means of a combo box selection I can display any user's details on the
form, including users with a [userlevel] greater than the person actually
viewing the details.

On the form I have all user details, including one particular field called
[userlevel]

I have another field called [text30] which does a Dlookup and finds the
current user's [userlevel].

To change a person's [userlevel] I have an option group [Frame11], and that
works fine and dandy too. These are the three options:
User - value=1
Manager - value=2
Administrator - value=3

I click on one of the three options, and the [userlevel] is changed.

What I am trying to do is stop anyone with a [userlevel] of 2 from altering
and administrator's (3) access level. I thought I had the code worked out,
but it works in the opposite direction! When I am logged in as Manager
(userlevel]=2) I can change an administrator's rights from 3 to 2 or 1.
But when I try and move him back up to 3, I get the error message described
below in the code!

Question: where is the code going wrong please?
Here's the code:

<snip>
If (Me.[Text30].Value < Me.[userlevel].Value) Then
MsgBox "Naughty Person! You will now explode.", vbInformation
Exit Sub
Else
Me.[userlevel].Value = Frame11.Value
End If
</snip>

tia

Goat-Herder


You didn't post the whole sub so I'm guessing that you are using the Frame11
BeforeUpdateEvent.

I thought "What's the problem? Just swap the True/False actions" .... Then I
tried it ... and ... I got the same results as you. But then...Success!

Here's the code:

Private Sub Frame11_BeforeUpdate(Cancel As Integer)

If (CInt(Me.Text30) < Me.userlevel.OldValue) Then
MsgBox "Naughty Person! You will now explode.", vbInformation
Cancel = True
Me.Undo
Exit Sub
Else
Me.[userlevel] = Frame11
End If

End Sub

First, .Value is the default property so it is not necessary to use it. I
also used the
CInt() function to make sure the Text30 type was numeric.

I made a table and a form and started testing. Note the .OldValue in the
If() condition. What I found is when you change the user level, the change is
applied, then the code runs.

So if the person logged on has a userlevel of 2, and changes a userlevel
record from 3 to a 2, the code compares the logged on level (2) to the NEW
user level value (also 2) and allows the change.

You need to compare the logged on user level to the current record userlevel
OLDVALVE.

If the logged on userlevel is less than the .OLDVALUE userlevel then do
Cancel = true and use Undo to revert the userlevel back to the original
value, then exit the sub.

*** I'm not sure whether Me.Undo just undoes the userlevel change or the
whole record....
 
Hi Steve

It was Frame11, and I posted in your code, and it worked great. Many
thanks for your input.

DubboPete
 
Back
Top