Before update not working?

  • Thread starter Thread starter hermie
  • Start date Start date
H

hermie

Hello
I put in the before update field of my combobox this event:
Private Sub cboAL_BeforeUpdate(Cancel As Integer)
If IsNull(Me.cboAL) Then
MsgBox "Please select a choice"
Cancel = True
Me.cboAL.SetFocus
End If
End Sub

I found this solution in this newsgroup but mine is not working, i get no
message box!
What goes wrong here?
Herman
 
I always have issues with the before_update field on a combobox. I don't
know the pattern, but I find that under some circumstances the event is not
invoked when text is directly typed into the combobox -- and especially when
the combobox is simply cleared.

I always do my testing of the combobox in the form beforeupdate event. I
check value of the combobox and (if limit to list is set on) I also check
the listindex. In addition, if limit to list is set on, I also put a test
in the form's error event to look for errors 3314 (blank/null in the
combobox) and error 2237 (item in combobox not in list).
 
Hello
I put in the before update field of my combobox this event:
Private Sub cboAL_BeforeUpdate(Cancel As Integer)
If IsNull(Me.cboAL) Then
MsgBox "Please select a choice"
Cancel = True
Me.cboAL.SetFocus
End If
End Sub

I found this solution in this newsgroup but mine is not working, i get no
message box!
What goes wrong here?
Herman


This works for me.
Note: You do not need to explicitly set focus to the same control
after the Cancel = True. Focus is returned automatically.

If IsNull(Me.cboAL) Then
MsgBox "Please select a choice"
Cancel = True
End If

If you are not getting a message, perhaps it's because the control is
not really Null. Set a breakpoint on the IsNull line and step through
the code, line by line.
 
Fred
Thabks for your advice but it still not works?
The code is now:

Private Sub cboAL_BeforeUpdate(Cancel As Integer)
If IsNull(Me.cboAL) Then
MsgBox "Please select a choice"
Cancel = True
End If
End Sub

In the intermediate window I did:
?forms.frmhg001.cboal
Null

So the value is null =

But not get a message box

Other solution available?
herman
 
The BeforeUpdate event does not occur unless the user "dirties" the combo
box....meaning that the user either types into the combo box display,
selects something from the dropdown list, or deletes characters (one or
more) from the combo box's display. Are you wanting it to run even if the
user doesn't enter anything in the combo box? If yes, you then will need to
use the form's BeforeUpdate event, and in that case you would use the
SetFocus step that you posted initially as part of your code.
--

Ken Snell
<MS ACCESS MVP>



hermie said:
Fred
Thabks for your advice but it still not works?
The code is now:

Private Sub cboAL_BeforeUpdate(Cancel As Integer)
If IsNull(Me.cboAL) Then
MsgBox "Please select a choice"
Cancel = True
End If
End Sub

In the intermediate window I did:
?forms.frmhg001.cboal
Null

So the value is null =

But not get a message box

Other solution available?
herman
 
Ken

I tried now this:
Private Sub cboAL_BeforeUpdate(Cancel As Integer)
If IsNull(Me.cboAL) Then
MsgBox "Please select a choice"
Cancel = True
Else
Me.cboAL.SetFocus
End If
End Sub

This not works too?

What I want is that if the user not select an item from the combobox and
tabs to the next control a message should appaer which says something like
"You not selected an item, please select one"

I tried also:
Private Sub cboAL_LostFocus()
If IsNull(Me.cboAL) Then
MsgBox "please select an item"
Else
Me.cboAL.SetFocus
End If
End Sub

With this event I get a messagebox but no focus on cboAL

Herman
Ken Snell said:
The BeforeUpdate event does not occur unless the user "dirties" the combo
box....meaning that the user either types into the combo box display,
selects something from the dropdown list, or deletes characters (one or
more) from the combo box's display. Are you wanting it to run even if the
user doesn't enter anything in the combo box? If yes, you then will need to
use the form's BeforeUpdate event, and in that case you would use the
SetFocus step that you posted initially as part of your code.
 
If you want to prevent the user from leaving the combo box without selecting
an item, use the Exit event of the combo box:

Private Sub cboAL_Exit(Cancel As Integer)
If IsNull(Me.cboAL) Then
MsgBox "Please select a choice"
Cancel = True
End If
End Sub


--

Ken Snell
<MS ACCESS MVP>
 
I tried this out Ken - but if you want to close the form without saving prior
to completing all fields that have this code on the exit event - how should
it be handled?

Thanks,

TDR
 
Ahhhhhhh..... isn't just intriguing how one idea can interfere with another?

You are correct that this code will not let the user exit the control unless
the value is entered. That precludes the ability to click on a "close" or
"cancel" command button, as it appears that you've discovered.

Therefore, the use of the Exit event is one that I do very rarely, for just
that reason. (Frustrated users will resort to Crtl+Alt+Delete, which is
almost guaranteed to mess up your database worse than wrong data!)

Therefore, if you know that there is a valid data range for a control to get
upon entry, one uses the BeforeUpdate event of the control.

If you wish to validate that all bound controls have values when you require
such values, use the form's BeforeUpdate event to validate that each control
has a desired value.

If one wants to validate only upon "tabbing" out of a control, but not
validate when clicking on another control (including a command button), it's
possible to put very tiny, almost invisible textboxes on the form, with a
textbox in the "tab order" after each "big" control. In those small
textboxes' GotFocus event, test if the "preceding" control has a value, and
if not, display a message and set the focus back on that control. If the
preceding control has a value, then set focus on the next control in your
sequence. This is not easy to maintain if you find that you are redesigning
your form, as you not only set a tab order for the form but also must
duplicate the tab order in the code that runs in the GotFocus events for
those "tiny" textboxes.


--

Ken Snell
<MS ACCESS MVP>
 
I suppose it will be more intriguing and less frustrating as I develop my
abilities at this game.

I thought it would be fairly straight forward to design a form - but to make
a decent form to append records full of 'good' data, it turns out that the
more I learn, the more I need to learn.

Thanks for the reply - gives me more to think about.

TDR
 
TDR said:
I suppose it will be more intriguing and less frustrating as I develop my
abilities at this game.

< smile > Each of us must travel this road.... no one can travel it for
you... but it becomes more fun the farther you go!
:-)
 
Back
Top