By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,077 Members | 2,160 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,077 IT Pros & Developers. It's quick & easy.

ComboBox use ListBox as data source

P: n/a
Art
Hi,

I've got a number of ListBoxes that hold data from an Access database. On
another tab of my form I need the user to use a ComboBox to choose one of the
items from one of the ListBoxes. I have attached the ComboBox to my Access
database and this works fine. However, it seems to me that I should be able
to attach it to the ListBox that has that same information instead. Can I do
this?

Art
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
I haven't tested this but you should be able to do this...

Dim L as new ListBox
Dim C as new ComboBox
''Do Databindings on ListBox
C.DataSource = L.DataSource

Hope this helps
Chris
"Art" <Ar*@discussions.microsoft.com> wrote in message
news:8F**********************************@microsof t.com...
Hi,

I've got a number of ListBoxes that hold data from an Access database. On
another tab of my form I need the user to use a ComboBox to choose one of
the
items from one of the ListBoxes. I have attached the ComboBox to my
Access
database and this works fine. However, it seems to me that I should be
able
to attach it to the ListBox that has that same information instead. Can I
do
this?

Art

Nov 21 '05 #2

P: n/a
Art,

Yes by creating more dataviews (with sort and/or rowfilter) and use that as
the datasources.

I hope this helps?

Cor
Nov 21 '05 #3

P: n/a
Art
Cor,

Thanks -- but I don't know enough yet to really understand. Also in
reference to the response from Chris, I don't have my ListBoxes connected to
a datasource. I fill them up by reading from a database (perhaps this isn't
good).

Unfortunately I don't know about "dataviews" -- could you refer me to
something or give me a hint.

I apologize for starting out with so little knowledge and little experience
in this area.

thanks,

Art

"Cor Ligthert" wrote:
Art,

Yes by creating more dataviews (with sort and/or rowfilter) and use that as
the datasources.

I hope this helps?

Cor

Nov 21 '05 #4

P: n/a
Can you post your code that you are using to fill the listbox? It will help
us give you better input.

If you are binding the Listbox to at database then you have a datasource.
Step through your code and look at the value of the datasource. Unless you
are manually using the Listbox.items.add method. Then you aren't using
databindings at all.

Chris

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
Cor,

Thanks -- but I don't know enough yet to really understand. Also in
reference to the response from Chris, I don't have my ListBoxes connected
to
a datasource. I fill them up by reading from a database (perhaps this
isn't
good).

Unfortunately I don't know about "dataviews" -- could you refer me to
something or give me a hint.

I apologize for starting out with so little knowledge and little
experience
in this area.

thanks,

Art

"Cor Ligthert" wrote:
Art,

Yes by creating more dataviews (with sort and/or rowfilter) and use that
as
the datasources.

I hope this helps?

Cor

Nov 21 '05 #5

P: n/a
Art
Chris,

I'm not using data bindings at all. The code just shows me reading data
from a database and using the "add" method (as you mentioned). I haven't
used data binding yet, although I'm sure it would make my life easier if I
learned how.

Thanks,

Art

"Chris, Master of All Things Insignifican" wrote:
Can you post your code that you are using to fill the listbox? It will help
us give you better input.

If you are binding the Listbox to at database then you have a datasource.
Step through your code and look at the value of the datasource. Unless you
are manually using the Listbox.items.add method. Then you aren't using
databindings at all.

Chris

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
Cor,

Thanks -- but I don't know enough yet to really understand. Also in
reference to the response from Chris, I don't have my ListBoxes connected
to
a datasource. I fill them up by reading from a database (perhaps this
isn't
good).

Unfortunately I don't know about "dataviews" -- could you refer me to
something or give me a hint.

I apologize for starting out with so little knowledge and little
experience
in this area.

thanks,

Art

"Cor Ligthert" wrote:
Art,

Yes by creating more dataviews (with sort and/or rowfilter) and use that
as
the datasources.

I hope this helps?

Cor


Nov 21 '05 #6

P: n/a
Art,

This is a little bit crazy sample I made now from a sample I made yesterday.
However I think that everything is in it that you need.
I used a Northwind sample access database, mostly there are a lot on your
computer when you have downloaded Microsoft samples.

You have to make a new project, add a combobox and a listbox.
Paste this in the code, change the dates in row 3 and 4 to your local
formats and run

