MS Access Force Update on Combobox - vba

How do I force an update when the ComboBox value is changed in code. Below is piece of code I have tried but does not seem to work
If (Not Mid(sCode, 1, 2) = ddlLevelID1) Then
ddlLevelID1 = Mid(sCode, 1, 2) 'force change/force AFTER_UPDATE event to run.
End If

Assuming ddlLevelID1 is the ComboBox:
ddlLevelID1.value = foo
will change the value. I do not believe you can link a value displayed in a ComboBox to a variable value without pushing changes up to the userform after the value is changed.
Regarding the AfterUpdate method, from msdn:
Changing data in a control by using Visual Basic or a macro containing
the SetValue action doesn't trigger these events for the control.
However, if you then move to another record or save the record, the
form's AfterUpdate event does occur.
http://msdn.microsoft.com/en-us/library/office/bb238392(v=office.12).aspx

Related

Microsoft Access VBA - Locking a field when value in the field is not null

I have fallen into a rut building a quote creation tool. I have a combo box that selects an item for the quote and will populate all of the fields for that line. In this case, I want the [Cost] field to be locked, preventing any changes. However, there are times when a user must type an item for the quote that is not in the combo box; therefore, all of the fields on that line will be null and must be typed manually.
I am trying to write a VBA event where [Cost] is locked when [Cost] is not null, preventing any change; and where [Cost] is not locked when [Cost] is Null, allowing editing.
Private Sub Form_Current()
If IsNull(Me.Cost) = True Then
Me.Cost.Locked = False
Else
Me.Cost.Locked = True
End If
End Sub
The top line is in the combo box and the entire line is populated; Here I want cost to be locked
The second line is not in the combo and the line must be manually entered; Here I want lock to be disabled
Sometimes code must be in multiple events to achieve desired result. Since you will programmatically populate Cost if an item is selected from combobox list, put code in form Current event and combobox AfterUpdate event. Could have a Sub behind form that can be called by both events.
Programmatically setting control's properties will affect ALL instances of control. An alternative is to use Conditional Formatting to dynamically enable/disable a textbox or combobox.
To allow user option to edit manually entered Cost value for a new record adds complication. VBA or Conditional Formatting rule could be conditional - don't disable if focus is on a new record and service item is not in lookup table.

Setting focus to another control prevents current control value from changing in VB.NET

I have a ComboBox in a WinForms app written in VB.NET. In the .SelectionChangeCommitted event I want to change the focus to a different specific control to assist user workflow. However when I do that, the change is not saved on the initial ComboBox and the value & index are reverted to the original values.
I've used both myControl.Focus and myControl.Select
The Combobox is setup like this:
With ChoosePartType
.DisplayMember = "PartName"
.DataSource = GetTable(qry) 'This custom function returns a DataTable with fields PartNum and PartName
.ValueMember = "PartNum"
.SelectedIndex = -1
End With
I assume something in the changing focus is short-circuiting the property change. Is there a way to force that to happen before I change focus?
Note: seems like a different issue from WInforms Combobox SelectionChangeCommitted event doesn't always change SelectedValue
Similar to this but I don't use databindings: Combobox DataBinding Bug - Won't write value if programmatically losing focus
Try using:
ChoosePartType.SelectedItem.Row("PartNum")
Assuming that this is the first column of the control's datasource, this could be also written as:
ChoosePartType.SelectedItem.Row(0)

Access Checkbox value update does not trigger AfterUpdate event

