This should be simple......checking for null value in combo box

B

BobT

I have a combo box that is bound. User needs to pick value to begin creating
a new record. If the bypass this combo box and try to enter other vaules,
error is generated that they can not create record because of value required
by combo box. I have tired on exit, etc coding to check for null value. If
null value is found, reset the focus to the combo box. Nothing seems to
work........
 
S

Stuart McCall

BobT said:
I have a combo box that is bound. User needs to pick value to begin
creating
a new record. If the bypass this combo box and try to enter other vaules,
error is generated that they can not create record because of value
required
by combo box. I have tired on exit, etc coding to check for null value.
If
null value is found, reset the focus to the combo box. Nothing seems to
work........

Use the combo's BeforeUpdate event, like this:

Cancel = (Len(Me.ComboName & "") = 0)

That will prevent the user from moving away from the combo till it's fixed.
It will also catch zero-length strings, which you can get if the user types
something into the control, then backspaces back to the beginning.

You'll notice that the event procedure generated when you pick BeforeUpdate
has a parameter called Cancel. That's what the above code is setting.
 
B

BobT

Version 1
If Forms![MainForm]![Control1].column(0) is null then
Forms![MainForm][Control1].SetFocus
end if

Version 2
If isnull(Forms![MainForm]![Control1].column(0) then
Forms![MainForm][Control1].SetFocus
end if
 
B

BobT

The code: Cancel = (Len(Me.ComboName & "") = 0)
in the exit event works perfectly.

Once focus is on control (when I open form, focus is set on control) they
can not move away from the control until they pick a value which is what I
need for them to begin to enter a record.

Can someone explain the cancel event a little more fully.....
Thanks
Bob

--
BT


ruralguy via AccessMonster.com said:
Close Stuart, but the BeforeUpdate event only fires when the user changes the
control. The Exit event (which also can be canceled) occurs *every* time the
user tries to move the focus away from the control. It sounds like the OP
will need to use the Exit event for their requirement. They will also need
some sort of escape coding in the user wants to bail.

Stuart said:
I have a combo box that is bound. User needs to pick value to begin
creating
[quoted text clipped - 5 lines]
null value is found, reset the focus to the combo box. Nothing seems to
work........

Use the combo's BeforeUpdate event, like this:

Cancel = (Len(Me.ComboName & "") = 0)

That will prevent the user from moving away from the combo till it's fixed.
It will also catch zero-length strings, which you can get if the user types
something into the control, then backspaces back to the beginning.

You'll notice that the event procedure generated when you pick BeforeUpdate
has a parameter called Cancel. That's what the above code is setting.

--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
B

BobT

Ok, This process may work a little too well......

When my form is first opened. The existing records are available for review
and or edit. When the user clicks on a button to create a new record, the
control gets the focus with the cancel event listed previously. However, if
the user by mistakes clicks a command button to create a new record when they
only want to edit an existing record, the only way to exit the control with
the cancel event is to close the form and re-enter it. How can I modify the
event above so that if the user presses the escape key, they can exit the
control and edit a record.

I've tried using the kepressed event to run code to check for the escape
key. I then modified the cancel statement to check for the cancel key and
exit the control but that did not seem to work.

Any suggestions are welcome.

Thanks,
Bob

--
BT


ruralguy via AccessMonster.com said:
Depending on the event, Cancel will cancel any update to the control and hold
the focus in the current control. I believe the BeforeUpdate and Exit events
are the only two that can be cancelled but it seems to be all anyone needs.
As you discovered, using SetFocus does not work for the control with the
focus.
The code: Cancel = (Len(Me.ComboName & "") = 0)
in the exit event works perfectly.

Once focus is on control (when I open form, focus is set on control) they
can not move away from the control until they pick a value which is what I
need for them to begin to enter a record.

Can someone explain the cancel event a little more fully.....
Thanks
Bob
Close Stuart, but the BeforeUpdate event only fires when the user changes the
control. The Exit event (which also can be canceled) occurs *every* time the
[quoted text clipped - 18 lines]
You'll notice that the event procedure generated when you pick BeforeUpdate
has a parameter called Cancel. That's what the above code is setting.

--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
B

BobT

That is one way. But it would be a pain for this prompt to be displayed
everytime they add a record. It would be less of a strain if they hit the
excape key or such to abort the process when they need to...
--
BT


ruralguy via AccessMonster.com said:
Instead of just blindly setting Cancel, test and ask the user if they want to
bail.
Ok, This process may work a little too well......

When my form is first opened. The existing records are available for review
and or edit. When the user clicks on a button to create a new record, the
control gets the focus with the cancel event listed previously. However, if
the user by mistakes clicks a command button to create a new record when they
only want to edit an existing record, the only way to exit the control with
the cancel event is to close the form and re-enter it. How can I modify the
event above so that if the user presses the escape key, they can exit the
control and edit a record.

I've tried using the kepressed event to run code to check for the escape
key. I then modified the cancel statement to check for the cancel key and
exit the control but that did not seem to work.

Any suggestions are welcome.

Thanks,
Bob
Depending on the event, Cancel will cancel any update to the control and hold
the focus in the current control. I believe the BeforeUpdate and Exit events
[quoted text clipped - 18 lines]
You'll notice that the event procedure generated when you pick BeforeUpdate
has a parameter called Cancel. That's what the above code is setting.
 

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