what's wrong with before update event procedure

A

alekm

Hi,
here is my code event procedure for a check box. I want to allow some users
to change its state and to prevent otheres from change it. Is there any way I
can ban that SendKeys "{ESC}" line? That line is the only way it works not
ending in the loop (you press anything on the form, msgbox pops up again),
but I don't see why??


Private Sub MyCheckBox_BeforeUpdate(Cancel As Integer)

If currentuser <> "RightUser" Then

MsgBox ("You are not right user. You don't touch this check box.")
Cancel = True
SendKeys "{ESC}"
' without last line msgbox pops up again and again when you press
' anything on the form
' only escape key stops it. Why?!

End If

End Sub

Any solutions?
Thanx

alekmil
 
B

BruceM via AccessMonster.com

Rather than scolding the user for checking a box they may have checked in
good faith, either disable or hide the box. You don't say how you are
identifying the "RightUser", but if you are using User Level Security (ULS)
you can use the form's Open event:

Me.CheckBoxName.Enabled = (CurrentUser = "RightUser")

or

Me.CheckBoxName.Visible = (CurrentUser = "RightUser")

If you are not using ULS, CurrentUser is Admin, and nobody will see an
enabled check box.

If you are using ULS, you may want to use groups rather than individual users.
For instance, everybody who can check the box is in the Managers group.
Place something like the following in a standard module:

Public Function UserGroup(ByVal GroupName As String) As Boolean

On Error GoTo UserGroup_Error

Dim usr As DAO.User
Dim grp As DAO.Group

Set usr = DBEngine.Workspaces(0).Users(CurrentUser)
For Each grp In usr.Groups
If grp.Name = GroupName Then
UserGroup = True
Exit For
End If
Next grp

ProcExit:
Exit Function

UserGroup_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & _
"in UserGroup, Form_frmCAR"
Resume ProcExit

End Function

I would give credit for the code if I could remember where it came from, but
I don't seem to have noted that.

Anyhow, in the form's Open event you could have:

Me.CheckBoxName.Enabled = UserGroup("Managers")

Note that ULS is for Access 2003 and earlier, or later versions that use the
2003 format.
 
K

Krzysztof Naworyta

Juzer alekm <[email protected]> napisa³

| here is my code event procedure for a check box. I want to allow some
| users to change its state and to prevent otheres from change it. Is
| there any way I can ban that SendKeys "{ESC}" line? That line is the
| only way it works not ending in the loop (you press anything on the
| form, msgbox pops up again), but I don't see why??
|
|
| Private Sub MyCheckBox_BeforeUpdate(Cancel As Integer)
|
| If currentuser <> "RightUser" Then
|
| MsgBox ("You are not right user. You don't touch this check
| box.") Cancel = True
| SendKeys "{ESC}"
| ' without last line msgbox pops up again and again when you
| press ' anything on the form
| ' only escape key stops it. Why?!
|
| End If
|
| End Sub

In fact a checkbox (as an option button and a toggle butoon) has not
BeforeUpdate event.
This event triggers in the same moment as AfterUpdate (and OnClick) and
can not be canceled.

You can use AfterUpdate event intead, changing wrong value:

Private Sub MyCheckBox_AfterUpdate()

If currentuser <> "RightUser" Then
MsgBox "Bla bla..."
With MyCheckBox
if .Value = True then
.Value = False
end if
'or:
'.Value = Not .Value
End If

End Sub




--
KN

archiwum grupy:
http://groups.google.pl/advanced_search
(grupa: pl*msaccess)
 
B

BruceM via AccessMonster.com

A check box as an option group control does not have Before Update, but a
single check box bound to a table field does.

If you use the After Update event as you have shown the value will be updated,
and the After Update code will run again, which means the value will be
updated, and the After Update code will run again.... The user will be able
to do nothing but click the message box button over and over.

Krzysztof said:
Juzer alekm <[email protected]> napisaÅ‚

| here is my code event procedure for a check box. I want to allow some
| users to change its state and to prevent otheres from change it. Is
[quoted text clipped - 16 lines]
|
| End Sub

In fact a checkbox (as an option button and a toggle butoon) has not
BeforeUpdate event.
This event triggers in the same moment as AfterUpdate (and OnClick) and
can not be canceled.

You can use AfterUpdate event intead, changing wrong value:

Private Sub MyCheckBox_AfterUpdate()

If currentuser <> "RightUser" Then
MsgBox "Bla bla..."
With MyCheckBox
if .Value = True then
.Value = False
end if
'or:
'.Value = Not .Value
End If

End Sub
 
D

Dirk Goldgar

alekm said:
Hi,
here is my code event procedure for a check box. I want to allow some
users
to change its state and to prevent otheres from change it. Is there any
way I
can ban that SendKeys "{ESC}" line? That line is the only way it works
not
ending in the loop (you press anything on the form, msgbox pops up again),
but I don't see why??


Private Sub MyCheckBox_BeforeUpdate(Cancel As Integer)

If currentuser <> "RightUser" Then

MsgBox ("You are not right user. You don't touch this check box.")
Cancel = True
SendKeys "{ESC}"
' without last line msgbox pops up again and again when you press
' anything on the form
' only escape key stops it. Why?!

End If

End Sub


You don't need SendKeys, you can use

Me.MyCheckBox.Undo

instead.

You need to undo the change to the checkbox because, although setting
Cancel=True disallows the update, the user's change to the checkbox value is
still pending, and Access will keep attempting to reapply it until you undo
it, whether by the .Undo method or by pressing/sending the Escape key.
 
J

John W. Vinson

Hi,
here is my code event procedure for a check box. I want to allow some users
to change its state and to prevent otheres from change it. Is there any way I
can ban that SendKeys "{ESC}" line? That line is the only way it works not
ending in the loop (you press anything on the form, msgbox pops up again),
but I don't see why??


Private Sub MyCheckBox_BeforeUpdate(Cancel As Integer)

If currentuser <> "RightUser" Then

MsgBox ("You are not right user. You don't touch this check box.")
Cancel = True
SendKeys "{ESC}"
' without last line msgbox pops up again and again when you press
' anything on the form
' only escape key stops it. Why?!

End If

If you don't want a user to check a checkbox, don't LET them check it in the
first place!

Try putting code in the Form's Current or Load event:

Me!MyCheckbox.Enabled = (CurrentUser = "RightUser")
 
K

Krzysztof Naworyta

Juzer BruceM via AccessMonster.com <u54429@uwe> napisał

| A check box as an option group control does not have Before Update, but
| a single check box bound to a table field does.

You are wrong :)
Single checkbox can have BeforeUpdate subrutine but it does not work!
You can not take OldValue for example. You can not cancel this event.


