How to reference a comboboxes to input values into a query? - ms-access

How to a get the value of value selected in a combo-box? I have a combo-box called "cndGetUsage" in a form called "frmStationUsage". How do I get the value in the form and check to see if a value was selected?
This are being referenced from a module called mMainOutputs.

myval = Me.cndGetUsage.Column(1)
or
myval = [Forms]![frmStationUsage]![cndGetUsage].Column(1)
these will tell you what the current value in the box is. the columns are because in access your combo box can have multiple columns. also if you set the bound column then you don't have to specify it.

Where are you populating the list from? It works great if you're using a Select statement, from a table with a unique ID field as a unique key. Your combo box index is zero based. If you have more controls in the form you can use the Recordset Clone and Bookmark statements to move back and forth as you select items from the combo box

Related

Populating listbox from another listbox selection

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).

How to enable filtering on a field defined by DLOOKUP()?

I have a form TForm based on table T, set up as a datasheet. My goal is to add a filterable column to the datasheet where the column's value is calculated from a query using another column's value.
I tried to do this by adding a text box currentBox to T. The control source for currentBox is:
=DLookUp("name","currentStatus","itemID=" & [ID])
where [ID] is a field in T and currentStatus is an aggregate query on a table that T is related to.
I can filter on all the fields in TForm that are in T. But I can't filter on currentBox, even though it also appears as a column in the form; clicking on the column header doesn't do anything.
I'm guessing the problem is that currentBox is not bound to a field in T; is there a way to work around this?
Here's a VBA solution:
Add a combo box (aka drop-down) object to your form header. This drop-down's source will be an independent query that displays all the values your Dlookup() currently pulls (names?) and stores the itemID. Let's call it ObjPickName in this example.
Add an AfterUpdate event to ObjPickName that will filter your form for you (your form will still be based on T). The code will be something like:
Private Sub Combo_ObjPickName_AfterUpdate()
Me.Form.Filter="[itemID]='" & Me.Combo_ObjPickName.Value & "'"
Me.Form.Filteron=True
End Sub
The way that I ended up solving this was to add a field to T, and have that field updated during the AfterUpdate() event with the value from the DLookup() call. Because the field is now no longer query-based, it can be used to filter the form.

MS Access ComboBox.Column - use name instead of index?

i have ComboBox controls with multiple columns as a Row Source in an Access 2007 form.
i'm currently getting the selected values out of each ComboBox item this way...
value = ComboBoxName.Column(i) 'where i is the index.
i would like to use the actual names of the columns as with Recordset's...
value = ComboBoxName.Recordset.Fields("columnname")
(please note that this does not work properly on the Recordset's of the ComboBox'es: it only works AFTER the first time the ComboBox is changed)
my questions:
can i do something to "make this work" on a ComboBox's Recordset?:
value = ComboBoxName.Recordset.Fields("columnname")
is there a method that directly gets the value of the selected record using the name ?
does the ComboBox or it's Recordset have a method i can use to get a column index by specifying a column name ?
i would like to avoid writing a function and i don't feel comfortable specifying column indices which may change in the future.
There is no simple way to do this. You can create a recordset, or you can assign values to variable for each column name (FirstCol=0).
As an aside, why would the columns change? If you are using a select statement, the combo will fail if the table is changed.
PFIELD = Me.Form.Combo6.Column(0, 0)

Access 2007 Using the ID value from a list box in VBA/SQL statement

Guys...if I want to run a button click event that takes a list box and uses the ID field that is in the listbox in a SQL statement in VB, then is it
me.MyListbox.selected
or
me.MyListbox.value
to get that value? for some reason I have tried both and neither seem to be working. .value returns an empty value, and .selected generates an error stating the argument is not valid.
thanks
justin
If the ID is the bound column and the listbox is not multiselect, you can use just the name of the listbox without any other qualifier. If the ID is not the bound column, then use the column property to get the value : MyListBox.Column(n) where n is the column number starting from zero.
For multiselect listboxes, you need to iterate through the selected items to get a list to use with SQL.
If you are using the query design window or a control on a form or report, you cannot use Me, you must either use the full reference (Forms!Formname!ControlName) or for a control on the same form, just the name of the listbox.

MS Access Dropdown List/Combo Box

This should probably be pretty simple but my Google-Fu is as yet unable to find an answer. I simply want to create a dropdown list in Access so that upon selection I can perform some action based on the value of the selection. For instance, I have a list of people and I would like to populate the combo box so that their names appear in the list but the "value" is set to their ID (the primary key).
It sounds like you might be asking how to display something in the dropdown other than the ID while keeping the ID as the returned data from the dropdown. If that's the case set the Bound Column to the ID field (usually 1) and (assuming the name field is next) set the Column Count to be 2 and the Column Widths to be 0";1" or 0";[whatever width you need].
You will need to hook into the onchange event for the dropdown list.
and from MSDN
How have you set the properties for your combo box?
Perhaps you could try setting (assuming you are pulling data from Table1 with fields ID and Field1
Row Source: SELECT [Table1].[ID], [Table1].[Field1] FROM Table1;
Row Source Type: Table/Query
Bound Column: 1
Column Count: 2
Column Widths: 0", 1"
and then hook into the onchange event as Chris Ballance suggests. The value property of the combo box is ID; the text will be what is in Field1.
OK, I figured it out even though it was a bit counter-intuitive. An Access Combobox can have as many values as you want (instead of just one key on value). By default all of the values are are shown in the list so you need to hide certain columns by setting their widths to 0. That is done via the ColumnsWidths property in the property pane. ColumnWidths takes a comma separated list of values which corresponds to the order of the columns in the list. I hope this helps someone.