473,385 Members | 1,569 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.

Populating ListBoxes and ComboBoxes with an SQL Query

I'm building a form and I'm having trouble with my SQL query to properly populate a list box. In this database, I have a table of families and then a table of patients within that family (Relationship is one to many, linked by a FamilyID field). The form displays the Family's demographic information, and I'd like to include a ListBox that includes all of the Patients within that Family (which I then hope will allow the user to navigate to the forms associated with each Patient). I set up a query to gather the FamilyID, PatientIDs and other demographic information that I wanted to include, and used it to populate this ListBox.

Here is the SQL text from the Row Source of the List Box:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    [queryFamilyPedigree].[FamilyID], 
  3.    [queryFamilyPedigree].[PatientID],
  4.    [queryFamilyPedigree].[Expr1],
  5.    [queryFamilyPedigree].[DOB], 
  6.    [queryFamilyPedigree].[Expr2], 
  7.    [queryFamilyPedigree].[Expr3], 
  8.    [queryFamilyPedigree].[Expr4] 
  9. FROM [queryFamilyPedigree] 
  10. WHERE FamilyID=[Form]![FamilyID] 
  11. ORDER BY [PatientID];
It doesn't seem to matter what I do, the ListBox is always blank (The column headers appear properly, and I don't get any error messages). To make sure that I had gotten my SQL query built correctly, I created a ComboBox and copied the SQL text into its Row Source (its exactly the same) and the Combo Box populates properly with all of the Patients for that family. Although the the options in the Combo Box remain the same when I switch records to a new family (I figure this will be fixed by a requery event, but I haven't progressed to the stage of implementing VBA scripts quite yet). Does anyone have any ideas? Is there a property on the List Box that I'm missing that might be causing the issue? I'm at a loss.

Danke,
Chris Patterson
Aug 31 '12 #1

✓ answered by twinnyfo

Chris,

Although I admit that I am not exactly certain what you are asking..... You might be answering your own question when you say, "Although the the options in the Combo Box remain the same when I switch records to a new family (I figure this will be fixed by a requery event, but I haven't progressed to the stage of implementing VBA scripts quite yet)."

This is a very simple operation using VBA, on the After Update event when you select a new family:

Expand|Select|Wrap|Line Numbers
  1. Me.ComboBoxName.Requery
  2.  
If you have no experience at all using VBA, you may want to do some research.... I would provide some links to get you started, but I am away from my desk right now.....

9 6698
zmbd
5,501 Expert Mod 4TB
Chris,
There are several things that can cause you some issues, normally it is because the SQL is either faulty or there are no records being pulled.

However, as the SQL is good in the combobox then I suspect this has to do with the List box properties.

Under the format tab, make sure you have the column count set correctly, the column widths set correctly, and that the width is set correctly (or you cut of the ends :) ).

I have some code that will pull the properties out... let me hunt it down and I'll post it in a minute if the above doesn't help.

-z
Aug 31 '12 #2
twinnyfo
3,653 Expert Mod 2GB
Chris,

Although I admit that I am not exactly certain what you are asking..... You might be answering your own question when you say, "Although the the options in the Combo Box remain the same when I switch records to a new family (I figure this will be fixed by a requery event, but I haven't progressed to the stage of implementing VBA scripts quite yet)."

This is a very simple operation using VBA, on the After Update event when you select a new family:

Expand|Select|Wrap|Line Numbers
  1. Me.ComboBoxName.Requery
  2.  
If you have no experience at all using VBA, you may want to do some research.... I would provide some links to get you started, but I am away from my desk right now.....
Sep 1 '12 #3
Thanks for the quick replies. I don't very much experience with Visual Basic, but I have done some C++ programming, and I've got a couple of books on VB and Access that I'm working with to help me with the stuff I don't know. When I can't find the answer in there, I tend to look online, so any sites you want to recommend, I would greatly appreciate. I have read about events and a couple sections for programming them, but as I said earlier, I'm taking this one step at a time. This is the first database that I've constructed, but I've worked with them for many years. I will give your ideas a shot and get back the results after the weekend.

Danke.
Sep 1 '12 #4
zmbd
5,501 Expert Mod 4TB
Twinney,

Unless Chris is changing the recordsource at runtime in either the onload or onopen events, or there is some other control changing the recordsource during one of its events, then the the SQL in OP should populate the control and work without a requery of either the form or the control.

HOWEVER

