Populating listbox from another listbox selection - ms-access

I am pretty much a newbie to using VBA in Access and I'm having trouble with something that seems like it should be quite simple.
I have two listboxes (called LB1_ID and LB2_ID) on my form (MainForm) that I want to list related IDs from their respective Row Sources. I need LB2 to be populated based on the selection in LB1. They both have Row Sources from the same Table (Table1) and it is a many to many relationship of Requirement IDs ("Req ID1" and "Req ID2"). My current form, which is not working, has the Row Source of LB1 as:
SELECT Table1.ID, Table1.[Req ID1] FROM Table1 ORDER BY Table1.ID;
and the Row Source of LB2 as:
SELECT Table1.ID, Table1.[Req ID2] FROM Table1 WHERE ([Forms]![MainForm]![LB1_ID]=Table1.[Req ID1]);
When I make a selection in LB1, nothing happens in LB2. The column widths are formatted correctly and I can get it to work if I use Me.[Forms]![MainForm]![LB1_ID] but I have to type out the LB1 selection manually in a popup box if I use that.
What am I missing?

If your listbox is multi-select, you cannot use a simple form reference as query criteria. If it is not multi-select, keep in mind that its value may be a hidden column (usually an ID field), so there are two possible issues and solutions:
Possible Issues:
Single-Select listbox has an ID field that is hidden (column width = 0") and you are matching it to the wrong field in your table. To check the output of the listbox, open the VBE and type ?[Forms]![MainForm]![LB1_ID] into the immediate window and press enter when your form is open in form view and a row is selected in LB1_ID. If the returned line is what you expect, then the problem must be elsewhere.
Multi-Select listbox property is enabled. In this case, your query will not work, because the listbox will only return Null. You will need to write some VBA to loop through the rows and figure out which ones are selected, which is a bit of a pain. Ultimately you'll build some code that will alter your query with the specific criteria for each selected row. Instead of explaining here, take a look at this article for a tutorial.
The .Requery method is still important to put in the AfterUpdate event of your first listbox to refresh the second.

Your query seems to work, but you need to refresh your listbox2 whenever you make selection into listbox1, so if both listbox are in the same form add this event handler :
Private sub LB1_ID_Change()
Me.LB2_ID.Requery
End sub
Without this, your listbox2 will only get populated once on load based on the initial value of listbox1.
Also, if you have not already done it, I would recommend to add your listbox1 control as a parameter into your listbox2 query (in query builder, right click -> parameters).

Related

Can't select combobox entries after applying filter to column

I have a column full of comboboxes. The comboboxes work fine on their own.
But if I right click on the column and apply a filter, e.g. sort alphabetically, the comboboxes still show with the correct entries, but I can no longer select another entry in the box. E.g. the box says "supplier", but I want to select "customer". Clicking on "customer" simply does nothing.
When I apply the filter, apparently the entire formula "refreshes" itself. Therefore I am currently trying to add some code to
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer).
However, being rather new to MS Access, I have no idea whether this is a step in the right direction and if so, what code I should have in Form_ApplyFilter in order to make my comboboxes clickable/updateable again.
The properties of the comboboxes are:
Row source type = Table/Query
Row source = SELECT Tbl_Liability.ID, Tbl_Liability.Liability FROM Tbl_Liability;
Bound column = 1
Enabled = Yes
Locked = No
And the form the comboboxes are in has ALLOW FILTERS set to YES (but no code in Form_ApplyFilter).

Trying to populate two comboboxes based upon selection of another combobox

