How to programmatically select a value in combo-box? - ms-access

Good Day all. I have a quick question. I have an openargs value that I'm am trying to use to show up in my combobox (cmbMemberName) on return from another form. The combobox populate the underlining subform. I just can't seem to get the right method. I can't use recordsource cause that would filter out the rest of the records. Rem: I just want the focus on the updated record and loaded into the combobox upon return. Here's the last method I tried.
If Nz(Me.OpenArgs) <> 0 Then
Me.cmbMemberName.SetFocus
DoCmd.FindRecord Me.OpenArgs
MsgBox (Me.OpenArgs)
Me!cmbMemberName.Dropdown
Else
....
The error occurs on the DoCmd. Any suggestions. Thanks. I could load the entire sequence, but don't think that would be necessary.

First you have to set the value of the combo box. Assuming your openargs matches the bound column of the combo box that should simply be
Me.cmbMemberName = Me.OpenArgs
After that you need to get you subform to populate based on the combo box value. Assuming you have set the subform to properly read the value you just need to requery it
Me.MySubForm.requery

Related

Force Combo Box List Update with VBA

I am working on an MS Access Form. I am trying to have a Combo Box on the form who's list is narrowed based on information earlier in the form. I have a Query saved in the database that gets the list properly.
The problem I am having is that the query is not called after each update of the form so the Combo Box's list is always what it was when the form was opened.
I have tried to force an update using Me.MyComboBox.Requery in the AfterUpdate event of the previous control but this only requeries the record source of theform, not the row source of the Combo Box.
I have also tried changing the row source property to force a requery but that did not work eithier:
Private Sub PreviusControl_AfterUpdate()
Me.MyComboBox.RowSourceType = "Table/Query"
Me.MyComboBox.RowSource = "qryDynamicComboSource"
End Sub
Thanks in advance!
It looks like everything you're doing is correct. Check your query design if it's actually changing based on your parameters.

Access VBA restore listbox selection after requery based on a non-bound column

So, I inherited MS Access 2010 (and 2013 now) code that has a listbox bound to a column that is not unique. However, that bound value is used in code, so, I cannot change which column is bound. But, I have a subform that needs to requery the listbox on the parent form. What I am struggling with is how to restore the selection in the parent's listbox to what item was selected before the listbox was requeried.
I have tried storing the Parent.List0 value before the requery and then using Parent.List0.Selected and Parent.List0.ItemData, but these do not seem to work because, presumably, the listbox is bound to a column that is not unique.
There is a column in the listbox that is unique. The 4th column (column(3)) is unique, but I'm not finding a way to select a listbox item based on a column other than the bound column.
I am confident someone has some ideas that will sort this out, but I've not been able to locate any solutions in my search of the web.
I am in version 2010 and while testing, I was unable to replicate a situation where the list box loses its value. Neither on a bound form or an unbound form.
In any case however, it seems that column 3 has a unique key - so you can use that.
' Assuming this code runs from within the sub-form
KeyID = Me.Parent.Controls("MyListBox").Column(3)
With Me.Parent.Controls("MyListBox")
.Requery ' Or do whatever you do that causes it to requery
' Find the previously selected item and select
For i = 0 To (.ListCount - 1)
If .Column(3, i) = KeyID Then
.Selected(i) = True
Exit For
End If
Next
End With
That should do the trick.

Refresh query-driven combo box values when field value changes?

I have a combo box on an form where the values are populated based on the value in a separate field.
To do this, I have created a combo box and set the "Row Source" to run a SQL statement.
The problem I am having is that if the data in the field changes, the combo box values do not update.
How do I get access to re-run the query?
See whether this description is reasonably close to your situation.
My form has a text box, txtFoo, and a combo box, cboBar.
The row source property for cboBar is a query which references txtFoo. And I want the combo's contents updated in response to changes in txtFoo. The solution is to requery cboBar from txtFoo's after update event.
Private Sub txtFoo_AfterUpdate()
Me.cboBar.Requery
End Sub
I have had issues in the past with Requery not working or even hanging.
It's not pretty but you might want to try this:
Me.cboBar.RowSource = ""
Me.cboDemoUnit.RowSource = "your SQL statement"
Here is an example of what I have done recently to do the same exact thing, this should help! Its in an afterupdate sub.Example of dynamic combobox

How to set the value of an unbound textbox with a default value to an empty string with Access VBA

