Is both the the controls being populated from database ?
No. It's like this:
I have a combobox that shows two options: for example, maybe day and night. If the user selects day, a certain item is going to be displayed in the list box, but they are not from database. It's gonna be in the coding. It sounds simple...hopefu lly you can understand what I'm trying to explain. Not good at explaining, really.
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 9
Combo1.AddItem "item " & i
Next
End Sub
Private Sub Combo1_Click()
List1.AddItem (Combo1.Text)
End Sub
=============== ==============
No, but close.
Actually the items of the combobox was determined in the design of the form, so when user clicked on an item from the combobox, then something will appear on the listbox. This is what I have in mind:
=============== =============== ====
'courts is one of the item of the combobox.
If combobox.select editem = "courts" Then
lstbox = "I'm going to add something here."
=============== =============== ====
obviously my code is wrong. But is it possible to do this kind of thing?
You have to make the link from the value in the combobox to the value in the listbox.
You can do this with a array in the background.
Like: each string in this array has 2 data separeted with a char that not can be present in the used data like= # or | or ...
I have used # in the example.
You can load the data from inside the form or better: from a file with the data strings (data is better to modify afterwards).
When You select a value in the combo, it looks in the array for the LEFT part of the string and puts the RIGHT part in the listbox.
Create a form with combobox1 and listbox1
code=
=============== =============== ========
Option Explicit
'§ declare a 1 dimensional array
Dim table() As String
Private Sub Form_Load()
Dim i As Integer
'§ fill the array with string= "number 1#value = 1", "number 2#value = 2", ...
For i = 1 To 9
ReDim Preserve table(i) As String
table(i) = "number " & i & "#" & "value = " & i
'§ fill the combobox with the LEFT string to char "#"= "number 1", "number 2", ...
Combo1.AddItem Left(table(i), InStr(table(i), "#") - 1)
Next
End Sub
Private Sub Combo1_Click()
Dim i As Integer
'§ seek in the table for the LEFT string in the combo selection and
'§ put the string right of the "#" in the listbox ="value = 1" OR "value = 2" OR ...
For i = 1 To UBound(table)
If Left(table(i), InStr(table(i), "#") - 1) = Combo1.Text Then _
List1.AddItem Mid(table(i), InStr(table(i), "#") + 1)
Next
End Sub
You have to make the link from the value in the combobox to the value in the listbox.
You can do this with a array in the background.
Like: each string in this array has 2 data separeted with a char that not can be present in the used data like= # or | or ...
I have used # in the example.
You can load the data from inside the form or better: from a file with the data strings (data is better to modify afterwards).
When You select a value in the combo, it looks in the array for the LEFT part of the string and puts the RIGHT part in the listbox.
Create a form with combobox1 and listbox1
code=
=============== =============== ========
Option Explicit
'§ declare a 1 dimensional array
Dim table() As String
Private Sub Form_Load()
Dim i As Integer
'§ fill the array with string= "number 1#value = 1", "number 2#value = 2", ...
For i = 1 To 9
ReDim Preserve table(i) As String
table(i) = "number " & i & "#" & "value = " & i
'§ fill the combobox with the LEFT string to char "#"= "number 1", "number 2", ...
Combo1.AddItem Left(table(i), InStr(table(i), "#") - 1)
Next
End Sub
Private Sub Combo1_Click()
Dim i As Integer
'§ seek in the table for the LEFT string in the combo selection and
'§ put the string right of the "#" in the listbox ="value = 1" OR "value = 2" OR ...
For i = 1 To UBound(table)
If Left(table(i), InStr(table(i), "#") - 1) = Combo1.Text Then _
List1.AddItem Mid(table(i), InStr(table(i), "#") + 1)
Next
End Sub
=============== =============== ========
Thanks for this! I think I will try and do this later, hopefully it will work. (I have so many assignments to finish!)
Comment