You did force me to take a more careful read of the OP...
My thought is that Chris should actually be using a subform... ( I just don't do my best work when trying to talk to the wife, cook dinner, herd the kids into the tub... oh wait... to the table... THEN the bathtub! :) )

http://bytes.com/topic/access/insigh...filtering-form

What I can see happening here is
- Parent form with the Demographic information as OP using the table families as it's record set
- Child form based on the SQL string in OP shown in datasheet (or directly on the table depending on the data types and user friendlness of the table).
- If the Child form is left open for updates and additions that could be useful... the form could actually be locked and a command button used to unlock if needed.
- The user can then select the record within the Child form.... a few designs... I've used an unbound control with an onclick event to launch code... or a command button on the parent form depending on what is being done.

Just a thought!

-z
Sep 1 '12 #5
twinnyfo
3,653 Expert Mod 2GB
Z,

I agree, that based on the query, it appears that the best solution is a form-subform setup, in which selecting the family automatically filters the subform. However, the original question appears to want to populate a list box based on the value(s) on the form, which, I believe, would require a requery of the control.

----------------

Chris,

Could you please explain exactly what it is you are trying to do here? Either Z, or I, or any of the other gurus here would love to help, but, as you can see, even Z and I are approaching things from two different directions.

If you can, provide some additional details and we all will work toward a solution.
Sep 1 '12 #6
zmbd
5,501 Expert Mod 4TB
Twinny,
Partially right... this is simply not my best work from home...

K... so... I missed the "where" clause in the OP sql...
Placing the code tags and stepping the SQL should have been my first step in helping OP.
Also, I missed the fact that OP was talking about two different comboboxes and a list box in the final paragraph... this is where I finally chose the door with the lion (or was that a tiger... claws and teeth either way!)

Do we really have two questions? Or a single question that could be solved by a requery.

For this one... I will have to go with Twinny and say try the requery on the listbox in the afterupdate_event of the ([form]![FamilyID] control).

I still stand by my suggestion for a form/subform. Done correctly, a lot of VBA code can be eliminated and the work pushed to the SQL engine.

-z
Sep 2 '12 #7
Okay, I'm following along with the form/subform suggestion. I had tried that previously, but didn't like the presentation of the data as much and I had issues with not all of the records showing up (which I've now determined was because I didn't understand the INNER JOIN vs LEFT JOIN statements in the SQL query). I couldn't follow the link that you posted for the "Example of Filtering on a Form", but I'd still like to see it to look at other ideas for better organizing this data. Also, forgive me, I'm not sure what the term "OP" refers to.

Twinny's suggestion for the requery script, did work with some minor changes. It didn't work under the After Update event for the FamilyID control on the form, but I think that is because the field isn't being used to navigate to a specific record, so it doesn't get updated. However, when I put the same script under the On Current event for the form, it updated both the list box and the combo box appropriatedly for that record, and it changes with each record as I scan through them one at a time. SO IT WORKS!!!

I still need to put in some navigation tools in to better access a specific record (I'm currently clicking through each record one at a time). Now that the List Box is populating correctly with all of the Patients within that family, I'd like to set up a couple of command buttons "New", "Update", and "Delete", where "New" opens up the a blank version of the FORMPatientData (but with the FamilyID field prepopulated with the current FamilyID being viewed on the FORMFamily), "Update" opens the FORMPatientData to the record selected in the List Box we just set up, and "Delete" deletes the patient record selected in the List Box.

How do I reference the selection that was made in the List Box? I figure that the process of creating the VBA script will be a much longer discussion. Should I continue it here or should I create a new thread to ask these questions?
Sep 4 '12 #8
Also would posting screen shots of the form design help?
Sep 4 '12 #9
zmbd
5,501 Expert Mod 4TB
Chris,

Fixed the link in the post... don't know what happened there; however, it does appear to be working now.
If you still can't get there from the link... go all the way to the bottom of this page, click on the "Microsoft Access / VBA Insights" link... the article is #165. You might also find the cascading combobox referenced in this article to be usefull (#39) (and you'll also get some help refernceing the listbox/cbo selction there)

Record movement can be a pain... the cascading cbo may be usefull there... topic for another thread.

As for the remainder of your post, buttons, navigation, etc... these questions deserve their own thread and we will gladly help you work through those questions as you post them (and it's in the rules ;-) )

-z
Sep 4 '12 #10

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

Similar topics

2
by: Lamine Darbouche | last post by:
Can anybody help? I need to have three list boxes automatically populating each other, (ie when region is selected from the first listbox, it will populate automatically the second one which is...
2
by: Justin Koivisto | last post by:
Firstly, I want to thank all that have helped me out with getting grips on Access each time I've had questions. This has got to be one of the most helpful groups that I've posted to over the years....
2
by: Scot Niklos via AccessMonster.com | last post by:
Sorry if it is way to much information but I'm trying to cover all bases Access 97 A= combobox that queries Distributor B= combobox that limits customers by distributor in a C= combobox that...
6
by: Ralph2 | last post by:
Some time ago with a lot of help from this group I made a reasonably successful database to keep track of our shop drawings. However the searching mechanism is too complicated for the occasional...
3
by: dom | last post by:
Hello All, The combo/list boxes in MS Access has the ability to show multiple fields in separate columns. I have found that in VB/VB.NET that combo/list boxes can only show one field in one...
14
by: Jim Andersen | last post by:
I have a problem with this standard employee-supervisor scenario For pictures: http://www.databasedev.co.uk/self-join_query.html I want to show all employees "belonging" to a specific...
2
by: mamun | last post by:
Hi All, The problem is as follows: I have a table from where I am getting consultant's name and populating the dropdown list control. But I want to populate the default one which I got it as...
3
hyperpau
by: hyperpau | last post by:
Hi there guys! I have a Form where there are three comboboxes. This comboboxes are used as references for the parameter of 3 fields in a query. when I hit a command button in my form, it opens...
1
by: Flo100 | last post by:
Hi All, I have a combobox that I am populating using a query but after that I also want to add just one item at the beginning or the end of this list. Is it possible? -Florence
0
by: Anil Gupte | last post by:
I am having some issues with Listboxes Here is my code: Dim DSContent As New DataSet DSContent = L3GContent.GlobalDataSet URLListBox.BeginUpdate() For Each dr As DataRow In...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.