Results 1 to 13 of 13

Thread: Set selected item of combobox (access)

  1. #1
    Lively Member
    Join Date
    Jul 02
    Location
    Australia
    Posts
    108

    Set selected item of combobox (access)

    The subject says it all really.

    I want to set the selected item in a combobox from code. Using VBA in access.

    Basically, I want it to pre-select the first option on load, and I want to be able to move the selected item down one item when needed.

    Thanks

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 01
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,465

    Re: Set selected item of combobox (access)

    Use the .SelectedIndex property. 0 is the first item.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  3. #3
    Lively Member
    Join Date
    Jul 02
    Location
    Australia
    Posts
    108

    Re: Set selected item of combobox (access)

    The control doesnt seem to have that property under access.

  4. #4
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 01
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,465

    Re: Set selected item of combobox (access)

    Sorry, I was still in VB.NET mode. Use the .ListIndex property. Same - 0 for first item and -1 for no selection.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  5. #5
    Lively Member
    Join Date
    Jul 02
    Location
    Australia
    Posts
    108

    Re: Set selected item of combobox (access)

    Well, thats what I thought. But if I try it I get runtime error '7777' - You've used the litstindex property incorrectly.

    Tried setting it to -1, 0 and 1 but same error every time.

    Any ideas? I'm sure I've done this before.

  6. #6
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 01
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,465

    Re: Set selected item of combobox (access)

    Post the code procedure in concern.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 00
    Location
    Excel Hell!
    Posts
    4,895

    Re: Set selected item of combobox (access)

    Code:
    lstbox.Selected(<itemnumber>) = true

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8
    Lively Member
    Join Date
    Jul 02
    Location
    Australia
    Posts
    108

    Re: Set selected item of combobox (access)

    Well ... starting feel pretty dumb right about now.

    Basically, I added a combobox to an access form, named it newCombo and bound it to a recordset.

    Then I went into the code for Private Sub Form_Load() and am looking through the autocomplete list of methods and properties.

    So, I type newCombo. and if I look through the list, I cant find .selected, it doesn't have .selectedIndex, it does have .listIndex but I cant seem to use it.

    If I try to use them anyway, they throw errors.

    Surely there is a way to do this, but I just cant seem to find it.

    Any more help here would be great.
    Last edited by Stryker; May 9th, 2005 at 09:01 AM. Reason: Forgot to mention

  9. #9
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 01
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,465

    Re: Set selected item of combobox (access)

    Heres a working example db for you.
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  10. #10
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 00
    Location
    Excel Hell!
    Posts
    4,895

    Re: Set selected item of combobox (access)

    Apologies - yes I was looking at a listbox not a combo box (duuh).

    Listindex (as RobDog has posted) should work fine, in normal instances, however you've bound it so this may affect things. As I don't use bound controls (hate 'em) I cannot advise on this; except to say you should experiment with as many different ways/methods as you can then you can decide which is better.

    Uh... second thought - if its bound why are you trying to change the selected item... surely it updates as you change records?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  11. #11
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 01
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,465

    Re: Set selected item of combobox (access)

    Quote Originally Posted by Ecniv
    Apologies - yes I was looking at a listbox not a combo box (duuh).

    Listindex (as RobDog has posted) should work fine, in normal instances, however you've bound it so this may affect things. As I don't use bound controls (hate 'em) I cannot advise on this; except to say you should experiment with as many different ways/methods as you can then you can decide which is better.

    Uh... second thought - if its bound why are you trying to change the selected item... surely it updates as you change records?
    Good point Ecniv. If its a bound control you can navigate the rs so its displaying the record or item you desire.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  12. #12
    New Member
    Join Date
    Dec 09
    Posts
    1

    Angry Re: Set selected item of combobox (access)

    I just had the same problem in Access 2007. My problem turned out to be that I did not setfocus to the control I was changing.

    I am populating two list or combo boxes with the same info. After you have chosen one I want to programmatically choose the next one to avoid time wasted for the user. Neither worked. Most of MS documents I came across tonight said the Listindex was "Read Only" only 2 stated it was read/write.

    1. In any case my problem was I did not setfocus to the control I was changing. While others I have populated with info I did not have to setfocus too.

    2. Just in case, for those that do not know. You have to make sure the item is selected and not just scrolling to it.



    With my number of years experience I still dont know the pattern or rule for setfocus. Any documentation or rules as to when you have to and should setfocus? Why I ask is I populate a number of textboxes during this code and setfocus never came to mind being the other objects filled in.

    Thank You.
    Last edited by machrf; Dec 9th, 2009 at 10:38 PM.

  13. #13
    New Member
    Join Date
    Nov 11
    Posts
    1

    Re: Set selected item of combobox (access)

    It's kind of a pain in the butt in VBA.

    You have to set either the Value or DefaultValue of the ComboBox equal to the value of the first item in the list.

Posting Permissions

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