| If you use the After Update event as you have shown the value will be
| updated, and the After Update code will run again, which means the
| value will be updated, and the After Update code will run again....
| The user will be able to do nothing but click the message box button
| over and over.

You are wrong!
Updating any control from code NEVER triggers AfterUpdate event.
 
K

Krzysztof Naworyta

Juzer Krzysztof Naworyta <[email protected]> napisaÅ‚

|| A check box as an option group control does not have Before Update, but
|| a single check box bound to a table field does.
|
| You are wrong :)
| Single checkbox can have BeforeUpdate subrutine but it does not work!
| You can not take OldValue for example. You can not cancel this event.

Ups! I was wrong!!! Sorry!
 
D

Dirk Goldgar

Krzysztof Naworyta said:
Juzer BruceM via AccessMonster.com <u54429@uwe> napisał

| A check box as an option group control does not have Before Update, but
| a single check box bound to a table field does.

You are wrong :)
Single checkbox can have BeforeUpdate subrutine but it does not work!
You can not take OldValue for example. You can not cancel this event.


You are wrong. <g>

For a standalone checkbox, the BeforeUpdate procedure does work. If the
checkbox is bound, its .OldValue property works just fine, and you *can*
cancel the event. However, you must also Undo the checkbox if you want it
to revert to its original value.
 
R

Rick Brandt

John said:
If you don't want a user to check a checkbox, don't LET them check it in
the first place!

Try putting code in the Form's Current or Load event:

Me!MyCheckbox.Enabled = (CurrentUser = "RightUser")

I'm assuming that this would be the proper method according to standard UI
practice, but in my experience disabling a control is likely to generate a
support call of "why is this control disabled?" whereas using BeforeUpdate
to display a message explaining why they are not allowed to use that control
does not.
 
J

John W. Vinson

I'm assuming that this would be the proper method according to standard UI
practice, but in my experience disabling a control is likely to generate a
support call of "why is this control disabled?" whereas using BeforeUpdate
to display a message explaining why they are not allowed to use that control
does not.

I should have added that setting its Visible property to no might be even more
effective: the user won't complain about being unable to click a checkbox if
they can't even see it!

Good point though. If it's important that the box be visible but protected,
the BeforeUpdate event would indeed be less user hostile.
 
B

BruceM via AccessMonster.com

I know you acknowledged you were incorrect about the Before Update event for
a single check box, but I want to point out that my tests with Access 2003
show that updating the check box from code in its own After Update event as
you showed does indeed trigger its After Update event again. However, if you
update CheckBoxA from After Update code in CheckBoxB, the After Update code
of CheckBoxA will not run. It is only when you update CheckBoxA with its own
After Update event that the code runs again and again.
 
K

Krzysztof Naworyta

BruceM via AccessMonster.com wrote:


(...)
| I want to point out that my tests
| with Access 2003 show that updating the check box from code in its
| own After Update event as you showed does indeed trigger its After
| Update event again.

I can not confirm it!
 

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