PDA

View Full Version : Solved: ComboBox - SetFocus ?



bdsii
02-01-2010, 07:59 AM
I have a ComboBox called Account01. I have specified in the Properties that the default Text is Select Account. The code below checks to make sure the user has selected something from the ComboBox before it allows the user to proceed to the next field.

The code works if the user has not selected something from the ComboBox but it will not remain in the ComboBox for the user to select. What happens if nothing is selected the message box pops up and then when the user clicks OK on the message box the cursor then advances to the next field instead of remaining in the Account01 field. I have used a SetFocus line in the code to return the cursor to the Account01 field.

I cannot figure out why it is working the way it is. :dunno I tried using a Change event instead of an Exit and that didn't even catch that the ComboBox had not been changed from "Select Account".

Anyone with advice for me ?


Private Sub Account01_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Account01 = "Select Account" Then
MsgBox ("Please Select Account")
Account01.SetFocus
End If
End Sub

Jan Karel Pieterse
02-01-2010, 08:10 AM
Replace
Account1.SetFocus
with
Cancel=True
(which cancels the Exit action and hence makes the user stay in the combo)

bdsii
02-01-2010, 08:49 AM
Thanks! That works.....however, now the user cannot click the Cancel button unless they select something from the ComboBox. I can live with that but is there something that can be done where the Cancel button can be clicked even the user has not selected from the ComboBox ?

Thanks so much !

lucas
02-01-2010, 08:56 AM
The wierd part is, you yourself were resetting the focus to the combobox.....

If you don't care whether they use the combo or not, why even use the if statement?

What about exit sub?

bdsii
02-01-2010, 09:08 AM
hhmmmm......well, I suppose normally I want to force a selection from the ComboBox but if the user encounters a problem with their data for some reason I would like them to easily just click on Cancel and then start again when they have the data they need to complete the form. You can still click Cancel and exit but only after selecting an item from the ComboBox. As I said, this will work, I was wondering if there is something missing causing this. :-)

lucas
02-01-2010, 09:09 AM
The logic is: You either want to force them to make a selection from the combobox or not.

bdsii
02-01-2010, 09:18 AM
Thanks Lucas......if those are the options, then it worked well ;-)

I will use it as is then. I learned a lot in this exercise, thanks Jan and Lucas !

lucas
02-01-2010, 09:27 AM
Be sure to mark your threads solved when you are satisfied. It is courtious. It keeps people from reading an entire thread just to find it has been resolved.