PDA

View Full Version : Userform - is this possible?



JarryS88
10-14-2010, 09:26 PM
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?

lehgzil
10-14-2010, 11:12 PM
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**
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
hope this works

Tinbendr
10-15-2010, 06:45 AM
...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

lehgzil
10-15-2010, 09:09 PM
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.

JarryS88
10-17-2010, 05:48 PM
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**
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
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.

JarryS88
10-17-2010, 05:49 PM
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).

Jan Karel Pieterse
10-18-2010, 02:47 AM
You could have one sub to process things:

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

Then in each combo's change event:

Private Sub ComboBox1_Change()
HandleChange 1
End Sub
Private Sub ComboBox2_Change()
HandleChange 2
End Sub

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.