Consulting

Results 1 to 8 of 8

Thread: Solved: ComboBox - SetFocus ?

  1. #1
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location

    Solved: ComboBox - SetFocus ?

    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. 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 ?

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

  2. #2
    Replace
    Account1.SetFocus
    with
    Cancel=True
    (which cancels the Exit action and hence makes the user stay in the combo)
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    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 !

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    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. :-)

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    The logic is: You either want to force them to make a selection from the combobox or not.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    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 !

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •