473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to populate a listbox using a combo box?

I have a combo box (cboStore) and a listbox (Qlist) both are unbound to a form call frmFAQ. What I want is to be a able to select a Subject from the combo box and have the results show up in the listbox.

In the rowsource of the combox I have this code:
Expand|Select|Wrap|Line Numbers
  1.  SELECT Subjecttbl.SubID, Subjecttbl.Subject FROM Subjecttbl; 
in the rowsource of the listbox I have:
Expand|Select|Wrap|Line Numbers
  1.  SELECT Subjecttbl.Subject, Listtbl.List FROM Subjecttbl INNER JOIN Listtbl ON Subjecttbl.SubID=Listtbl.SubID GROUP BY Subjecttbl.Subject, Listtbl.List;  
Everything shows up in the combo box but nothing in the listbox. Can someone help me with this.
Feb 6 '07 #1
14 12288
Rabbit
12,516 Expert Mod 8TB
The way it is set up right now, your list box should show every record in Listtbl that has a matching SubID in Subjecttbl.

Is the column count and column widths set up correction?

If you want it to populate the list box depending on the choice in the combo box, then you have to add code to the After Update event of the combo box.

Expand|Select|Wrap|Line Numbers
  1. Me!NameOfListBox.RowSource = "Select Listtbl.List From Subjecttbl Where SubID = " & Me!NameOfComboBox
This means the combo box has to be bound on SubID.
Feb 6 '07 #2
Phille
22
Theres also an example access file on microsofts download page that does exactly this


http://www.microsoft.com/downloads/d...DisplayLang=en

Have a look
Feb 6 '07 #3
NeoPa
32,556 Expert Mod 16PB
See (Example Filtering on a Form.) for a fuller explanation.
In your SQL though, you are trying to reference items on a form. The SQL though, is executed outside of the form (in the SQL engine) so local references to the controls on the form will not be interpreted correctly (or at all even).
Feb 7 '07 #4
The way it is set up right now, your list box should show every record in Listtbl that has a matching SubID in Subjecttbl.

Is the column count and column widths set up correction?

If you want it to populate the list box depending on the choice in the combo box, then you have to add code to the After Update event of the combo box.

Expand|Select|Wrap|Line Numbers
  1. Me!NameOfListBox.RowSource = "Select Listtbl.List From Subjecttbl Where SubID = " & Me!NameOfComboBox
This means the combo box has to be bound on SubID.
I have add the code to the program like this in the Afterupdate event of combobox:
Expand|Select|Wrap|Line Numbers
  1. Me!Qlist.RowSource = "Select Listtbl.List From Subjecttbl Where SubID = " & Me!Subject 
When I go to run the program a subject is already selected in the combo box? When I select a different subject in the combo box a message box pops up and prompt me to enter a parameter. It also have Listtbl.List right before you enter the parameter. So then I enter say 1 and nothing happens in the listbox. What happen here?

Does the combo box has to be bound to the form or table? I set the combox box bound to the SubID in the form through the control source. Is this correct?

Also do I need to bound the listbox to the form like I did the combo box. What goes in the rowsource of the listbox now?
Feb 7 '07 #5
NeoPa
32,556 Expert Mod 16PB
Your code :
Expand|Select|Wrap|Line Numbers
  1. Me!Qlist.RowSource = "Select Listtbl.List From Subjecttbl Where SubID = " & Me!Subject 
is trying to get Listtbl.List From Subjecttbl.
Feb 7 '07 #6
Your code :
Expand|Select|Wrap|Line Numbers
  1. Me!Qlist.RowSource = "Select Listtbl.List From Subjecttbl Where SubID = " & Me!Subject 
is trying to get Listtbl.List From Subjecttbl.
The subject shows up in the combo box but when I click on the the subject nothing shows up in the list box

Expand|Select|Wrap|Line Numbers
  1. Me!Qlist.RowSource = "Select Subjecttbl.Subject From Subjecttbl Where SubID = " & Me!Subject 
