Access VBA Combobox Store Value in Column - vba

I have an MS ACCESS Combo Box and I wish to change the value of one of the columns in a particular row. I get error "object required" when I run this line:
Me.ComboName.Column(12, intUseRow) = myVar
(If I am unable to use the above syntax then you should also know that the row I am trying to change is always going to be the "current" visible row so there may be another way of solving the problem due to this fact).
Thanks!

If you have a recordset that is bound to a Table/Query, you will need to change the underlying data then requery the combobox to see changes.
If you load it manually (like in the form load event) and have the comboBox Row Source Type to "Value List" - you should be able to update it like this:
Copy all the data from the selected row into variables.
Combobox.RemoveItem (selected index)
change the required variable to the new value.
construct the semicolon separated string for the value list entry
combobox.AddItem new-string.
a bit messy, but it works correctly!

Related

How to reference tables/queries/reports in access using an alias name?

In my Access database, I have tables with the naming convention of "t_customers" or "t_orders". I have a combo box which lets me pick from these options. For a better look, I want to have the options in the combo box without the "t_", so one of the options is just "customers". Then in VBA, I can access the combo box's value of "customers" and then pass that as a parameter to my function which will export the contents of the associated table "t_customers" to an excel file.
Is this possible to do? I can think of using a select case statement and for each combo box value I manually assign the related table value, but there has to be a better way. Is there some sort of short cut that does this in Access using a relationship or similar feature without coding? Or is there a simpler way to code this?
EDIT:
I am also trying to get the value of the current item selected in my combo box named "cbTable". I have no macros attached to the combo box. I have a button that when pressed it runs a macro which calls my VBA function. In the function I have this code:
Dim cbValue As String
cbValue = cbTable.Value
MsgBox (cbValue)
I get this error:
Runtime Error '424'
Object Required
The error is on the
cbValue = cbTable.Value
line. What is wrong with this code and how do I get the current value of the combo box?
Thanks in advance.
You can set your RowSource for your combobox to
SELECT MSysObjects.Name, Replace(MSysObjects.Name,"t_","") AS Expr1
FROM MSysObjects
WHERE (((MSysObjects.Type)=1) AND ((MSysObjects.Flags)=0));
Then set the Column Count to 2, the Column Widths to 0;2 (the second number just needs to be any positive number) an the Bound Column to 1.
Now when you go to call your function the value of the combobox will be the real name of the table, t-customers, but what you saw on your form was customers.
As for accessing that combobox a few things need to be in place.
Let's say you have a form, Form1, and your combobox, cbTable, is on that form.
So when your form runs it looks like this
Now let's say you have an OnChange even for that combobox
Notice that when you type cbTable it appears in the intellisense (ctrl+space). This tells you that your object is accessible at this scope.
Now if you were in another module where that variable is out of scope you would get this error when you try to compile.
Because Module1 has no idea what cbTable is. However, you can reference anything so long as it's fully qualified. So if we run this test with your form open, and "orders" selected all 3 of these lines are equivalent
Forms("Form1").cbTable
Form_Form1!cbTable
Forms("Form1").Controls("cbTable")
Notice at the top of all my modules it says Option Explicit. This forces your code to basically be checked for syntax/scope validity before you run it. Tools > Options

Getting value of previous selected item (dropDown)

When a user changes the selected item on a dropdown I need to get the PREVIOUS item selected,
EX:
dropdown items:
1) Questions
2) Jobs
3) Tags
4) Badges
User has #2 Selected and then changes to #4 -- How can I get the value of #2 when they change the selection?
Declare an instance variable in your form (WinForms) or window (WPF).
When a user selects an item:
Do what you want to do.
Save the current item index in the instance variable.
In step 1, you can now access the instance variable to get the previously selected item.
Declare a global variable that will contain the previous value.
When the user changes the selection in the combobox, set the variable to the currently selected value. Allow the selection to be changed. You now will have the previous value.
If you need to have the history of changes, then the global variable would be a collection. Then on changed event, add the current selection to the collection.
If your control is bound to data, there is no need to "Squirrel" the old value away, your data provider usually does this for you.
For example, if you are bound to a DataRow, this code will get the previous value.
? = [Your DataRow].item("[Your column name]",OrigialVersion)
This varies based on your data but ultimately, you could always re-query the database to get the original value as well.
Regardless of what you are bound too, if you ask the datasource for it's value during the Validating event of the control, it will have not changed yet so it will give you the old value, which you can then compare against the current selection.
Lastly, if you are not bound to data, I typically store the old value in the TAG property on the GotFocus event of the control. Then you can compare against that.
Hope this provides some other options that might help you, depending on your case.

BIRT result set values in specific cells

