Enabling field visibility from combo box selection in Access 2007 - ms-access

I have a form in Microsoft Access 2007 called System, and a combo box called Utility within this form. Below this is yet another combo box called Utility_FOO, and I have disabled its visibility by default. One of the options in Utilities is a checkbox labeled 'FOO.' I want Utility_FOO to become visible whenever FOO is selected.
I have tried creating a subroutine in Visual Basic that checks whether or not FOO is selected every time I select an item from the list (using onUpdate), but I cannot figure out how to check that specific entry. Is there a simple way to do this?
Thanks!

If your combo box is bound to a multi-valued field, examine its .Value property to determine whether FOO is among the selected (checked) items.
Private Sub Utility_AfterUpdate()
Call SetVisible
End Sub
Private Sub SetVisible()
Dim varItm As Variant
Dim blnVisible as Boolean
blnVisible = False
If Not IsNull(Me.Utility.Value) Then
For Each varItm In Me.Utility.Value
If varItm = "FOO" Then
blnVisible = True
Exit For
End If
Next varItm
End If
Me.Utility_FOO.Visible = blnVisible
End Sub
You may also want to do the same thing for the form's On Current event. If so, add this:
Private Sub Form_Current()
Call SetVisible
End Sub

Related

Cannot catch any events of an ActiveX Image Combo Control in VBA Access

I am using an ActiveX Image Combo Control in my VBA Access application and I have been having a lot of issues with it. There is little to no documentation on it. I am unable to catch any events from the control. I tried using the "on change, on get focus, and on exit events" and none of them work. The events fire when the form is initialized but that is it. Does anyone know what is up with this control? I am debating as to whether I should cut my losses and use a normal combo box without any images.
The full name of the ActiveX Control is "Microsoft ImageComboBox Control, version 6.0"
Here is my debugging code
Private Sub ImageCombo8_Change()
Debug.Print "change"
End Sub
Private Sub ImageCombo8_Enter()
Debug.Print "entered"
End Sub
Private Sub ImageCombo8_Exit(Cancel As Integer)
Debug.Print "exit"
End Sub
Private Sub ImageCombo8_GotFocus()
Debug.Print "focused"
End Sub
On initialization, I get these ouputs
entered
focused
exit
change
change
And nothing happens when I actually do anything to the control.
Setting up the control with items:
Private Sub Form_Load()
Dim objNewItem As ComboItem
Set objNewItem = ImageCombo8.ComboItems.Add(1, , "Option 1", "pic1key")
objNewItem.Indentation = 1
Set objNewItem = ImageCombo8.ComboItems.Add(2, , "Option 2", "pic2key")
objNewItem.Indentation = 1
End Sub
I found the GotFocus event only triggered when tabbing into the control, not mouse click. Only the Change event triggers during form load, it does not trigger when item selected, nor does Updated. Enter, Exit, GotFocus, LostFocus all trigger appropriately. Also, the Click event does trigger when an item in the list is clicked.
Here is my working code:
Private Sub Form_Load()
Dim objNewItem As ComboItem, x As Integer
For x = 1 to 2
Set objNewItem = ImageCombo8.ComboItems.Add(x, , "Option " & x, "pic" & x & "key")
objNewItem.Indentation = 1
Next
End Sub
Private Sub ImageCombo8_Click()
MsgBox Me.ImageCombo8.SelectedItem.Text
End Sub
Even though the ControlSource property shows available on property sheet, it errors if bound to a field. The Value property also fails in code. Use Text property to capture info from the selected item and code to save data to field.
Took forever to figure out how to set up the ImageList and ImageCombo controls. Not a lot of info out there on these critters and most of it involves non-Access programs. Finally discovered that a double click on control in design view opened a dialog box and then found clicking the ellipsis on Custom property does the same.

Form: clear the filter combo if user remove the filter

