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)
Related
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 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
I have a database table in Microsoft Access. I want to assign the default value of one of those fields to another field that happens earlier in the same table.
I tried the value =[Form1].[AssignedByes] with no luck and the same for =[AssignedByes] but with no luck.
Can anyone help?
The field name I'm assigning the value to is [RemainingByes] and the field that assigning the value of is [AssignedByes].
To get around this problem, I create a Query that basically functions as the table. I use it instead of the table when creating other queries, forms, etc.
I'm guessing that you copy [AssignedByes] into [RemainingByes], because [RemainingByes] will start off with the value of [AssignedByes], but you want to be able to change it later.
So, you can set up the Query with a calcualted field called [xRemaningByes]. You would enter the changed values in [RemainingByes]. Then set up a formulat in [xRemainingByes] that returns the value of [RemainingByes] if there is one, and it not, it returns the value of [AssignedByes].
[xRemainingByes]: Iif([IsNull([RemainingByes],[AssignedByes], [RemainingByes].
When you use the Query instead of the Table, you will use [xRemainingByes], which is the Query's calculated field, instead of the Table's field [RemainingByes].
ANOTHER OPTION ---
Create a field in the Table such as [RemainingByesOverride], and that would be where you hard enter the changing data. Then get rid of [RemainingByes] in the table and only have it in the Query as a calculated field.
[xRemainingByes]: Iif([IsNull([RemainingByesOverride],[AssignedByes], [RemainingByesOverride]
A field's default value is applied at the instant a new record is created. So you can't use [AssignedByes] as the default value for [RemainingByes], because [AssignedByes] doesn't have a value yet.
OTOH, if you've assigned a default value property for [AssignedByes], use that same property for [RemainingByes].
If your Access version is 2010, see whether you can use a data macro as a pseudo-trigger to accomplish what you want.
You could also try binding a form to the table. In the after update event of the control bound to [AssignedByes], you can assign a value to another control bound to [RemainingByes]. This could allow you to do what you need in the form, but won't apply for changes made outside the form.
I'm trying to follow Microsoft's example on how to add an "All" option to a ComboBox in Microsoft Access, but their article does not do an adequate job of providing guidance, aside from specifying the code.
What I'm trying to do is build a form that allows a user to select an option from a ComboBox (the options are generated from records in a table), and then build a report filtered based on the user's selected option. The ComboBox consists of 2 columns: the primary key/ID of the records and their displayable names.
I can't understand the VBA code Microsoft provides enough to figure out what is going on, but I would like the "All" option in my ComboBox to either have a blank primary key/ID, or one that = 0. That isn't the case, as selecting the "All" option when using the form results in the error message "The value you entered isn't valid for this field". This leads me to believe that the "All" text is getting filled into the primary key/ID column instead of the display column. The example instructs me to assign the display column number as the "Tag" property of the ComboBox - and in this case, my display column number is 2. However, this (and pretty much any other value I add) results in the aforementioned error message.
Any idea if Microsoft's example is even applicable to my case, or do I need to adjust their code somehow?
Check the Control Source property of your combo box. Sounds like it may be bound to a field in the form's record source. If you make it an unbound control (nothing in the Control Source property) you should be able to select any item from the combo's Row Source without Access complaining at you.
Say your combo's Row Source is a query like this:
SELECT id, disp_name
FROM YourTable
ORDER BY disp_name;
You can add an "all" row with a UNION query:
SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT TOP 1 0, "**ALL**"
FROM AnyTable
ORDER BY disp_name;
AnyTable can be just that. If you happen to have a table which contains only a single row, use that one ... and you wouldn't even need the TOP 1 part. Just try not to use some ReallyBigTable as AnyTable.
Edit: Actually some ReallyBigTable would be fine if it has a primary key or other unique field which you can use in a WHERE clause to retrieve a single row:
SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT 0, "**ALL**"
FROM ReallyBigTable
WHERE pk_field = 1
ORDER BY disp_name;
UNION ALL will return all combined rows. If you have any duplicate rows, you can thin them out by using just UNION instead of UNION ALL.
I have a form in Access with a combobox on it.
The combobox gets the displayed values through an SQL select statement by
means of the RowSource property which is set to something like
select description, id_of_table, col_foo from tblTable
Since I usually need the selected id_of_table, the property BoundColumn is set to 2. So, I can access this value (for example in an AfterUpdate Event) like
var_id_of_table = me.cboWhatever
which works with no problems.
Now, at times, I do not only need id_of_table but also the value of col_foo. So, the question is, how would I go about getting this desired value. I thought I could get it by
something like
var_foo = me.cboWhatever("col_foo")
but it didn't work.
I'd appreciate any input on this matter.
Thanks / Rene
You need:
var_foo = me.cboWhatever.column(n)
Where n is the column number starting from 0.