I've been searching this issue on Microsoft, Stackoverflow, techinthenet.com and several others, with no real solution that I can find.
I am trying to get two comboboxes (cboCourse and cboVols) to populate based on the selection of cboTrainee_Name. The code for the first combobox is:
SELECT DISTINCT [qryBooks].[PName] FROM qryBooks WHERE qryBooks.Complete=No ORDER BY [qryBooks].[PName];
Everywhere I've searched gives assistance and samples to populate the first combobox, then the second combobox based upon what is selected in the first. I need both cboCourse and cboVols to populate based on the result of the same query. What I have so far is:
Private Sub cboTrainee_Name_AfterUpdate()
cboCourse.RowSource = "SELECT DISTINCT [qryBooks].[Book] FROM qryBooks WHERE [qryBooks].[PName]=[Forms]![frmIntro]![cboTrainee_Name] ORDER BY [qryBooks].[Book];"
cboVol.RowSource = "SELECT DISTINCT [qryBooks].[Vols] FROM qryBooks WHERE [qryBooks].[PName]=[Forms]![frmIntro]![cboTrainee_Name] ORDER BY [qryBooks].[Vols];"
End Sub
This works for cboCourse, but cboVols will not populate. I know there has to be a simple explanation. Any suggestions?
ADDED! I used SELECT DISTINCT because each column in the query contains multiple instances of the same data.
I don't see any problems with your SQL.
You might need to call the Requery method on both combo boxes to get them to refresh after cboTrainee_Name is changed:
cboCourse.Requery
cboVol.Requery
Note that you don't have to set the RowSource property in code for this to work. You should be able to plug in your SQL statements as is in the RowSource property and just the Requery method on the dependent combo boxes in the AfterUpdate event of your cboTrainee_Name combo box.

Access: Fill a combobox with selections from another field

I'm just learning how to use Access and while I've managed to muddle my way through most of what I'm trying to do, there's something I haven't been able to figure out how to do yet.
I have two forms and corresponding tables. In frmProducts is ColorOptions, a multi-select combobox containing a list of possible color options for a product, and Design, a text control for the name. In frmCustomers is OrderDesign, a combobox with a list of items from tblProducts, and OrderColours, a combobox.
Now, this is the problem: I want OrderColours to display list of the color options in tblProducts, but I can't figure out how. I can get it to display the value, but it's not a list of items, just one entry with the 'list' (e.g. a single entry reading "Brown,Red,Green"). I want the user to be able to select a single item from that subset.
Ideally I'd like to do this without messing with VBA or any advanced SQL, but if that's not possible then that's fine as well. I think the issue may be that the Colours field which contains the colours for that product is stored as text, but I'm not sure how else to store it as there's no 'array' or 'list' option for datatypes.
Sorry if I haven't been clear enough, or if this is posted in the wrong sub. I'm a beginner in Access, so I may have not been clear enough or used the wrong terminology. Any help would be much appreciated.
I'm not quite sure I understand exactly how you want this set up, so I'm assuming the following. Please correct me if this is not right:
tblProducts contains (at least) the two fields productDesign and productColour
It is possible for there to be multiple records in tblProducts with the same productDesign but different productColour (different colours of the same design)
There is another table, tblCustomers, in which each record contains a productDesign and one of the corresponding productColours.
So you need the combobox OrderColours to display a list of the possible productColours for the selected value of productDesign in OrderProducts.
Now, set up combobox OrderDesign as follows:
Row Source Type: Table/Query
Row Source: SELECT DISTINCT tblProducts.productDesign FROM tblProducts;
and OrderColour:
Row Source Type: Table/Query
Row Source: SELECT tblProducts.productColour, tblProducts.productDesign FROM tblProducts WHERE (((tblProducts.productDesign)=[Forms]![frmCustomers]![OrderDesign]));
Column Count: 1
and give OrderDesign the following event AfterUpdate:
Private Sub OrderDesign_AfterUpdate()
Me.OrderColour = Null
Me.OrderColour.Requery
Me.OrderColour = Me.OrderColour.ItemData(0)
End Sub
You may well also need to consider what happens when moving between records, if your comboboxes are bound:
Private Sub Form_Current()
Me.OrderColour.Requery
End Sub
in the Form_Current event should do the trick.
Read this for details.

Access combobox value