My query returns location_cd(string) and item_count(int). I only need certain rows from the result however and I need them to display in specific places in my layout so I don't think the table solution is going to work. Can I determine where I place the value for a particular row of the result set?
I am using a grid to display values for a number of fields. I cannot seem to be able to get the values from the results to show. The grid is bound to the result set. I even tried binding the cells to the result set but that didn't work either.
I checked in the query editor and there is a result set shown in the Preview so I know the query works. The complete and correct result set shows if I put a table on the page.
I tried inserting a Dynamic Text or Data object in a cell and used the expression:
dataSetRow["location_cd"]=="3SD"?dataSetRow["item_count"]:""
This returns a blank and does not seem to evaluate. I tested it with :
dataSetRow["location_cd"]=="3SD"?dataSetRow["item_count"]:"BLANK"
and got 'BLANK' to appear in that cell.
dataSetRow["location_cd"] and dataSetRow["item_count"] will display the location_cd and item_count from the first row of the result set. row.outer[] did the same thing. Obviously I am just hacking at this report at this point.
A co-worker suggested that she uses a JAVA if-statement in places like this but I could not get that to work either.
Any ideas or suggestions that will get me on the right road??
Thanks
An elegant option would be to use a HashMap storing the result of the dataset.
Declare a report variable named "values", with a new hashmap as default value (see image below)
Fill values in the onFetch script of the dataset: vars["values"].put(row["location_cd"],row["item_count"]);
Insert new data elements at any place of the report with expressions such: vars["values"].get("myFavoriteLocationCD");
Though it is important to note the dataset should be triggered by the report before these data elements.
The particular row you want to display you specify in a "Text" field inside your grid. Just drag and drop a "Text" field inside your grid. If you bound the fields you want to display to your grid, the "Text" field inside the grid inherits the bindings of its parent (the grid), so you can access the bindings automatically in the "Text" field.
You could try following steps, maybe that works.
Don't use "Dynamic Text" field, instead use a regular "Text" field
Ensure the fields of your query which you use, are bound to the grid (you sayed you already did)
Open the "Text" field
Change preselected pull-down entry "Auto" into "HTML"
Change preselected pull-down entry "Formatting" into "Dynamic Text"
Wrap your code in <value-of format="HTML"> your code goes here... </value-of>
Note: You should check in the "Expression Builder" of your "Text" field if you are able to access the fields you bound to the grid. If they are not available sth. went wrong with your binding. Avoid binding query records to cells this will drive you crazy.
If you want to display a list, ensure you didn't set a constant height in the row of your grid. Set the height to 100% than the row takes its height dynamically.
What about the idea to optimize your query, that only get the results you want are displayed, than you don’t need to filter them with java script? If you don’t need the filtered results in another place this would be the cleaner solution in my opinion.

When adding a checkbox to a datagridview datatable I get dataerrors

I have a datagridview that I've added a checkbox column to. When I preview my data without the checkbox it works fine. when I run the program and try to load the datatable, I get a data error. I'm not sure what may be causing this. Where can I look to figure out what's causing this error?
Thank you
Doug
Your e.Context returns as Display. If you take a look at MSDN you will see for display it says
A data error occurred when displaying
a cell that was populated by a data
source. This value indicates that the
value from the data source cannot be
displayed by the cell, or a mapping
that translates the value from the
data source to the cell is missing.
In other words the checkbox column in the dgv probably doesn't quite know how to display the information you are giving it. So, chances are you are telling the checkboxes whether they should be true or false incorrectly.
Update: Perhaps, your solution lies with the TrueValue and FalseValue properties of DataGridViewCheckBoxColumn Class. These properties define when a checkbox should be checked or not checked. If you don't have these set, or if you are trying to set your checkboxes to something other than either of the two values in these properties, you would have problems.

VB in Access: Combo Box Values are not visible in form view but are visible through Debug.Print

Code in Form onLoad:
country_combo.RowSourceType = "Value List"
Code in a reset function:
Dim lListIndex As Long
With Me.country_combo
For lListIndex = .ListCount - 1 To 0 Step -1
.RemoveItem (lListIndex)
Next lListIndex<br/>
End With
Code to populate country combo:
*For n = 1 To numCountries*
*countryCombo.AddItem (countryRS.Fields("countryName"))*
*countryRS.MoveNext*
*Next n*
I'm having a problem that occurs AFTER the code to populate the country combobox runs. The values are there as I can run Debug.Print(countryCombo.Value) and it prints out the name of the selected country, but I can't see the values in the combobox at all. They're invisible, and as far as I know there is no visiblity property for specific items, unless I'm completely mistaken.
comboBoxError.png http://img110.imageshack.us/my.php?image=comboboxerror.png
I think you should probably use Access's GUI tools to do what you're looking for. In design mode, click on the field you are trying to populate, then click the "lookup" tab. You can then specify a table to populate the field with and your forms should automaticly update as well.
I've also seen what you describe here - as far as I can tell, it's a bug within Access (I was using 2007) that only occurs when you programatically mess with the contents of a combo box. It does not happen every time. The issue corrects itself if you highlight the text that is in the combo box.
I am experiencing a similar issue with Access 2003. Based on the selection of one combo box, the row source of a listbox is set to an SQL string Basically a SELECT DISTINCT [MyField_Selected] FROM MyTable. For some fields the values are visible in the list box and others it is not. The values are there however as I can access them via code. To make it more interesting it works fine in Access 2007.
Just found the resolution on another forum. Check the format property of the field(s) in question on the table. In my case, when Access 2007 created the table, it put an # format in there. I removed that and all works great!