Consulting

Results 1 to 7 of 7

Thread: Userform - is this possible?

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    11
    Location

    Userform - is this possible?

    Hi,

    I have a userform with 26 combobox's in it.

    i have each combobox with its own sub with variable fields so it can update textbox's on the userform when i select an item in the combobox.

    now, i have tidied up the userform so it has a loop within sub userform_initialize()
    with all the information from the 26 combobox subs

    now, my issue is, the combobox subs were "combobox_change" so when i made a change to the combobox it would update my textbox fields. however i cant seem to get it to update the textbox fields within the userform initialize.

    can anybody help me?

  2. #2
    VBAX Regular
    Joined
    Sep 2010
    Posts
    40
    Location
    hi jarry,

    is it like this?, when you choose a value from combobox1, it will produce an update value on a textbox which are both on the userform?

    try this
    **from youre previous thread**
    [vba]Private Sub UserForm_Initialize()

    UserForm1.Controls("combobox1").List = Array("first item", "second item")

    UserForm1.Controls("combobox1").DropDown
    End Sub


    'add this outside the userform initialize event. on the combobox_change event
    Private Sub ComboBox1_Change()
    textbox1.value = vbnullstring
    Select Case ComboBox1.Value
    Case "first item"
    textbox1.value = "first item choice"
    Case "second item"
    textbox1.value = "second item choice"
    end select
    end sub[/vba]
    hope this works

  3. #3
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Quote Originally Posted by JarryS88
    ...however i cant seem to get it to update the textbox fields within the userform initialize.
    And why are you updating the textboxes within the Initialize event? Wouldn't all the comboboxes be empty at startup, thus the textboxes empty also?

    David

  4. #4
    VBAX Regular
    Joined
    Sep 2010
    Posts
    40
    Location
    maybe he wants to have all his textboxes have values when userform initializes, like maybe textbox1 value would be "hey type on me". XD

    but what you say sir david is true, yet his comboboxes from his previous thread would contain values on userform initialization.

  5. #5
    VBAX Regular
    Joined
    Oct 2010
    Posts
    11
    Location
    Quote Originally Posted by lehgzil
    hi jarry,

    is it like this?, when you choose a value from combobox1, it will produce an update value on a textbox which are both on the userform?

    try this
    **from youre previous thread**
    [vba]Private Sub UserForm_Initialize()

    UserForm1.Controls("combobox1").List = Array("first item", "second item")

    UserForm1.Controls("combobox1").DropDown
    End Sub


    'add this outside the userform initialize event. on the combobox_change event
    Private Sub ComboBox1_Change()
    textbox1.value = vbnullstring
    Select Case ComboBox1.Value
    Case "first item"
    textbox1.value = "first item choice"
    Case "second item"
    textbox1.value = "second item choice"
    end select
    end sub[/vba]
    hope this works
    yeah pretty much exactly this.
    i just used if statements instead of cases.

    yeah ive done it this way and it works, but i was wondering if its possible to put my 26 private subs (1 for each combobox) into 1 sub with a loop (as theyre all the same, just with a rolling variable.
    (e.g. i = 1, finishing with i = i + 1, then looping).

    however it doesnt seem to work.

  6. #6
    VBAX Regular
    Joined
    Oct 2010
    Posts
    11
    Location
    Quote Originally Posted by Tinbendr
    And why are you updating the textboxes within the Initialize event? Wouldn't all the comboboxes be empty at startup, thus the textboxes empty also?

    David
    yeah i only want them updated when i change the combobox. so it makes sense to have them in combobox change subs intead of a userform initialize.

    i was just wondering if i could put all the combobox subs into 1 (not necessarily the userform initialize sub).

  7. #7
    You could have one sub to process things:

    [vba]Private Sub HandleChange(lIndex as long)
    Me.Controls("Textbox" & lId).value vbnullstring
    Select Case Me.Controls("ComboBox" & lId).Value
    Case "first item"
    Me.Controls("Textbox" & lId).value = "first item choice"
    Case "second item"
    Me.Controls("Textbox" & lId).value = "second item choice"
    End Select
    End Sub [/vba]

    Then in each combo's change event:

    [vba]Private Sub ComboBox1_Change()
    HandleChange 1
    End Sub
    Private Sub ComboBox2_Change()
    HandleChange 2
    End Sub [/vba]

    It is also possible to do all of this in a single class module with just one change event stub, but it is a bit more complex to describe.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

Posting Permissions

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