\\\
Private Sub Form4_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim startdate As String = "20-07-1996" 'this in your local standard format
Dim enddate As String = "31 juli 1996" 'this in your local standard format
'Used are Dutch settings
Dim conn As New OleDb.OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test1\northwind.mdb")
Dim da As New OleDb.OleDbDataAdapter _
("SELECT OrderId, OrderDate FROM Orders WHERE (OrderDate > ?) AND (OrderDate
< ?)", conn)
da.SelectCommand.Parameters.Add _
(New OleDb.OleDbParameter("", OleDb.OleDbType.Date))
da.SelectCommand.Parameters.Add _
(New OleDb.OleDbParameter("", OleDb.OleDbType.Date))
da.SelectCommand.Parameters(0).Value = CDate(startdate)
da.SelectCommand.Parameters(1).Value = CDate(enddate)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "OrderDate"
Dim dv As New DataView(ds.Tables(0))
dv.RowFilter = "OrderId > 10260"
ListBox1.DataSource = dv
ListBox1.DisplayMember = "OrderId"
End Sub
///

I hope this helps?

Cor
Nov 21 '05 #7

P: n/a
Art
Cor,

Thank you very much -- it will take me a little time to go through your
example. I very much appreciate the time it must have taken for you to put
this together, I'm sure I will learn a lot.

Art

"Cor Ligthert" wrote:
Art,

This is a little bit crazy sample I made now from a sample I made yesterday.
However I think that everything is in it that you need.
I used a Northwind sample access database, mostly there are a lot on your
computer when you have downloaded Microsoft samples.

You have to make a new project, add a combobox and a listbox.
Paste this in the code, change the dates in row 3 and 4 to your local
formats and run

\\\
Private Sub Form4_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim startdate As String = "20-07-1996" 'this in your local standard format
Dim enddate As String = "31 juli 1996" 'this in your local standard format
'Used are Dutch settings
Dim conn As New OleDb.OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test1\northwind.mdb")
Dim da As New OleDb.OleDbDataAdapter _
("SELECT OrderId, OrderDate FROM Orders WHERE (OrderDate > ?) AND (OrderDate
< ?)", conn)
da.SelectCommand.Parameters.Add _
(New OleDb.OleDbParameter("", OleDb.OleDbType.Date))
da.SelectCommand.Parameters.Add _
(New OleDb.OleDbParameter("", OleDb.OleDbType.Date))
da.SelectCommand.Parameters(0).Value = CDate(startdate)
da.SelectCommand.Parameters(1).Value = CDate(enddate)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "OrderDate"
Dim dv As New DataView(ds.Tables(0))
dv.RowFilter = "OrderId > 10260"
ListBox1.DataSource = dv
ListBox1.DisplayMember = "OrderId"
End Sub
///

I hope this helps?

Cor

Nov 21 '05 #8

P: n/a
Gotcha. Well now that I understand that, her is the simpliest solution to
your orginal question.

L is your listbox and C is your combobox.

Dim I as Object
For Each I In L.Items
C.Items.AddRange(I)
Next

Cor has shown you in his post how to do databinding. Mine will get you what
you want I think quickly.
Chris
"Art" <Ar*@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
Chris,

I'm not using data bindings at all. The code just shows me reading data
from a database and using the "add" method (as you mentioned). I haven't
used data binding yet, although I'm sure it would make my life easier if I
learned how.

Thanks,

Art

"Chris, Master of All Things Insignifican" wrote:
Can you post your code that you are using to fill the listbox? It will
help
us give you better input.

If you are binding the Listbox to at database then you have a datasource.
Step through your code and look at the value of the datasource. Unless
you
are manually using the Listbox.items.add method. Then you aren't using
databindings at all.

Chris

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
> Cor,
>
> Thanks -- but I don't know enough yet to really understand. Also in
> reference to the response from Chris, I don't have my ListBoxes
> connected
> to
> a datasource. I fill them up by reading from a database (perhaps this
> isn't
> good).
>
> Unfortunately I don't know about "dataviews" -- could you refer me to
> something or give me a hint.
>
> I apologize for starting out with so little knowledge and little
> experience
> in this area.
>
> thanks,
>
> Art
>
> "Cor Ligthert" wrote:
>
>> Art,
>>
>> Yes by creating more dataviews (with sort and/or rowfilter) and use
>> that
>> as
>> the datasources.
>>
>> I hope this helps?
>>
>> Cor
>>
>>
>>


Nov 21 '05 #9

P: n/a
Note: I haven't tested this, but it should work

Dim I as Object
For Each I In L.Items
C.Items.Add(I)
Next
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:%2*****************@TK2MSFTNGP12.phx.gbl...
Gotcha. Well now that I understand that, her is the simpliest solution to
your orginal question.

