How to get bound column value in VBA -MS Access - ms-access

I have a combo box in my form which I filled from another table role.
role has two columns : Id and Role
Given my table in Row Source and Bound Column value as 2 and column widths as 0cm;2cm
I can see my Role in combo box which is fine. What I need is
On Submit, I want to read the Id value in VBA for the Role selected. How to achieve this -- Resolved it by setting Bound Column value as 1
How to set Default Value as one of the Role -- Help to make this happen
I have to set similar case for my ListBox in same form

Since you have a multi-column combo box, both questions relate to the same combobox property : column
MyComboBox.Column(Index, Row)
Row is optional.
Use 0 to refer to the first column, 1 to refer to the second column,
and so on. Use 0 to refer to the first row, 1 to refer to the second
row, and so on.
Question 1: Suppose you bound your id to the second column. In the submit code, you can retrieve your id like this: myid = MyComboBox.Column(1)
Question 2: Suppose you want to assign role id = 3 and your id is bound to column 2. In the Load event of your form, do this : MyComboBox.Column(1) = 3

Related

Access 2013 Sum field column by ID number

My simple form
Here is my simple purchase order form.
The form based on 2 tables. One named POMaster (data being POID and PODate) and the other one named Items(data being What you see plus a type called POIDno which is linked to the POID field to store items by POID)
How do I get Text10 to equal the sum of ItemTotal Column (By POID ofcourse) and if possible How do I get this value to store in a (hopefully) new data field in POMaster I would name POTotal?
Put a hidden total text box on the sub form and set your text box to the value of the hidden control.
You shouldn't store the value in the header as it can always be calculated.
However if you are storing it, you would have to determine a specific point in your process and ideally make sure no more updates can be made to the order lines, as that would mean having to re-calculate your stored value.

How to reference a comboboxes to input values into a query?

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

Access - Excluding first column from lookup values

I have this database and a field's lookup is set to comboBox with 'bound column' to 2 and 'column count' to 2.. My row source's first column is ID (AutoNumber), and my second column is Name(Text). When I go to Datasheet View and select a value it displays ID column value.
How to make it display a Name column value?
There are two basic ways to achieve this manually. This has to do with updating properties of the combo box. The particular properties are listed under Data or Format.
Return the ID as the first column, set the column count to 2, set the data bound to column 1, and define column widths with the first width = 0. Something like, 0";2" This way technically both columns display, but the first one has 0 width.
Or my preferance
Return the ID as the second column. Set the column count to 1 and bind the value to column 2. Doing this only the first column will display, but the 2nd column will be the value. The reason I prefer this method is that you can let Access determine the size of the drop-down column instead of defining it.
I just managed it in one way. In Design View, from dropdown list of your field (the one with lookup comboBox) select Lookup wizard.. Then go through the wizard, and that's it. However, I'm not sure how the wizard solves it.
Select the column1, column2, column1(again)
Set width = 0.01;.5;.5
column1 is not visible, but is the value you want in your field

Hide items in Access 2007 combo box without also clearing the text for records that refer to the hidden items

Say I have two tables in Access 2007:
Table 'Person:
Id : Autonumber
Name : Text
IsActive : Yes/No
Table 'Note':
Id : Autonumber
PersonId : Number (foreign key, Person.Id)
Note : Memo
I have a form that lists all entries in the Note table, and allows users to insert/update records. The control for the Person field on the form is a combo box with
Control Source = PersonId
Row Source = SELECT Person.Name, Person.Id FROM Person;
Bound Column = 2
Column Count = 2
Column Widths = 3cm;0cm
Now I want to limit the combo box so it only allows active persons to be selected, but when I add a 'WHERE Person.IsActive' to the RowSource query, the combo box for all records which refer to inactive people show up blank. I thought that setting the 'Limit To List' option to 'No' would fix this, but when I do Access forces me to change the displayed column so that the combo box now displays the Person ids rather than the Person names, which is not helpful.
Is there a good way to have the best of both worlds? I want to be able to hide inactive people from the combo box list, but still have inactive people show up as the value for the combo box in records that were created while currently-inactive people were still active.
Edit: related question: Custom row source for combo box in continuous form in Access
You can add code to the current event that either changes the row source according to whether the person is active or inactive or you can show a textbox and hide the combobox on the same criterion.
if you add the isactive column to the Row Source and display it so the user can see which people are active
Row Source = SELECT Person.Name, Person.id, Person.IsActive FROM Person
Column Count = 3
Column Widths = 3cm;0cm;1cm
then in the before update event of the combo box
Private Sub Combo10_BeforeUpdate(Cancel As Integer)
If Not Me.Combo10.Column(2) Then
MsgBox "You can only pick active people"
Cancel = True
End If

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.