Hide/Unhide some fields based on user selection MS Access - vba

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

Related

How do I hide/unhide a subform based on the input from two comboboxes?

I have a mainform, called TrainingsSU
In it I am calling subform qry_TrainingSU (built from the query of the same name)
I have it set up so that the records auto populate the subform based on the two comboboxes in the main form.
But what I need to do is hide the subform, and have it display only when the two comboboxes are populated and records are loaded.
Here's the current VBA (of which I am not really even a novice at)
Private Sub cbo_EmployeeLookup_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_EmployeeLookup.Value = "" Then
Forms!qry_TrainingsSU.Visible = False
Else
Forms!qry_TrainingsSU = True
End If
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_TrainingName.Value = "" Then
Forms!qry_TrainingsSU = False
Else
Forms!qry_TrainingsSU.Visible = True
End If
End Sub
I found the general form of this code in another answer, here: MS Access: Hide and Unhide subform based on drop down in Main Form
However the code doesn't seem to be working for me.
Currently the subform is set to Visible:No
So nothing shows up at all.
If I change that, it doesn't disappear and the empty subform is still visible.
This wouldn't be a problem, except I need to use this form for another query and want to layer them over eachother when the second subform is ready to be used.
Later this form will be used to push an UPDATE SET to a table based on the different subforms.
Is there something that is obviously wrong with the code, or did I miss a setting somewhere?
You can try this:
Private Sub updateStates()
Me!qry_TrainingsSU.Form.Requery
If (Me.cbo_EmployeeLookup.Value <> "" AND Me.cbo_TrainingName.Value <> "") Then
Me!qry_TrainingsSU.Visible = True
Else
Me!qry_TrainingsSU.Visible = False
End If
End Sub
Private Sub cbo_EmployeeLookup_AfterUpdate()
updateStates
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
updateStates
End Sub
Forms!qry_TrainingsSU searches form opened as main form, not a subform.

How to assign values entered in a textbox to a combobox selection on another form MS Access using VBA

I have a form with 6 textboxes, 3 of which hold the updated monetary value of monthly, quarterly and annual membership types. In another form I have a combobox with the 3 types of membership, when a membership type is selected I want to output the updated monetary value of the selection to a textbox. Would appreciate if anyone could help/recommend any resourceful links. My vba understanding is decent however struggling with writing code that runs correctly.
Private Sub cbMmemberType_change()
If cbMemberType.Text = "Monthly" Then
frmSubscriptionDetails.txtSubCost.Text = frmUpdatedCosts.txtUpdatedMonth.Text
ElseIf cbMemberType.Text = "3 Months" Then
frmSubscriptionDetails.txtSubCost.Text = frmUpdatedCosts.txtUpdated3Months.Text
ElseIf cbMemberType.Text = "Annual" Then
frmSubscriptionDetails.txtSubCost.Text = frmUpdatedCosts.txtUpdatedAnnual.Text
End If
End Sub
The Text property is only valid when the control has focus, and use the AfterUpdate event, so try:
Private Sub cbMmemberType_AfterUpdate()
Select Case Me!cbMemberType.Value
Case "Monthly"
frmSubscriptionDetails!txtSubCost.Value = frmUpdatedCosts!txtUpdatedMonth.Value
Case "3 Months"
frmSubscriptionDetails!txtSubCost.Value = frmUpdatedCosts!txtUpdated3Months.Value
Case "Annual" Then
frmSubscriptionDetails!txtSubCost.Value = frmUpdatedCosts!txtUpdatedAnnual.Value
End Select
End Sub

Make combo box record as table field name , MS Access

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

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.

Select a option from a filtered cell

I am new in VBA so i am unable to do this.
I have 21 sheets in a workbook. I want to select a cell in 3rd sheet (which contains a Pivot Table) which I am able to do. This cell B3 contains a filter and I can select from the filtered drop menu on how to sort my data. It contains whether I want to filter by first name OR last name OR all.
My usual routine is first to select by first name, then copy the filtered data and paste it on another sheet. Then come back to the same sheet and filter by last name and then copy the filtered data and paste it on the sheet I pasted the earlier data.
What I need help with is the following:
If any or all check boxes are selected then deselect them.
Select the first_name checkbox in the filter drop down
De-select the first_name box and select the last_name box
Finally deselect last_name and then select all checkbox
I have used the following code
Public Sub Open_Sheet3()
Workbooks("MASTER.xlsx").Activate
ActiveWorkbook.Sheets("Sheet3").Activate
ActiveSheet.PivotTables("PivotTable1").PivotFields("Technology").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Technology")
.PivotItems("Mobility").Visible = False
.PivotItems("(blank)").Visible = False
.PivotItems("Enterprise Messaging Tech").Visible = False
End With
End Sub
This code should do what you want. Just replace the names of worksheet/pivot table/pivot field and fill in the copyStuff sub.
Private Sub YourProcedure()
SelectItem "first_name"
CopyStuff
SelectItem "last_name"
CopyStuff
SelectItem "all" 'in case you have an element called "all"
CopyStuff
SelectAll 'In case you mean the "(All)" 'element', i.e. include everything
CopyStuff
End Sub
Private Sub SelectItem(strItemName As String)
Dim i As Integer
'Change to your worksheet/pivot talbe/pivot field name!
With Worksheets("Sheet 1").PivotTables("PivotTable1").PivotFields("a")
.PivotItems(1).Visible = True 'at least one item always needs to be visible
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next
.PivotItems(strItemName).Visible = True
If .PivotItems(1).Name <> strItemName Then .PivotItems(1).Visible = False
End With
End Sub
Private Sub SelectAll()
Dim i As Integer
'Change to your worksheet/pivot talbe/pivot field name!
With Worksheets("Sheet 1").PivotTables("PivotTable1").PivotFields("a")
For i = 1 To .PivotItems.Count
.PivotItems(i).Visible = True
Next
End With
End Sub
Private Sub CopyStuff()
'Your code goes here
End Sub
Some explanation:
If you want to deselect items of a pivot field, you need to make sure that there's at least on item selected at any time. Therefore you cannot unselect all and then select your desired item - but rather select the first item, unselect all other, select your item and unselect the first item unless it is your item. That's what SelectItem is doing