BeforeUpdate or AfterUpdate ?

W

W

Hi all,

I have an unbound form in which I have (amongst others) two comboboxes with
mandatory data.

Although I present a combobox, limited to the choices to pick out, people
manage not to fill in those data and to jump via the TAB-key to the next
field. Of course I’ll have NULL value for this combobox, but how can I force
the user to look for the correct row ?

In the BeforeUpdate or in the AfterUpdate event ?

I have some code like :

Private Sub CboAfterUpdate()
if isnull(me.Cbo) then
Beep
Msgbox(“You must choose a valueâ€)
Me.cboAfter.setfocus
endif
end sub

But this does not seem to be helpful and it does not prevent the user to
jump to the next TAB-stop.

Any help will be appreciated.

Thank you,

W
 
M

Marshall Barton

W said:
I have an unbound form in which I have (amongst others) two comboboxes with
mandatory data.

Although I present a combobox, limited to the choices to pick out, people
manage not to fill in those data and to jump via the TAB-key to the next
field. Of course I’ll have NULL value for this combobox, but how can I force
the user to look for the correct row ?

In the BeforeUpdate or in the AfterUpdate event ?

I have some code like :

Private Sub CboAfterUpdate()
if isnull(me.Cbo) then
Beep
Msgbox(“You must choose a value”)
Me.cboAfter.setfocus
endif
end sub

But this does not seem to be helpful and it does not prevent the user to
jump to the next TAB-stop.


Generally, it's better to use the form's BeforeUpdate event,
because users can click anywhere and never touch the combo
box (i.e. none of its event will be invoked).
 
J

John W. Vinson

In the BeforeUpdate or in the AfterUpdate event ?

Since the Form is unbound, its before and after update events will never fire;
and since the user might simply never setfocus to the controls, their before
and afterupdate events will never fire either.

You'll need to use whatever event (a button click perhaps?) you have
programmed to write the data to the table.

That's just one of the costs of using unbound forms: you must manually do what
Access does automatically with bound forms.

John W. Vinson [MVP]
 
A

Albert D. Kallal

You don't need to use setfocus.

Simply use the combo boxes before update event, and set the cancel flag to
true.

in fact a significant difference between the before update event, and the
after update event, is that the before update event can be canceled.

eg:

if isnull(me.Cbo) then
Beep
Msgbox("You must choose a value")
cancel = true
end if

if you do the above setting cancel=true old will prevent the cursor and the
user leaving the control, and you'll not have to set focus.
 
L

Linq Adams via AccessMonster.com

Albert said:
You don't need to use setfocus.

Simply use the combo boxes before update event, and set the cancel flag to
true.

if isnull(me.Cbo) then
Beep
Msgbox("You must choose a value")
cancel = true
end if

And how does this help if the user

Doesn't enter the Combobox at all

or

Enters the combobox but doesn't select a value, merely Yabs to the next
control?

The cbo's BeforeUpdate will only fire IF a selection is made.
 
W

W

Hi mr Vinson and all the other good man and one lady,

You all put me back into reality.
Many thanks for your answers. As a matter of fact, having studied yesterday
the Q&A's on this site, I found the answer.

Where can I find GOOD documentation about all the events one can meet in an
form ?

Eventually, I programmed the OnEnter event of my Save button to verify if
all data have been put in the places and the format I want them to be.

Thanks again,

W
 
A

Albert D. Kallal

And how does this help if the user

Doesn't enter the Combobox at all

or

Enters the combobox but doesn't select a value, merely Yabs to the next
control?

Remember, the user asked WHAT event (before, or after update) of the combo
box to use. I think is VERY clear that the before update event is the clear
winner. We were only given two choices by the user.

Of course I assume that most people here will also assume that in addition
to the above testing of the controls, there has to be some code that
eventually writes out, or uses this data (and tests that data before hand).
However we do want to give the user warning at the time when he uses the
combo box, but we WILL STILL need additional code if the control(s) NEVER
receive the focus.

Thus, I am of course assuming that the user Obviously realizes that
additional code at update time (or examine time) will be required. In fact I
don't know of any access developer that assumes that you can just use any
event on a control to verify the final data before writing out. We've never
EVER had that ability anyway. You either use data engine integrity or you
put in your own special code that test things and use your own custom
messages.

Furthermore we don't know if the users actually updating data, and with the
given information given we actually can NOT make that assumption.

The only thing we have at this point in time is which of the two events to
use, and clearly in my humble opinion of the before update event is the
correct event to use. We *could* use lost focus, but then again, why? We are
back to square one if the control NEVER receives the focus!!!

So, in addition to using the before update event, if the control never
receives the focus, and the control is never modified, then I have to assume
that most people here have concluded that additional code WILL be needed
when the data needs to be used.