I have a combobox, and a button, that makes runs a query with the values it gets from combobox, but it does not seem to get the right value.
I tried using
[Forms]![Kooli otsing]![Combobox]
or
[Forms]![Kooli otsing]![Combobox].[Text]
the query did not work, it seems like it does not get the value from combobox. because it worked with normal TextBox.
I ADDED EXPLAINING PICTURE!!!!!
ADDED PICTURE OF VBA EDITOR
ADDED PICTURE OF ERROR AND NO COMMENT AUTOCOMPLETE
Based on the latest comments you posted on your question, you want to use:
[Forms]![Kooli otsing]![Combo19].Column(1)
Here's why. You said you have the following settings for your combobox:
column count: 2
bound column : 1
row source type : table/query
row source: SELECT [Haridusasutused].[ID], [Haridusasutused].[Nimetus] FROM Haridusasutused;
Column count of 2 is telling Access to use the first two columns from your rowsource (the only two columns in this case). Bound column is telling access that the default value of the combobox should be the first column of the row source. In this case, that would be [Haridusasutused].[ID]. Often ID columns are autonumber fields.
The reason you were having problems is that [Forms]![Kooli otsing]![Combo19] was returning data from the ID column (most likely a number) not "Elva Gümnaasium". By adding the .Column(1) you are telling Access to choose the data from the second column (.Column is a zero-based array) of the rowsource, ie, "Elva Gümnaasium".
EDIT: Alternatively, you can change the bound column from 1 to 2 and leave the rest alone (ie, you won't need the .Column(1) part at all).
This works in my application:
[Forms]![Hour-registration]![mwkselect]
^form ^combobox
Maybe try this to refresh:
Me.Requery
Me.Refresh
Have you tried to step through debugger and search for the value through the watch window? For instance put a breakpoint into a button click event, then add [Forms] to the watch window and look into it.
You can use:
[Forms]![Form1]![Combo1].[Text]

Custom row source for combo box in continuous form in Access

I have searched around, and it seems that this is a limitation in MS Access, so I'm wondering what creative solutions other have found to this puzzle.
If you have a continuous form and you want a field to be a combo box of options that are specific to that row, Access fails to deliver; the combo box row source is only queried once at the beginning of the form, and thus show the wrong options for the rest of the form.
The next step we all try, of course, is to use the onCurrent event to requery the combo box, which does in fact limit the options to the given row. However, at this point, Access goes nuts, and requeries all of the combo boxes, for every row, and the result is often that of disappearing and reappearing options in other rows, depending on whether they have chosen an option that is valid for the current record's row source.
The only solution I have found is to just list all options available, all the time. Any creative answers out there?
Edit Also, I should note that the reason for the combo box is to have a query as a lookup table, the real value needs to be hidden and stored, while the human readable version is displayed... multiple columns in the combo box row source. Thus, changing limit to list doesn't help, because id's that are not in the current row source query won't have a matching human readable part.
In this particular case, continuous forms make a lot of sense, so please don't tell me it's the wrong solution. I'm asking for any creative answers.
I also hate Access, but you must play with the cards you are dealt.
Continuous forms are a wonderful thing in Access, until you run into any sort of complexity as is commonly the case, like in this instance.
Here is what I would do when faced with this situation (and I have implemented similar workarounds before):
Place an UNBOUND combobox on the form. Then place a BOUND textBox for the field you want to edit.
Make sure the combobox is hidden behind (NOT invisible, just hidden) behind the textBox.
In the OnCurrent event fill the listBox with the necessary data. Go ahead and "Limit to list" it too.
In the OnEnter or OnClick event of the textBox give the combobox focus. This will bring the combobox to the forefront. When focus leaves the combobox it will hide itself once more.
In the AfterUpdate event of the combobox set the value of the textbox equal to the value of the combobox.
Depending on your situation there may be some other details to work out, but that should more or less accomplish your goal without adding too much complexity.
use continuous forms .. definitely. In fact you can build entire applications with great and intuitive user interface built on continuous forms. Don't listen to Toast!
Your solution of listing all options available is the correct one. In fact there is no other clean solution. But you are wrong when you say that Acccess goes nuts. On a continuous form, you could see each line as an instance of the detail section, where the combobox is a property common to all instances of the detail section. You can update this property for all instances, but cannot set it for one specific instance. This is why Access MUST display the same data in the combobox for all records!
If you need to accept only record-specific values in this combobox, please use the beforeUpdate event to add a control procedure. In case a new value cannot be accepted, you can cancel data update, bringing back the previous value in the field.
You cannot set the limitToList property to 'No' where the linked data (the one that is stored in the control) is hidden. This is logical: how can the machine accept the input of a new line of data when the linked field (not visible) stays empty?
You could also make the value of the combo box into an uneditable text field and then launch a pop-up/modal window to edit that value. However, if I was doing that, I might be inclined to edit the whole record in one of those windows.
I don't think that Access continuous forms should be condemned at all, but I definitely believe that they should be avoided for EDITING DATA. They work great for lists, and give you substantially more formatting capabilities than a mere listbox (and are much easier to work with, too, though they don't allow multi-select, of course).
If you want to use a continuous form for navigation to records for editing, use a subform displaying the detailed data for editing, and use the PK value from the subform for the link field. This can be done with a continuous form where you place a detail subform in the header or footer, linked on the PK of the table behind the continuous form.
Or, if you are using a continuous form to display child data in a parent form, you can link the detail subform with a reference to the PK in the continuous subform, something like:
[MySubForm].[Form]!MyID
That would be the link master property, and MyID would be the link child property.
We also encounter this a lot in our applicatins. What we have found to be a good solution:
Just show all rows in the comboboxes.
Then, as soon as the user enters the compobox in a specific row, adjust the rowsource (with the filter for that row). When the combobox loses the focus, you can re-set the rowsource to display everything.
I have a simpler way to go than Gilligan. It seems like a lot of work but it really isn't. My solution requires having my continuous form as a subform datasheet. On my subform I have two lookup comboboxes, among other fields, called Equipment and Manufacturer. Both simply hold a Long Integer key in the data source. Manufacturer needs to be filtered by what is selected in Equipment. The only time I filter Manufacturer.RowSource is in the Manufacturer_GotFocus event.
Private Sub Manufacturer_GotFocus()
If Nz(Me.Equipment, 0) > 0 Then
Me.Manufacturer.RowSource = GetMfrSQL() '- gets filtered query based on Equipment
Else
Me.Manufacturer.RowSource = "SELECT MfgrID, MfgrName FROM tblManufacturers ORDER BY MfgrName"
End If
End Sub
In Manufacturer_LostFocus I reset Manufacturer.RowSource to all Manufacturers as well. You need to do this because when you first click in the subform, GotFocus events fire for all controls, including Manufacturer, even though you are not actually updating any fields.
Private Sub Manufacturer_LostFocus()
Me.Manufacturer.RowSource = "SELECT MfgrID, MfgrName FROM tblManufacturers ORDER BY MfgrName"
End Sub
In the Enter event of Manufacturer you have to check if Equipment has been selected, if not set focus to Equipment.
Private Sub Manufacturer_Enter()
If Nz(Me.EquipmentID, 0) = 0 Then
'-- Must select Equipment first, before selecting Manufacturer
Me.Equipment.SetFocus
End If
End Sub
You also need to requery the Manufacturer combobox in Form_Current event (i.e. Me.Manufacturer.Requery), and you should set the Cycle property of this subform to "Current Record".
Seems simple enough, but you're not done yet. You also have to reset Manufacturer.RowSource to all Manufacturers in the SubForm_Exit event in the parent form in case the user goes to the Manufacturer combobox but does not make a selection and clicks somewhere on the parent form. Code sample (in parent form):
Private Sub sFrmEquip_Exit(Cancel As Integer)
Me.sFrmEquip.Controls("Manufacturer").RowSource = "SELECT MfgrID, MfgrName FROM tblManufacturers ORDER BY MfgrName"
End Sub
There is still one piece of this that is not clean. When you click on Manufacturer and have multiple rows in the datasheet grid, Manufacturer field will go blank in other rows (the data underneath the comboboxes is still intact) while you're changing the Manufacturer in the current row. Once you move off this field the text in the other Manufacturer fields will reappear.
This seems to work well.
CBOsfrmTouchpoint8 is a combobox shortened to just the dropdown square.
CBOsfrmTouchpoint14 is a textbox that makes up the rest of the space.
Never say never:
Private Sub CBOsfrmTouchpoint8_Enter()
If Me.CBOsfrmTouchpoint8.Tag = "Yes" Then
CBOsfrmTouchpoint14.SetFocus
Me.CBOsfrmTouchpoint8.Tag = "No"
Exit Sub
End If
Me.CBOsfrmTouchpoint8.Tag = "No"
Me.CBOsfrmTouchpoint8.RowSource = "XXX"
Me.CBOsfrmTouchpoint8.Requery
Me.CBOsfrmTouchpoint8.SetFocus
End Sub
Private Sub CBOsfrmTouchpoint8_GotFocus()
Me.CBOsfrmTouchpoint14.Width = 0
Me.CBOsfrmTouchpoint8.Width = 3420
Me.CBOsfrmTouchpoint8.Left = 8580
Me.CBOsfrmTouchpoint8.Dropdown
End Sub
Private Sub CBOsfrmTouchpoint8_LostFocus()
Me.CBOsfrmTouchpoint8.RowSource = "XXX"
Me.CBOsfrmTouchpoint8.Requery
End Sub
Private Sub CBOsfrmTouchpoint8_Exit(Cancel As Integer)
Me.CBOsfrmTouchpoint14.Width = 3180
Me.CBOsfrmTouchpoint8.Width = 240
Me.CBOsfrmTouchpoint8.Left = 11760
Me.CBOsfrmTouchpoint8.Tag = "Yes"
End Sub
What if you turn off the "Limit To List" option, and do some validation before update to confirm that what the user might have typed in matches something in the list that you presented them?
Better...
Set you combo box Control Source to a column on the query where the values from your combo box will be stored.
For Me I think the best way and easiest way is to create a temporary table that has all your bound fields plus an extra field that is a yeas/no field.
then you will use this table as the data source for the continuous for. You can use onLoad to fill the temporary table with the data you want.
I think it is easy after that to loop for the choices, just a small loop to read the yeas/no field form the temporary table.
I hope this will help
Use OnEnter event to populate the combo box, don't use a fixed rowsource.
I've just done similar. My solution was to use a fixed row source bound to a query. The query's WHERE clauses reference the form's control i.e. Client=Forms!frmMain!ClientTextBox. This alone will fill the combo boxes with the first row's data. The trick then is to set an 'On Enter' event which simply does a re-query on the combo box e.g. ComboBox1.Requery, this will re-query that combo box alone and will only drag in the data related to that record row.
Hope that works for you too!
Disclaimer: I hate Access with a passion.
Don't use continuous forms. They're a red herring for what you want to accomplish. Continuous forms is the same form repeated over and over with different data. It is already a kludge of Access's normal mode of operation as you can't have the same form opened multiple times. The behavior you are seeing is "as designed" in Access. Each of those ComboBox controls is actually the same control. You cannot affect one without affecting the others.
Basically, what you have done here is run into the area where Access is no longer suitable for your project (but cannot ditch because it represents a large amount of work already).
What seems to be the most likely course of action here is to fake it really well. Run a query against the data and then create the form elements programmatically based on the results. This is a fair amount of work as you will be duplicating a good bit of Access's data handling functionality yourself.
Reply to Edit:
But as they are, continuous forms cannot accomplish what you want. That's why I suggested faking out your own continuous forms, because continuous forms have real limitations in what they can do. Don't get so stuck on a particular implementation that you can't let go of it when it ceases to work.