saving values programmically to cbo

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • allj
    New Member
    • Sep 2007
    • 23

    #1

    saving values programmically to cbo

    Hi
    I can add values to a cbo values list by cbo.additem but when I close the form and reopen it it is gone. How do I save it to the value list? Thanks
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by allj
    Hi
    I can add values to a cbo values list by cbo.additem but when I close the form and reopen it it is gone. How do I save it to the value list? Thanks
    Use the Open() Event of the Form to programmaticall y add Items to the Combo Box via the AddItem Method.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      In short you don't.
      A static list (of values) is just that. If it is a design change you need (you realised later that a required item was missing from your list), then you open the form in design mode and update your list.
      If it is a dynamic list to start with, then it is either session level dynamic (not in your case clearly) in which case what you already have is fine, or it is dynamic across sessions, in which case a static list is not the right way to handle this. In that case a table based ComboBox should be used. Doing dynamic updates using a static list (This is in Access after all) is like having a dog and barking yourself.

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        ADezii's method using AddItem works if you want to do something such as loading the available printers each time a form cranks up, but if you have a cbo with a list of vegetables, and after tasting broccoli you just have to add it to the list, it won't do! As NeoPa has so elegantly put it, a table/query based combobox is really the only sane way to go in this situation! It not only allows items to be added to the combobox which will carry over into future sessions, it allows items to be removed from the control as well!

        A while back I did write a hack for adding items permanently to a combobox, and can probably find a copy for you (although it may be tomorrow before I get around to finding it; it's on a "retired" PC and I'll have to dig it out) if you absolutely have to go this route, but it's convoluted and probably more trouble than it's worth! It involves


        1. Opening a second, dummy, form, passing it the new item using OpenArgs
        2. Closing the first form
        3. In the second form, opening the first form in Design Mode
        4. Adding the item to the cobobox
        5. Closing the first form
        6. Closing the second form
        7. Re-opening the first form, with the new item in the cbo
        As I said, it's not pretty! Let me know; I'll try to locate it! But give some thought to using a table/query based combobox!

        Welcome to TheScripts!

        Linq ;0)>

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by missinglinq
          ADezii's method using AddItem works if you want to do something such as loading the available printers each time a form cranks up, but if you have a cbo with a list of vegetables, and after tasting broccoli you just have to add it to the list, it won't do! As NeoPa has so elegantly put it, a table/query based combobox is really the only sane way to go in this situation! It not only allows items to be added to the combobox which will carry over into future sessions, it allows items to be removed from the control as well!

          A while back I did write a hack for adding items permanently to a combobox, and can probably find a copy for you (although it may be tomorrow before I get around to finding it; it's on a "retired" PC and I'll have to dig it out) if you absolutely have to go this route, but it's convoluted and probably more trouble than it's worth! It involves


          1. Opening a second, dummy, form, passing it the new item using OpenArgs
          2. Closing the first form
          3. In the second form, opening the first form in Design Mode
          4. Adding the item to the cobobox
          5. Closing the first form
          6. Closing the second form
          7. Re-opening the first form, with the new item in the cbo
          As I said, it's not pretty! Let me know; I'll try to locate it! But give some thought to using a table/query based combobox!

          Welcome to TheScripts!

          Linq ;0)>
          There may be cases where you would not want to 'expose' the RowSource of a Combo Box especially with a Table or Query. A Hybrid Dynamic/Static Value List can persist and be written directly to the System Registry as a Delimited String. A simple Algorithm can Add, Remove, Modify, Populate certain elements in the List while the entire process remains Transparent to the User. I know it's a little extreme, but I have used this concept before when it was imperative that the Source for the Combo Data be totally inaccessible.

          Comment

          Working...