Results 1 to 15 of 15

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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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 09:01 AM. Reason: Forgot to mention

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

    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  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
    211

    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

  15. #15
    New Member
    Join Date
    Sep 2016
    Posts
    2

    Re: Set selected item of combobox (access)

    cboYourName.value = cboYourName.Listindex(0) 'set combobox to top of 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
  •  



Click Here to Expand Forum to Full Width