I am working on a tool that saves data into a database to later retrieve it and display in the same form
I save data into the database using integer id's
In order to set combobox values I have tried the following
frm.statusComboBox.SelectedValue = initiativeDataGridView.SelectedRows(0).Cells(initiativeDataGridView.Columns("initiativeStatus_Id").Index()).Value
I understand that I am in the wrong, since I need to set the combobox selectedIndex and not the selectedValue. So I guess my main question is how do I identify the combobox index for a particular value member?
Try using ComboBox.SelectedItem instead, but you should add the item as well, so it appears in the dropdownlist. Working with Indexes of ComboBox isn't the best idea, because they can change as you sort your ComboBox, delete or add items.
Related
In access 2010 I am trying to create a listbox in a form. Once I make a selection for a row in the form, the listbox automatically assigns those values to all the rows instead of on a per row basis.
Does anybody know how to change this option such that the listbox assigns the values per row?
I am doing this via the clickable solutions since I do not know any VBA code.
This happens when the listbox is unbound - then the value selected is common for all visible records.
So, create a field in the table used as source for the form. Then bind the listbox to this field.
I need help with MS Access 2010.
One of the client's requirement is they need to be able to input new data and also be able to have a drop down list of the data existing in the table for selecting using a combobox. Im very new to MS Access, only managed to work around it using online tutorials. Im not too clued up with VBA coding. I have tried swicthng around the properties components, still not working.
please assist.
thanks
That's what I have done. Wanted to allow free entry but still assist the user if value already exists.
One option is to use the data table as source for the combobox RowSource. Set the combobox LimitToList property to No. Then the combobox RowSource would be like:
SELECT DISTINCT fieldname FROM tablename WHERE NOT fieldname IS NULL ORDER BY fieldname;
However, if you want a new value to be available for next record for a continuous or datasheet form, the record must be committed to table and then requery the combobox. Record is committed when: 1. close table/query/form; or 2. move to another record; or 3. run code to save. So code in the form Current event like:
Me.comboboxname.Requery
Another option is to have lookup table for the combobox RowSource and set the comobobox LimitToList property to Yes. Then code in the combobox NotInList event to allow new value input by user to be saved to lookup table. Review: https://msdn.microsoft.com/en-us/library/office/ff845736.aspx
Apologies for not being clear... i have however managed to fix this by pointing the row source to a table. This has allowed me to be able to do a selection on existing entries on the table and also "allow" adding new data using the same combobox.
thanks.
I've got a little problem with passing the value of a combobox to a query which I would like to use to populate another combobox.
There is a large table containing the columns: AutoID, Projectname, Projecttype and some more which I want to filter for certain records.
The first combobox is populated by a different table containing: AutoID and Projecttype (I use this already to create the records in the previous table). This combobox is bound to the first column (AutoID) but I display the second one. What I want
to do is to choose a "Projecttype" and populate the second combobox with all the corresponding records and display the "Projectname" for further processing.
I already read that it's not possible to use a combobox selection directly in a query and you have to go for a public function. I created this function (using the listindex to get the AutoID from the corresponding Projecttype) but can't pass it to the query (don't get any results although the value is correct!
Is there a "better" way to filter records (based on combobox selection) and populate a second combobox?
Thank you in advance!
Moritz
Why do you have AutoID and Projecttype in both tables? Looks like you could possibly do with some normalisation here.
To do what you want with the combo boxes, first of all define a global variable in the module where you've put your public function (I'll call it glngAutoID). The public function should just return the value of this variable, and then you can use this function in the query for the second combo box. In the AfterUpdate event of the first combo box put
glngAutoID = combo1
combo2.Requery
You shouldn't need to use the ListIndex property to get the AutoID because you've bound the combo box to the first column.
I want to populate a DataGridView Grid in my Form with Data from SQL Query or from some Table form Database. I want to know various possible ways to populate the DataGridView Grid.
Thanks in advance.
There are basically 3 ways to display data in a DataGridView
Create the rows manually in a loop: it's very inefficient if you have a lot of data
Use the DataGridView's virtual mode: the DGV only creates as many rows as can be displayed, and
dynamically changes their contents when the user scrolls. You need
to handle the CellValueNeeded event to provide the required data to
the DGV
Use databinding: that's by far the easiest way. You just fill a
DataTable with the data from the database using a DbDataAdapter, and
you assign this DataTable to the DGV's DataSource property. The DGV
can automatically create the columns (AutoGenerateColumns = true),
or you can create them manually (you must set the DataPropertyName
of the column to the name of the field you want to display). In
databound mode, the DGV works like in virtual mode except that it
takes care of fetching the data from the datasource, so you don't
have anything to do. It's very efficient even for a large number of
rows
Link.
What's the best practise way to handle a primary key in an unbound datagrid view combo-box column?
I want to display a meaningful value but store only the primary key (much like Access combo-boxes). I achieve this with a regular combo-box by adding an object with two properties into the items collection rather than a plain string. I then retrieve this by casting the selected index value of the combo-box back into it's object form and then retrieving the properties. This works very well but I am unable to replicate the technique with a datagridview combo as I can't seem to access the items collection. I only seem to be able to retrieve a string value back from the grid although I can add the object as normal when creating the column.
Hope this makes sense...
RESOLVED - Set the combo datasource to a collection class and use the ValueMember and DisplayMember properties in the same way as when binding to a datasource. Works a treat.