I created a continuous form, with a combo box in the header to allow user to easily apply one of the predefined filters.
If the user sets a filter and then clicks on the built-in Filtered rectangle at the bottom of the form to remove the filter, I'd like to empty the combo.
Since the Filter event does not fire in that case, and the ApplyFilter event runs BEFORE the FilterOn is modified, I cannot rely on them.
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
Debug.Print Now, "Apply", Me.FilterOn, Me.Filter 'returns the OLD values :-(
End Sub
Any suggestion to solve this puzzle ? I think I carefully went through every form event, without success.
Otherwise I will have to hide the built in navigation buttons and I like to keep as much as possible of the built-in tools.
You can just check the ApplyType in the ApplyFilter event (0 = filter cleared, 1 = filter applied or changed).
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
If ApplyType = 0 Then
'Clear that combobox
End If
End Sub

MS Access - field_AfterUpdate

I'm trying to lock a separate combobox on a form when another one is filled. Below is the code, however it's not doing anything and I'm unsure why.
Private Sub fieldApplicationType_AfterUpdate()
If Me.fieldApplicationType.Value Like "Application" Then
Me.fieldPrimaryBusinessFunction.Enabled = True
Else:
Me.fieldPrimaryBusinessFunction.Enabled = False
End Sub
The value being displayed in the form is the "name" field on the table. Basically if the name contains "Application" I want to disable the other combobox so it can't be used.
Bonus Points on how do I get this to combobox to stay disabled if the form already has the fieldApplicationType selected as NOT Application?
The Update does not seem to be called and maybe it's due to the following?
There is a Form_Current() which calls a module. I didn't design this code but guessing that Module is taking precident over the form Events?
Private Sub Form_Current()
' Temporarily turn off screen refreshes while we update widget states.
' For some reason, if this isn't done MS Access doesn't consistently repaint all controls
application.Echo False
Call UpdateManagementFormReadonlyState(Me, _
Me.lblReadOnlyStatusIndicator, _
hasEditPermissionsForManagementFields(Me.recordOwnerId.Value))
' Re-enable normal GUI updates
application.Echo True
End Sub

disable cells based on another cell

In Access 07 on a form: I need to disable two cells based on a dropdown. Another words, if the user picks "new" from the dropdown, two other cells get disabled. I beleive I will need to right-click the dropdown, build code, but I don't know the IF... script I need to use.
Say your form includes a combo box named cboMyField which is bound to a field named MyField in the form's record source, and two text boxes: txtField2 bound to Field2; and txtField3 bound to Field3.
You could check the combo's value and set the Enabled property of the two text boxes appropriately by calling a procedure in your form's code module.
Private Sub SetButtonStatus()
If Me.cboMyField = "new" Then
Me.txtField2.Enabled = False
Me.txtField3.Enabled = False
Else
Me.txtField2.Enabled = True
Me.txtField3.Enabled = True
End If
End Sub
Call that procedure from the form's On Current event so the text box status will be set as you navigate between rows.
Private Sub Form_Current()
SetButtonStatus
End Sub
Do the same for cboMyField's After Update event so the text box status will be updated based on a user change for cboMyField.
Private Sub cboMyField_AfterUpdate()
SetButtonStatus
End Sub
Edit, Trouble-shooting: Since you're using Access 2007. Put the database in a trusted location and run it from there. See Get started with Access 2007 security
Edit2:
Change the SetButtonStatus procedure temporarily to check whether it even runs, and how it sees the combo box value if it does run.
Private Sub SetButtonStatus()
MsgBox "SetButtonStatus"
MsgBox "Value of cboMyField is '" & Me.cboMyField & "'"
If Me.cboMyField = "new" Then
Me.txtField2.Enabled = False
Me.txtField3.Enabled = False
Else
Me.txtField2.Enabled = True
Me.txtField3.Enabled = True
End If
End Sub
Also add Option Explicit to the Declarations section (at the top) of your module. Then select Debug->Compile from the VB editor's main menu. That effort should tell us whether the code includes any names which VBA doesn't recognize.
Edit3: From private communication, the combo's bound field was numeric data type, so could never be equal to "new". Therefore every time the procedure ran, it set text box Enabled property to True.