L is your listbox and C is your combobox.

Dim I as Object
For Each I In L.Items
C.Items.AddRange(I)
Next

Cor has shown you in his post how to do databinding. Mine will get you
what you want I think quickly.
Chris
"Art" <Ar*@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
Chris,

I'm not using data bindings at all. The code just shows me reading data
from a database and using the "add" method (as you mentioned). I haven't
used data binding yet, although I'm sure it would make my life easier if
I
learned how.

Thanks,

Art

"Chris, Master of All Things Insignifican" wrote:
Can you post your code that you are using to fill the listbox? It will
help
us give you better input.

If you are binding the Listbox to at database then you have a
datasource.
Step through your code and look at the value of the datasource. Unless
you
are manually using the Listbox.items.add method. Then you aren't using
databindings at all.

Chris

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
> Cor,
>
> Thanks -- but I don't know enough yet to really understand. Also in
> reference to the response from Chris, I don't have my ListBoxes
> connected
> to
> a datasource. I fill them up by reading from a database (perhaps this
> isn't
> good).
>
> Unfortunately I don't know about "dataviews" -- could you refer me to
> something or give me a hint.
>
> I apologize for starting out with so little knowledge and little
> experience
> in this area.
>
> thanks,
>
> Art
>
> "Cor Ligthert" wrote:
>
>> Art,
>>
>> Yes by creating more dataviews (with sort and/or rowfilter) and use
>> that
>> as
>> the datasources.
>>
>> I hope this helps?
>>
>> Cor
>>
>>
>>


Nov 21 '05 #10

P: n/a
Art
Chris,

Thanks -- as with cor's answer this will take me a little time to try out.
I really appreciate your help.

Art

"Chris, Master of All Things Insignifican" wrote:
Note: I haven't tested this, but it should work

Dim I as Object
For Each I In L.Items
C.Items.Add(I)
Next
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:%2*****************@TK2MSFTNGP12.phx.gbl...
Gotcha. Well now that I understand that, her is the simpliest solution to
your orginal question.

L is your listbox and C is your combobox.

Dim I as Object
For Each I In L.Items
C.Items.AddRange(I)
Next

Cor has shown you in his post how to do databinding. Mine will get you
what you want I think quickly.
Chris
"Art" <Ar*@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
Chris,

I'm not using data bindings at all. The code just shows me reading data
from a database and using the "add" method (as you mentioned). I haven't
used data binding yet, although I'm sure it would make my life easier if
I
learned how.

Thanks,

Art

"Chris, Master of All Things Insignifican" wrote:

Can you post your code that you are using to fill the listbox? It will
help
us give you better input.

If you are binding the Listbox to at database then you have a
datasource.
Step through your code and look at the value of the datasource. Unless
you
are manually using the Listbox.items.add method. Then you aren't using
databindings at all.

Chris

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
> Cor,
>
> Thanks -- but I don't know enough yet to really understand. Also in
> reference to the response from Chris, I don't have my ListBoxes
> connected
> to
> a datasource. I fill them up by reading from a database (perhaps this
> isn't
> good).
>
> Unfortunately I don't know about "dataviews" -- could you refer me to
> something or give me a hint.
>
> I apologize for starting out with so little knowledge and little
> experience
> in this area.
>
> thanks,
>
> Art
>
> "Cor Ligthert" wrote:
>
>> Art,
>>
>> Yes by creating more dataviews (with sort and/or rowfilter) and use
>> that
>> as
>> the datasources.
>>
>> I hope this helps?
>>
>> Cor
>>
>>
>>



Nov 21 '05 #11

P: n/a

"Art" <Ar*@discussions.microsoft.com> wrote
Hi,

I've got a number of ListBoxes that hold data from an Access database. On
another tab of my form I need the user to use a ComboBox to choose one of the
items from one of the ListBoxes. I have attached the ComboBox to my Access
database and this works fine. However, it seems to me that I should be able
to attach it to the ListBox that has that same information instead. Can I do
this?


You no doubt got a lot of good information from other replies, but the
direct route to what you wanted to do was possible with one line of code:

Combobox1.DataSource = Listbox1.Items

LFS
Nov 21 '05 #12

P: n/a
Chris,

In my opinion can you even do it directly with the collection with AddRange.
They both have the same base class and the collection is implementing Ilist.
Than you get the same solution as with your first sample. However I show no
sample because it would only confuse Art in my opinion.

The problem with this solution is, that there is not a selection, while the
dataview solution especially using the value from the first is very good to
use. (As well not showed to make it not to complicated).

Cor
Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.