Connecting Tech Pros Worldwide Forums | Help | Site Map

how to error check if combo box is selected

Newbie
 
Join Date: Sep 2007
Posts: 29
#1: Oct 24 '07
I am trying to check and see if the user selects an item in the cbobox and if not it will display them a message but it gives me an error saying "argument not optional"

I have tried the following snippets but neither one works
can someone help me with this? thanks in advance

Expand|Select|Wrap|Line Numbers
  1. If Me.cboBeverages="" Or  Me.cboDoughnuts = "" Then
  2. MsgBox "A selection is required", vbCritical
  3.  
  4. else
  5. 'executs my code
  6. end if
  7.  
  8.  
Expand|Select|Wrap|Line Numbers
  1. If Not Me.cboBeverages.Selected Or Not Me.cboDoughnuts.Selected Then
  2. MsgBox "A selection is required", vbCritical
  3.  
  4. else
  5. 'executs my code
  6. end if
  7.  

Jim Doherty's Avatar
Moderator
 
Join Date: Aug 2007
Location: Derbyshire,England
Posts: 639
#2: Oct 25 '07

re: how to error check if combo box is selected


Quote:

Originally Posted by cluce

I am trying to check and see if the user selects an item in the cbobox and if not it will display them a message but it gives me an error saying "argument not optional"

I have tried the following snippets but neither one works
can someone help me with this? thanks in advance

Expand|Select|Wrap|Line Numbers
  1. If Me.cboBeverages="" Or Me.cboDoughnuts = "" Then
  2. MsgBox "A selection is required", vbCritical
  3.  
  4. else
  5. 'executs my code
  6. end if
  7.  
  8.  
Expand|Select|Wrap|Line Numbers
  1. If Not Me.cboBeverages.Selected Or Not Me.cboDoughnuts.Selected Then
  2. MsgBox "A selection is required", vbCritical
  3.  
  4. else
  5. 'executs my code
  6. end if
  7.  


If you are dealing with a combobox then if nothing is selected from it the ListIndex property of the control returns a value of -1

Consider this code which deals with the controls individually and sets the focus (places the cursor) to the relevant control if each condition is true and exits at that point so the user has to address the non selection for each control before it can pass over to continue with you code after that

Expand|Select|Wrap|Line Numbers
  1. If Me!cboBeverages.ListIndex = "-1" Then
  2. MsgBox "A selection is required from the beverages list", vbCritical, "Beverage Required"
  3. Me!cboBeverages.SetFocus
  4. Exit Sub
  5. End If
  6. If Me!cboDoughnuts.ListIndex = "-1" Then
  7. MsgBox "A selection is required from the doughnuts list", vbCritical, "Beverage Required"
  8. Me!cboDoughnuts.SetFocus
  9. Exit Sub
  10. End If
...Place your other execution code after the above code blocks because if they have not selected something from both combos then the other execution code will never be reached.

As relates to your second sample the .Selected property relates to a listbox not a combobox. Also which event are you placing your code for this sequence anyway? (am considering your 'argument not optional' error warning where you might have un unrelated event popping you an error that is nothing to do with this sequence)

Regards

Jim :)
Reply