Access 2010 VBA ControlSource Loop

I have multiple fields on a form that have no values associated with them until data is entered into that field. At which point those controls have the #Name? value. All I'm trying to do is hide the fields using a loop as opposed to writing If statements. Here is the code working with a single If statement to hide said field/fields.
If Not Me.txtField.ControlSource = "#Name?" Then
Me.txtField.ColumnHidden = True
End If
I can't seem to figure out how to hide multiple controls that have the #Name? flag. Any help would be appreciated.
Dim ctl As Control
Dim ctlError As String
ctlError = "#Name?"
For Each ctl In Me.Controls
If Not ctl.ItemsSelected(txtField) = ctlError Then
ctl.ColumnHidden = True
End If
Next ctl
EDIT:
On the main form there is a subform with a cross tab query. But the problem is results on the query are populated from a combobox. So I've added fields that are not yet available in the query to the subform and end up with #Name? Attached is a screenshot to better illustrate the issue.
There is a 90% chance I'm going about this process wrong, so it's a learning process right now.
It would be easier to address this question if you described the specific errors that are raised by your code. This answer assumes that the control in question is a TextBox.
"#Name?" means that a control's ControlSource refers to a field that is not in the form's RecordSource. For example, if your query has two fields (ID and BirthDate, for example) and your form has three controls, with ControlSource of "ID", "BirthDate", and "BirthCountry", then the control whose RecordSource is "BirthCountry" will show "#Name?". However, its Value is not "#Name?". Rather, calling the Value property raises error 2424 (with the rather unhelpful message "The expression you entered has a field, control, or property name that Microsoft Office Access can't find").
The only way I know to check for the string "#Name?" is through the control's Text property, which is only available when the control has the focus, so that's not much help. If you retrieve the value of the ControlSource property, you'll get "BirthCountry" (of course). So you could check all ControlSources on your form against the fields of the form's RecordsetClone (add error handling, please):
For Each ctl In Controls
If Not RecordSourceContains(ctl.ControlSource) Then
ctl.Visible = False
End If
Next
Function RecordSourceContains(strFieldName as String) As Boolean
Dim fld As Field
For Each fld In RecordsetClone.Fields
If fld.Name = strFieldName Then
RecordSourceContains = True
Exit For
End If
Next
End Function
You'll probably want to do some other bookkeeping. For example, you'd want to check for expression control sources (which begin with "=") because they won't be in the RecordSource. You'll also either need to handle error 438 (Object doesn't support this property or method) or check each control's type to make sure it has a ControlSource property.
In design view for the form, what does the field say for Control Source? It should be blank, meaning the form is unbound. If it's not, then that explains #Name.
More information: http://www.databasedev.co.uk/unbound-forms-add-data.html
Here is code I use to take care of that:
Sub setControl
'' You can use this in your loop
Call FieldEnable(subFrm, "boxQty", booSellSet)
End Sub
''----------------
Private Sub FieldEnable(subFrm As Access.Form, strCtlName As String, _
booEnableMe As Boolean)
On Error GoTo errHandler
subFrm(strCtlName).Enabled = booEnableMe
subFrm(strCtlName).Visible = booEnableMe
errExit:
Exit Sub
errHandler:
If Err.Number = 2164 Or Err.Number = 2165 Then ''control has the focus
Resume Next
Else
DoCmd.Hourglass False
Screen.Application.Echo True
MsgBox "Problem with control named " & strCtlName
Resume errExit
End If
End Sub
I have to adjust many fields on the form, so this is very reusable. I might be turning them "on" or "off"; this takes care of either. Let me know if you have questions.
It all boils down to this method for hiding the field:
Control.Enabled = False
Control.Visible = False
Delete the field Black from table.
And then make make again the Black field in your table.