Set selected item of combobox (access)-VBForums
Results 1 to 14 of 14

Thread: Set selected item of combobox (access)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    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 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,152

    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 (C#, VB.NET, VB 6, VBA)
    Senior Jedi 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 Multiple .NET Framework Versions 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 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    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 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,152

    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 (C#, VB.NET, VB 6, VBA)
    Senior Jedi 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 Multiple .NET Framework Versions 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 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    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 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,152

    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 (C#, VB.NET, VB 6, VBA)
    Senior Jedi 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 Multiple .NET Framework Versions 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 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

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

    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

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    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 10:01 AM. Reason: Forgot to mention

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

    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 (C#, VB.NET, VB 6, VBA)
    Senior Jedi 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 Multiple .NET Framework Versions 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 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

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

    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 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,152

    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 (C#, VB.NET, VB 6, VBA)
    Senior Jedi 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 Multiple .NET Framework Versions 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 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  12. #12
    New Member
    Join Date
    Dec 2009
    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 11:38 PM.

  13. #13
    New Member
    Join Date
    Nov 2011
    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.

  14. #14
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    162

    Re: Set selected item of combobox (access)

    Hi All,

    Using Access 2007 I just did this for a combobox to preselect a certain value (Values are from a query):

    Code:
    Me.comboIssue_Status = 1
    Remember if the query results are sorted then it may not be clear what the index of the item is. I found it easier to select the item, then press any button having set a breakpoint and check the value of the combobox which in my case was 1 (which was the 3rd item in the list as the list was sorted)

    Kristian

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.