In the Subjecttbl I have:
SubID Subject
1 Red
2 White
3 Blue
4 Green

In the Listtbl I have:
ListID SubID List
1 1 Roses
2 2 Clouds
3 3 Sky
4 4 Grass

There is a little "+" beside the SubIDs which shows what is in the Listtbl.
When you click on the "+" the Subject and List shows up I guess this links the tables.

The Listtbl has the same "+" but is linked to a FAQtbl. The FAQtbl is bound to the form "frmFAQ".

Now in the Rowsource of the Combobox I have this:
Expand|Select|Wrap|Line Numbers
  1. SELECT Subjecttbl.SubID, Subjecttbl.Subject FROM Subjecttbl; 
the colum count is 2 and the column widths is 0";0.7875" for the combo box
as I stated before do I bound the combox to the form SubID or the table?

but I have nothing in the listbox. Is any suppose to go in the rowsource for the listbox?
Feb 7 '07 #7
Rabbit
12,516 Expert Mod 8TB
The subject shows up in the combo box but when I click on the the subject nothing shows up in the list box

Expand|Select|Wrap|Line Numbers
  1. Me!Qlist.RowSource = "Select Subjecttbl.Subject From Subjecttbl Where SubID = " & Me!Subject 
In the Subjecttbl I have:
SubID Subject
1 Red
2 White
3 Blue
4 Green

In the Listtbl I have:
ListID SubID List
1 1 Roses
2 2 Clouds
3 3 Sky
4 4 Grass

There is a little "+" beside the SubIDs which shows what is in the Listtbl.
When you click on the "+" the Subject and List shows up I guess this links the tables.

The Listtbl has the same "+" but is linked to a FAQtbl. The FAQtbl is bound to the form "frmFAQ".

Now in the Rowsource of the Combobox I have this:
Expand|Select|Wrap|Line Numbers
  1. SELECT Subjecttbl.SubID, Subjecttbl.Subject FROM Subjecttbl; 
the colum count is 2 and the column widths is 0";0.7875" for the combo box
as I stated before do I bound the combox to the form SubID or the table?

but I have nothing in the listbox. Is any suppose to go in the rowsource for the listbox?
I'm sorry, it was my fault in the first place for giving you the wrong code.

For the Combo Box:
Row Source = SELECT Subjecttbl.SubID, Subjecttbl.Subject FROM Subjecttbl
Column Count = 2
Column Width = 0;0.7875
Bound Column = 1

For the After Update event of the Combo Box:
Expand|Select|Wrap|Line Numbers
  1. Me!QList.RowSource = "SELECT Listtbl.List FROM Listtbl WHERE SubID = " & Me!Subject
  2.  
Feb 7 '07 #8
I'm sorry, it was my fault in the first place for giving you the wrong code.

For the Combo Box:
Row Source = SELECT Subjecttbl.SubID, Subjecttbl.Subject FROM Subjecttbl
Column Count = 2
Column Width = 0;0.7875
Bound Column = 1

For the After Update event of the Combo Box:
Expand|Select|Wrap|Line Numbers
  1. Me!QList.RowSource = "SELECT Listtbl.List FROM Listtbl WHERE SubID = " & Me!Subject
  2.  
Nothing shows up in the listbox when I make a selection from the combox box.
Feb 8 '07 #9
Rabbit
12,516 Expert Mod 8TB
Try putting the code in the On Change event.
Feb 8 '07 #10
Try putting the code in the On Change event.
I put this in the After_Update of the combobox
Expand|Select|Wrap|Line Numbers
  1. Static str1 As String
  2. Dim SQL As String
  3. str1 = str1 & Me![Combo0].Value & ","
  4. Me![List2].RowSource = str1
  5. Me![List2].Requery 