So perhaps I'm assuming too much here. Perhaps the obvious conclusion that
additional checking code will be needed at update, or at the time when the
data used perhaps was too much of an obvious conclusion on my part (but in
effect we've always had to do it that way).

So, yes....if I was coding this, I would use

1) the before update event of the control
and,
2) Additionally I would also have code at update (or examine time) that
tests the data. (so, fair question on your part).

Of course the person could use the lost focus event, but as we mentioned if
the control never gets the focus, then we still need the code at
update/examine time. Furthermore the lost focus event does not have a cancel
event, and I know that experienced developers will instinctively and
naturally look to the before update event for the controls validation code.

There might be a VERY small UI advantage to using the lost focus event, I
think this advantage does not out weigh the fact that every developer is by
natural instinct to look and find the verification code for that control in
the before udpate event. That's where verification code traditionally goes.

The above approach is pretty much par for the course, and a lot of my forms
do have the testing code in the before update event of a contorl, and ALSO
that of the form's before update event (or this case an unbound form, when
the data is to be used).

Why not use the standard and longtime and long established method of testing
for controls by using the built in cancel feature? About the only downside
is there are cases when the user might tab through the control and not enter
anything. However, perhaps they WANT to do that, and perhaps they're going
to tab BACK into the other control when they get the appropriate information
for that control.

So, my approach gives the user freedom to move around anywhere in the form
in any direction, and enter data in THEIR order. At the end of the day we
STILL have to test the code at use/update time anyway.

I think it's a reasonable user interface to let users move around at will,
and not nag them unless they actually change the control value.

And I think it's a reasonable programming approach to put verification code
where 99% of developers will expect the verification code to be placed....

Your mileage and thoughts may vary and your view may be different on this
issue...this is not the "only" way to approach this...
 
W

W

Well, actually, this is the way I did it (as I explained in my answer to one
of the other experts). Problem is, and remains, I have no good documentation
about the events.
Many thanks anyhow,

W
 
J

Jeanette Cunningham

Hi,
I don't know of any good documentation about the events.
One thing to take notice of is the events that have a cancel argument, these
are very handy to know.
Another thing that I found hard to remember was the way that the before and
after update events changed or disappeared when you moved from bound forms
to unbound forms. There is a lot to learn about the events, your knowledge
grows as you keep programming. One way to see in what order events fire is
to step through code and see how and what makes it skip from event to event.
Probably one of the best ways to get yourself some documentation that is of
practical help in day to day programming is to do a search on these
newsgroups on each event that is available in forms and reports.

Jeanette Cunningham
 
J

John W. Vinson

Eventually, I programmed the OnEnter event of my Save button to verify if
all data have been put in the places and the format I want them to be.

<shrug> You already have code in the Click event, to actually save the data.
I'd just put the code there; if everything is OK go ahead and save the data,
if not issue a message and don't save. Putting this code in the Enter event
just splits it up and adds more complexity. Your choice though!

John W. Vinson [MVP]
 
P

Powderfinger

W,

I guess you know that when you select an event in the property box and press
F1, you get a help screen about that event. The examples in these help
screens have helped me the most.

FYI I put a function like the following in the form and call it in the
OnClick event of the Save/Close button

Private Sub SaveAndCloseButton_Click()
DoCmd.RunCommand acCmdSaveRecord
If DoConsistencyCheck Then
DoCmd.Close
End If
End Sub


'**** here is the function
Private Function DoConsistencyCheck() As Boolean
Dim bolSuccess As Boolean
bolSuccess = True

If IsNull(Me.School) And (IsDate(Me.StartDate) Then
bolSuccess = False
MsgBox "Please enter a school."
Me.School.SetFocus
ElseIf IsNull(Me.LName) Then
bolSuccess = False
MsgBox "Please enter a last name."
Me.LName.SetFocus
ElseIf IsNull(Me.FName) Then
bolSuccess = False
MsgBox "Please enter a first name."
Me.FName.SetFocus
ElseIf Not IsNumeric(Me.Status) Then
bolSuccess = False
MsgBox "Please enter a status."
Me.Status.SetFocus
ElseIf Not IsNumeric(Me.GenderID) And IsDate(Me.StartDate) Then
bolSuccess = False
MsgBox "Please enter a Gender."
Me.GenderID.SetFocus
Me.GenderID.Dropdown
ElseIf Me.JobPlacement And IsNull(Me.Employment) Then
bolSuccess = False
MsgBox "You must select an Employment Status for students in Job
Placement."
Me.Employment.SetFocus
Me.Employment.Dropdown
ElseIf IsDate(Me.StartDate) and Not IsNumeric(Me.TotalPrice) Then
bolSuccess = False
MsgBox "Please enter a total price."
End If
End If

DoConsistencyCheck = bolSuccess
End Function
 

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