By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

How to require user to select value from combo box?

P: 167
MS2007 - I have 2 combo boxes, 'cboDivisions' & 'cboGroups'. User selects the division from 'cboDivisions' but not all Divisions have groups associated. If there are no groups for the selected division, I set cboGroups invisible property. The problem I am having is my users are not always selecting a Group for a Division when there are some present. I want to force them to select a Group when the Divsion had them. I have put some code in the Form_BeforeUpdate event. The test for cbo_group.value works and the msbbox is displayed
Here is the code I am using:
Expand|Select|Wrap|Line Numbers
  1. If IsNull(cbo_group.value) or (cbo_group.value) = "" Then
  2.    msgbox "Group is required"
  3.    cbo_group.setfocus
  4.    Exit sub
  5. End If
  6. ]
I thought about putting the code on the exit of the cbo_groups event, however, if the user does not click in the group combo box, it won't work. Any suggestions?
Sep 12 '12 #1
Share this Question
Share on Google+
10 Replies

Seth Schrock
Expert 2.5K+
P: 2,941
You could use a before update event on the form to check if cbo_group is null and cancel if it is null. You would probably have to come up with another If/Then/Else check to see if cbo_group is supposed to have a value. You might be able to check if it is visible and if it is, then run the check to see if it is null.
Sep 12 '12 #2

Expert Mod 10K+
P: 12,366
Set the field as required in the table.
Sep 12 '12 #3

P: 167
Seth, I did put the code in the before update event on the form and I check to see if null. I do have an if/then/else test to see if the division has groups wrapped around the code I have above. How do I 'cancel'? Haven't used this before. I have always used 'exit sub'.
Sep 12 '12 #4

P: 167
The divisions and groups are in two different tables and some groups can belong to multiple divisions so setting the field as required in the table does not work in this situation.
But thanks for the suggestion!
Sep 12 '12 #5

Expert Mod 100+
P: 2,321
Before the Exit sub, you simply do:
Expand|Select|Wrap|Line Numbers
  1. Cancel=True
Sep 12 '12 #6

P: 167
I put the cancel=True in my code before the Exit Sub so my code looks like this:
Expand|Select|Wrap|Line Numbers
  1. If vGroupRqd = True Then 
  2.   If IsNull(cbo_Group.Value)  Then
  3.      MsgBox "Group is required for this Division.", , "Select Group from drop down list"
  4.     [cbo_Group].SetFocus
  5.      cancel = True
  6.      Exit Sub
  7.   End If
  8. End If
The problem is now that it closes the form it is on and goes back to the main form. I start with 'Main Menu' (form) and from there I select 'Edit Data' (form). This is the form that has all info for the asset that has division/group assignments. This is the form I put the code on, I want to stay on this data edit form and have the 'focus' set on the group combo box. Not sure why it is closing the form completly.
Sep 12 '12 #7

Seth Schrock
Expert 2.5K+
P: 2,941
What action is triggering the form's Before_Update event? Are you trying to close the form or just going on to the next record?
Sep 12 '12 #8

P: 167
On the form, I am selecting a Division from the combo box, I assume this is what is triggering the Before_update event.
Sep 12 '12 #9

Expert Mod 5K+
P: 5,397
don't assume where the before_update event is being triggered from... double check as both the form and the control can have such an event.
Sep 12 '12 #10

Seth Schrock
Expert 2.5K+
P: 2,941
Good point z. The form's before_update event shouldn't trigger until the record is left, either by going to the next record or closing the form. Otherwise, there isn't anything in that code to close the form. You should look through your other events to see if some other event is triggering it to close.
Sep 12 '12 #11

Post your reply

Sign in to post your reply or Sign up for a free account.