I have a database with information about visitors to our department. Here is my setup: A user selects a visitor name from a listbox. Access assigns the values from that visitor's record to temporary variables and opens an unbound form where those variables are the default values in the text field. When a user changes a value in a textbox, an After_Update event uses SQL to update the corresponding field in that visitor's record and assigns the new value to the temporary variable.
After_Update example:
Dim strRUN As String
strRUN = updateVisitorOnDirty(Me.txtVisitorTravelMethod, "VisitorTravelMethod", 1)
HideWarnings (strRUN)
TempVars!strVisitorTravelMethod = Nz(Me.txtVisitorTravelMethod.Value, "")
"updateVisitorOnDirty" is a function that returns SQL to update a field (specified in second argument) in the table with visitor information with the first argument. (The number at the end is says it's a string.) "HideWarnings" turns warnings off, executes the SQL, and then turns warnings back on.
My problem:
When a user clears a textbox, the After_Update event makes the textbox revert to it's previous value.
I thought at first that maybe the unbound textbox was reverting to its default value when it was cleared, but I put a breakpoint in the sub that fires during After_Update:
If I change a value in mytextbox from a to b, debug.print shows the value of mytextbox as b. If I change a value in mytextbox from a to (blank, or empty string), debug.print shows the value of mytextbox to still equal a.
My workaround has been to include a little "x" near the textbox that the user can click to 1)clear the textbox, 2)update the temporary variable, and 3)update the table. It works fine, but it seems like there should be a better way. Help?
Please let me know if you need any other information or if I should otherwise re-word this. I come here all the time for answers, but this is the first time I've actually submitted my own question. Thank you!
(I edited this to make the code show up correctly...didn't leave a line before.)
I figured out that the unbound textboxes were reverting to their default value when a user tried to make them blank. To stop them from doing this, I put this in the AfterUpdate of the textboxes:
Call ResetDefault(Me.textbox1.Text, Me.textbox1)
And defined the function:
Public Sub ResetDefault(ByVal strChange As String, ByRef txtCurrent As TextBox)
If Nz(strChange, "") = "" Then txtCurrent.DefaultValue = ""
End Sub
That is, if a user cleared a textbox, the default value of that textbox was set to "", allowing the textbox to actually go blank.
I think the best approach is to scrap the after_update event on your text boxes and instead either put a save button on your unbound form so that all updates are written in one pass, or capture the forms closing event and update your table then.
This is the advantage of using unbound forms, you do not have to comit data until you are absolutely ready to, but with using an after_update event you are almost mimic-ing the behaviour of a bound form.

Dropdown in Access 2007 parameter query

I want a Access parameter query to ask an user for a value (a location in this case). When I type [Enter location] in the Criteria field it works fine: I get a dialog box (Enter Parameter Value) with a textbox and my text (Enter Location). So far, so good. This works (the result also).
But now I want a dropdown/combobox (instead of a textbox ) for the user to pick a location. I made a form and type Forms![Form1]![CmbLocation] in the Criteria field.
Like this: http://office.microsoft.com/en-us/access/HA011170771033.aspx
But I still get a textbox (with the reference as textlabel).
What am I doing wrong? Has anybody any advice?
In addition to Albert's suggestion, you might want to make this work within the query itself, so that it's "bootstrappable." To do that, you'd have to write function that returns the value chosen in the combo box on the form. It would be something like this:
Public Function ReturnMyCriterion() As Variant
DoCmd.OpenForm "dlgGetCriterion", , , , , acDialog
With Forms!dlgGetCriterion
If .Tag <> "Cancel" Then
ReturnMyCriterion = Nz(!cmbMyCombo, "*")
End If
Else
ReturnMyCriterion = "*"
End With
Close acForm, "dlgGetCriterion"
End Function
(when opening a form with the acDialog switch, the code pauses as long as the form is open or visible; to get the value out of the combo box, you have to set the form's .Visible property to False. You could do this in the AfterUpdate event of the combo box, or in the OK button. You'd also want a Cancel button that set's the form's .Tag property to "Cancel" and then sets the form's .Visible property to False; this is all relatively a standard approach to working with dialog forms in Access).
You'd then make the criterion in your query be:
Like ReturnMyCriterion()
That is, assuming you want to return all records if no value is chosen in the combo box.
If you removed parameter form your query, and then re-typed in the above form exprsison into the query builder, then it should work.
So, in the query builder, in the criteria section just type in
[forms]![form1]![Combo4]
Make sure you have the right form name and control name of the combo box.
You should not need to type in anything else into the query builder. As mentoned, remove the old parameter prompt you previous had in the query builder.
Now, open the form, select the combo box, and now try opening the query, it should open without any prompts. Note this approach means that the form will have to be open, and the combo box will have be selected a value BEFORE you attempt to launch the query. So, if you basing a report on this query, then palce the button to launch the report on the same form as the one with combo box. This quite much ensures that the form will be open before you attempt to launch a query or report that is based on that query