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

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.

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 a Form tab depending on the value of a field

pretty simple question here in scope.
Question:
Wondering If I would be able to hide the tabs of a form based off the values of a table's fields.
I have been reading the 2019 Access Bible and so far it is still unclear to me how I would write the VBA module to constantly be running. Im asking the question a little early in my attempts, but hoping I can ask this well enough to get a head start.
I dont quite understand the execution model of VBA for access yet. I have prior expierence coding but this will be my 1st time in access. Just looking for a little help on how to write the function scope. You see below that I think it should be "Main" something, as I want it to run whenever in form view. Like I know how to write Private sub functions to respond to a button click. But not for a function that just runs in the background of the form.
I guess the real question is when to execute? Right? Any suggestions?
I was thinking something along the line of this below.
Main function()
If Me.FieldProcess_Water = "1" Then
Me.TabProcess_Water.Visible = True
Else
Me.TabProcess_Water.Visible = False
End If
End Sub
This requires some setup to reduce redundant code but should do what you want.
First you'll need your checkbox names and page names to be similar. In my example I have the page names as Tab_Name and the checkboxes as just Name. eg. Tab_Process Water and Process Water.
Then Create a sub called Form_Current() in the form's code-behind.
Private Sub Form_Current()
Dim chk As Control
For Each chk In Me.Controls
If chk.ControlType = acCheckBox Then
If chk = False Then
'Change TabCtl0 to whatever your's is called
Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = False
Else
Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = True
End If
End If
Next
End Sub
This will iterate through the current record's checkboxes and toggle it's respective tab's visibility.
To have the CheckBox update the tabs as they are clicked:
Private Sub Form_Load()
Dim chk As Control
Dim prop As Variant
For Each chk In Me.Controls
If chk.ControlType = acCheckBox Then
chk.OnClick = "=Check_Click()"
End If
Next
End Sub
This will assign a command to each checkbox.
Dim caller As String
caller = Me.ActiveControl.Name
If Me.ActiveControl.Value = False Then
Me.TabCtl0.Pages("Tab_" & caller).Visible = False
Else
Me.TabCtl0.Pages("Tab_" & caller).Visible = True
End If
This will hide the relevant tabs.

VBA code for unhiding a bookmarked text is not working

I've created an ActiveX dropdown list and each option is linked to a bookmark for the text. Under the ActiveX controls there are the bookmarks (R1 andR2), hidden.
When I hit the btnselect button, all the other bookmarks, except the selected one, get deleted and the selected one becomes visible.
In the bookmark R2
I have a MacroButton for showing/hiding another text (CollapseMentiuniReclamant). When clicking the button it runs either Expand1 sub or Collapse1 sub, but the bookmark CollapseMentiuniReclamant doesn't show up.
I've simplified the document and codes as much as possible. Link to the document - https://wetransfer.com/downloads/1caea3c5d3b05e226e8b8f6a29760ad220190522071742/15db59.
The vba code is:
Private Sub btnselect_Click()
If ComboBox1.Value = "1" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("R2").Range.Delete
End If
If ComboBox1.Value = "2" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R1").Range.Delete
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End If
End Sub
Sub Expand1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Collapse1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
Sub Collapse1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Expand1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End Sub
Update: I've simplified the last part of code and the problem still persists:
Sub Expand1()
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
I've even removed the button entirely and ran the macro from View Macros Tab and it's not working.
Why doesn't CollapseMentiuniReclamant show up?
It's not showing up because what you're trying to hide/unhide isn't inside the bookmarked range. In any event, you should be inserting/deleting the content, not simply toggling it's hidden property. Making something hidden is no guarantee it won't be seen or printed (even if not seen), as those settings depend on how the end user has Word configured.

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.

Copy record from previous field if specific combobox value

I have an access form that needs filling in daily by various people.
It's to document changes to a website and I currently have a combobox box set up for the various sections to state whether they are AMENDS, REVERTS or NO CHANGE.
I have set conditional formatting to then highlight these sections but am also trying to get it to work so that if the user chooses "NO CHANGE" then the data for that field copies over from the previous record.
I have set this up in the AfterUpdate code for the combobox, but nothing is happening, not even an error... can anyone help?
Private Sub COMBOBOX1_AfterUpdate()
If Me.COMBOBOX1 = 3 Then
Me.[FIELD_TO_CHANGE] = DLookup("[FIELD_TO_CHANGE]", "tb_TABLE", "[ID]=Forms![form_FORM]![ID]-1")
End If
End Sub
(Where 3 is the value of NO CHANGE in the combobox, and FIELD_TO_CHANGE, tb_TABLE and form_FORM being the names of the various elements)
Thanks!
First you should define your control COMBOBOX1, enter in Properties Window, define
COMBOBOX1.AfterUpdate = "[Event Procedure]"
then your Private Sub COMBOBOX1_AfterUpdate() will be taken into account. Error may occur and popup to you.
Then change the event handler like this to start:
Private Sub COMBOBOX1_AfterUpdate()
If Me.COMBOBOX1 = 3 Then
Me.[FIELD_TO_CHANGE] = DLookup("[FIELD_TO_CHANGE]", "tb_TABLE", "[ID]=" & (Me.[ID] - 1))
End If
End Sub
There will be many errors to correct before your form works...