Make combo box record as table field name , MS Access - vba

I have combo box in form, i want to have it's drop down value to be Table fields name.Table name is tblCap, and the field are Year1,Year2 and Year3.I want that in the Combo box to have dropdown list as Year1,Year2,Year3 and when that year is selected it should display related field in subform.
Any help would be appreciated.
Thank you.

This is much simpler than you may imagine:
Open the Property sheet of the combobox, tab Data
Set RowSourceType: Value List
Set RowSource: "Year1";"Year2";"Year3"
To display the selected value (field name) in a subform, use this expression:
=[Parent]![NameOfYourCombobox]
To display, in a textbox on the main form, the value of the selected field name of the subform:
=[NameOfYourSubformControl].[Form]([NameOfYourCombobox])
or (?) on the subform:
=[Parent]([Parent]![NameOfYourCombobox])

Update after comments.
Once the RowSource has been set-up as per #Gustav answer, in order to hide/unhide the TextBox controls according to the value of ComboBox you need to set their Visible property to True/False.
On the Events tab set the AfterUpdate of your ComboBox control to [Event Procedure] and on the code behind file set the following:
Private Sub YourComboControlName_AfterUpdate()
With Me
Select Case .YourComboBoxName.Value
Case "Year1":
With .YourSubformName.Form
.Your2000TextBoxControlName.ColumnHidden = false
.Your2001TextBoxControlName.ColumnHidden = true
.Your2002TextBoxControlName.ColumnHidden = true
End With
Case "Year2":
With .YourSubformName.Form
.Your2000TextBoxControlName.ColumnHidden = true
.Your2001TextBoxControlName.ColumnHidden = false
.Your2002TextBoxControlName.ColumnHidden = true
End With
Case "Year3":
With .YourSubformName.Form
.Your2000TextBoxControlName.ColumnHidden = true
.Your2001TextBoxControlName.ColumnHidden = true
.Your2002TextBoxControlName.ColumnHidden= false
End With
End Select
End With
End Sub

Related

Hide record from continuous form if checkbox is marked in query

I have a continuous Form with records, the records contain a yes/no. So if the checkbox in the form is marked I want the record to be hidden from the form next time you open it, but it does not seem to work.
Private Sub Form_AfterUpdate()
If Me.Checkbox = False Then
Me.Checkbox.Visble = True
Else
Me.Checkbox.Visible = False
End If
End Sub
Add a Boolean field named Hidden having a DefaultValue of False in the table and, in the form, bind this field to the checkbox.
Then, when opening the form, filter on [Hidden] = False.

Hide/Unhide some fields based on user selection MS Access

I would ask if i can hide or unhide some fields based on user selections in MS Access
example:
if user choice 'Other' from dropdown menu a text box appears to write down their answer and so on..
i tried this code after update event but does not work
Private Sub NAME_AfterUpdate()
Select Case Me.NAME
Case "Other"
Me.other.Visible = True
End Select
End Sub
any help?
much appreciated
Me.Name is the name of the form. Thus:
Private Sub NAME_AfterUpdate()
Select Case Me!NAME.Value
Case "Other"
Me!Other.Visible = True
' Read value of "other".
Me!Name.Value = Me!Other.Value
End Select
End Sub

ListBox with multiselect property loses selected items

I need to change a filter in a PivotTable based on a ListBox with multiselect property = 1 - fmMultiSelectMulti, but when I run my VBA code items that are selected in the ListBox lose the selection.
Here is the code:
Private Sub BtnAtualizarLiberty_Click()
With Sheets("Liberty_Data").PivotTables("DinamicaLiberty1").PivotFields("RSCORE_CGV6")
.EnableMultiplePageItems = True
For Z = 1 To .PivotItems.Count
.PivotItems(Z).Visible = False
For r = 0 To ListRiscoScoreLiberty.ListCount - 1
If ListRiscoScoreLiberty.Selected(r) = True And ListRiscoScoreLiberty.List(r) = .PivotItems(Z).Value Then
.PivotItems(Z).Visible = True
End If
Next
Next
End With
End Sub
I've solved the problem. I was filling the listbox with a pivot table data and when I was changing the pivot fields filter at the any others pivot table, the listbox lost all the selected items.
So, now I'm filling the listbox 'manually', I've made a sheet called "Filters" and I've filled a column with all listbox row and it has solved the problem.
Sorry for the bad english.

SubForm won't Requery after MainForm Changes are made

I have an access 2010 database that has a main form 'MainForm' and a subform 'SubForm'. The SubForm is attached to the MainForm as a Subform/Subreport object. The user will select a unique identifier from a dropdown and the subform should use that identifier to pull up employee information on the subform. I have tried any number of ways to avail...
Private Sub Dropdown_Exit(Cancel As Integer)
If IsNull(Me!Dropdown) Or Me!Dropdown= "" Then
' nothing to do due to no one selected
Else
Forms!MainForm!SubForm.Requery
' Forms!SubForm.Requery
' DoCmd.OpenForm "SubForm",,,"[ID]=" & me!SubForm!ID,,acDialog
End If
End Sub
The commented out statements are only some of the things I have tried.
Thanks in advance
You should be able to do this without any code by specifying the LinkMasterField and LinkChildField properties of the subform control on your main form.
It is clear that LinkChildField should be set to ID in the form design mode. It looks like you'll want to set LinkMasterField to Dropdown. You can set the FilterOnEmptyMaster property to Yes to hide all records before the Dropdown is filled, or No to show all records before Dropdown is specified.
EDIT:
If LinkMaster/LinkChild are not appropriate, then code for Dropdown's AfterUpdate event. This fires after a choice is completed via keyboard or mouse. It should look like :
Private Sub Dropdown_AfterUpdate()
If Len(Me!Dropdown & "") = 0 Then
'' handle cleared Dropdown
Else
Subform.Form.Filter = "[ID] = " & Me!Dropdown
Subform.Form.FilterOn = True
End If
End Sub
Changing the filter should update the subform.

Show/Hide tab page from a combo box on a subform

I have been looking around the internet for ages trying to find this solution to a problem i've been having.
I have a form (Form1) which consists of tabs and a subform (subForm1). Within subForm1 I have a combo box which I want to assign an AfterUpdate Event to. If the combo box (in the subform) is yes, show tab1, else if combo box is no then hide tab1. The code I'm looking for is something like this :
Private Sub Combo52_AfterUpdate()
If Me.Combo52.Value = "Yes" Then
Forms!FrmAssessment.TabCtl50.Pages(5).Visible = True
ElseIf Me.Combo52.Value = "No" Then
Forms!FrmAssessment.TabCtl50.Pages(5).Visible = False
End If
Got any ideas?