and this in the Rowsource of the combobox
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT Subjecttbl.SubID, Subjecttbl.Subject FROM Subjecttbl; 
What happens now is the comboxbox has the subjects in it but the list box displays numbers instead of a list like when I click the first subject in the combobox a number 1 is displayed in the listbox or a 2 you if I click on the second subject.

the column count is 1 which shows the numbers when I set it to 2 or 3 nothing shows in the listbox. colum width 0.7875. I also change the list box to Value list.
Feb 10 '07 #11
NeoPa
32,556 Expert Mod 16PB
Try putting the code in the On Change event.
I put this in the After_Update of the combobox
Expand|Select|Wrap|Line Numbers
  1. Static str1 As String
  2. Dim SQL As String
  3. str1 = str1 & Me![Combo0].Value & ","
  4. Me![List2].RowSource = str1
  5. Me![List2].Requery 
and this in the Rowsource of the combobox
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT Subjecttbl.SubID, Subjecttbl.Subject FROM Subjecttbl; 
I think Rabbit is away for a few days, but I'm sure he'll want to know why you chose to put it in the AfterUpdate event of the ComboBox rather than where he suggested in the OnChange event (In case you weren't sure, they are not the same thing).
Feb 10 '07 #12
I think Rabbit is away for a few days, but I'm sure he'll want to know why you chose to put it in the AfterUpdate event of the ComboBox rather than where he suggested in the OnChange event (In case you weren't sure, they are not the same thing).
I finally got it to work. I tried putting the code in the OnChange event but it did not work. So I put it back in the AfterUpdate event and modified the code
Expand|Select|Wrap|Line Numbers
  1. Dim SQL As String
  2. SQL = "Select List From[Listtbl] where [SubID] = " & Me![Subject].Value
  3. Me![Qlist].RowSource = SQL
  4. Me![Qlist].Requery
  5.  
Thank you for all of your help Rabbit and NeoPa.
Feb 15 '07 #13
Rabbit
12,516 Expert Mod 8TB
Not a problem, glad you got it working.
Feb 15 '07 #14
NeoPa
32,556 Expert Mod 16PB
I guess the AfterUpdate event was the right place after all then :D
Glad you got it working anyway.

Just as a final point, it's very rarely necessary to add the .Value part to a control as it is the default property in most (if not all) cases. Sometimes people choose to for documentary purposes, and that's fine, but it's not required.
Feb 17 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John M | last post by:
I have 1st list box from table Grain 2nd list box from related table Varieties, related by GrainID At the moment Grain listbox is on its own form and Variety listbox is on subform in Grain form....
3
by: MS | last post by:
I would like to be able to examine a folder, and populate a List Box or Combo Box with the files that are in it. How would you go about doing this? The reason is that I would like the user to...
1
by: Devin Wood | last post by:
Hi, I have a page with a ListBox on it, and It's also have a some buttons to populate the ListBox by using JavaScript. I have no problem with populate the listbox using JavaScript. But when the...
0
by: Thief_ | last post by:
I have a combobox on form1 which I want my user to be able to edit it's values, so I created Form2 with a listbox. I then placed the following code in Form2 so that the values of the combo on Form1...
7
by: technocraze | last post by:
Hi guys, I encountered this error while using the AfterUpdate event for my listbox. Error: Update or CancelUpdate without using AddNew or Edit. What i wanted to achieve is just to display the...
3
by: deejayquai | last post by:
Hello Simple one this I guess, but I'm quite stuck at the moment. I would like to update the records displayed in my listbox (lstStudents) using criteria selected from my combo (cboForm) in a...
4
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt...
4
by: =?Utf-8?B?R3JlZw==?= | last post by:
Can someone give me e simple example of to populate a combo box / list box using an ArrayList? THanks.
9
by: weirdguy | last post by:
Hello, Just for anyone information, there is a similar title "Search in Listbox" but it is via Combo Box. In case, anyone need it, I put a link to here. Please let me know if I break any rules...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.