I could not find an answer to my specific question, but I am also relatively new to VBA, so maybe I just didn't look for the right terminology.
What I have, is a form that contains a bunch of comboboxes. These are arranged in rows and columns like so:
cboThingOne1 cboThingTwo1 cboThingThree1
cboThingOne2 cboThingTwo2 cboThingThree2
... ... ...
cboThingOne15 cboThingTwo15 cboThingThree15
I have set cboThingOne to show a selection of items (e.g. department1, department2, department3,...) from an SQL database.
cboThingTwo and cboThingThree are also set to a certain Rowsource (containing things like Apple, Bananas and Cherries).
What I would like to happen is, as soon as I change the value in cboThingOne1 to department3, to fill all of cboThingOne checkboxes with the same value.
This is working. I am using the AfterUpdate event of each cbo, to call a function, which iterates over all items in Me.Controls, checks their name + 1 (in this case cboThingOne2) and sets the value of this control to department3.
Private Sub fun_fill_cbo_with_value(FieldName As String, FieldValue As String)
Dim i as Integer
'This function returns the name without number
my_fieldname = Striptext(FieldName)
'gets me the number of the field I am working with (i.e. cboThingOne1 --> 1)
i = Int(Replace(FieldName, my_fieldname, ""))
i = i+1
cbo_name = my_fieldname + CStr(i)
For Each my_control In Me.Controls
If my_control.Name = my_fieldname Then
my_control.Value = FieldValue
End If
Next my_control
End Sub
When the value for cboThingOne2 is changed, I expected the AfterUpdate event of this cbo to be triggered, which apparently does not happen.
In this AfterUpdate event the Rowsource for cboThingTwo2 should be updated.
Private Sub cboThingOne2_AfterUpdate()
'To update the cbo in the row below
Call fun_fill_cbo_with_value(cboThingOne2.Name, cbo_ThingOne2.value)
Me.cboThingTwo2.RowSource = "somedifferentqueryhere"
Me.cboThingThree2.Rowsource = "yetanotherquery"
What I expected, was that updating the value in fun_fill_cbo_with_value would update the value of the cbo + 1 (which works) and also triggers the AfterUpdate event (which does not happen).
Furthermore, because I am calling fun_fill_cbo_with_value in the AfterUpdate event, I expected the column to fill to the end, rather than just the cbo below (as it does right now).
I can fill the entire column of cbos, by adding an additional loop in fun_fill_cbo_with_value which just goes from 1 to 15.
That still does not help me with updating the rowsource of cboThingTwo and cboThingThree.
I hope you can help and if you need more information, I am glad to provide it.
I expected the AfterUpdate event of this cbo to be triggered, which
apparently does not happen.
It is triggered by a user action only.
Call the AfterUpdate event function itself from your code (bad practice); or (allows for better organised code) let the AfterUpdate event call a separate function which your other code can call as well.

How do I get the updated value of a textbox in VBA Access in a custom Sub?

This is for VBA Access 2003
I've got a textbox I want to use as a filter for a list box rowsource command. I also have a checkbox which adds another filter for the same rowsource command. I've only programmed in C# and I'm trying to write a single Sub which will simply set the RowSource regardless of if my textbox filter is changed or if my checkbox filter is changed. However, my textbox is giving me problems.
If my checkbox filter changes and I run my method the textbox.Text throws an error saying that it must have focus - Text is null. If I do a null check on that property it throws an error saying the control must have focus.
I've used the .Value property, but for whatever reason it doesn't update to the newer values.
My current attempt:
If Me.txtClientFilter.Text = Null Then ' Error 2185
filter = Me.txtClientFilter.Value
Else
filter = Me.txtClientFilter.Text
End If
Should I
Manually add focus then remove it everytime I want to check a
control?
Duplicate my code in each control's event Sub?
Manually set
the .Value property when the change happens?
I fixed my problem with some code which I'll show below. I don't know what was happening behind the scenes, but the .Value was not getting updated with the .Text value. I decided to set it explicitly, which then caused the entire text box value to be selected.
I ended up with the following code which explicitly sets the .Value of the control and also reset the cursor to the end of the text in the control. Thanks to some guy named Brent Spalding here for the cursor code.
Private Sub txtClientFilter_Change()
Me.txtClientFilter.Value = Me.txtClientFilter.Text
ProcessFilter
txtClientFilter.SelStart = Len(Me.txtClientFilter.Text)
txtClientFilter.SelLength = 0
End Sub

Removing Item from Combobox without Firing Change Event

I am creating a userform in Word VBA that only has one combobox named cboType and one command button named cmdNext. In default, cboType have Null value and cmdNext is disabled.
I use cboType_Change event so that when an item is selected from the combobox, cmdNext is enabled and the combobox value is stored to a variable Type. However, I have another sub that will remove each item in cboType when cmdNext is clicked. I find out that removing item also fire the cboType_Change. I don't want this to happen because it will also change the value in variable Type.
In order to overcome this, I have tried using cboType_AfterUpdate event instead of cboType_Change, but I found out that this event will be fired only if the focus moved from cboType (cmiiw). Meanwhile, I don't have any other active/enabled control in the userform, so the cboType_AfterUpdate event won't be fired (the combobox value won't be stored to variable Type and cmdNext won't be enabled).
Any suggestion on how to do this?
If you just want the function to be called, can't you use
Call cboType_AfterUpdate()
to call this manually when the user clicks the cmdNext button?
Not sure if this solves your initial problem that cboType_Change is called when you change some value in the combobox.