How to Update the Values of a Dropdown in a Subform when the Main Form Changes - sql

I have two forms:
InterviewMaster and InterviewDetail
InterviewDetail opens up as a subform in InterviewMaster and these two forms are linked through a common field called InterviewID
In InterviewDetail I have a textbox called Questiontype as well as combobox called InterviewDropdown.
The data in the dropdown varies based on the data on in the textbox. To make this happen, I have a next button to move to the next question. Whenever I click on next the following runs:
Dim ctlCombo As Control
Set ctlCombo = Forms!InterviewDetail!cmbInterviewDropdown
ctlCombo.Requery
The Row Source setting for my combobox is set to look up the required answers, again this is based on the value as per the textbox:
SELECT [queryAnswerOptions].[Answer] FROM queryAnswerOptions ORDER BY [Answer];
So the options are determined by my query called queryAnswerOptions
So as I cycle through my questions using my next and previous buttons, the dropdown options are updated based on the value of my textbox. This works perfectly when I open the subform from the navigation pane. However, when I open the main form and click on the next button my dropdown does not have any values. I've tried requerying the subform with no luck. I've also tried opening the subform in full screen from my main form but this also does not work. I also don't want to go that route as it does not work well with the overall flow of my form.
Any assistance will be greatly appreciated.

Problem with the query WHERE criteria parameter is when form is installed as a subform, the form reference no longer works because it must use the subform container control name. I always name container control different from the object it holds, such as ctrDetails.
Option is to put SQL statement directly in combobox RowSource instead of basing combobox RowSource on a dynamic parameterized query object - then it will work whether form is standalone or a subform.
SELECT Answer FROM InterviewAnswers WHERE QuestionID = [txtQuestionID] ORDER BY Answer;

Related

How to link dynamically loaded unbounded subforms to main form?

I have a bound main form (FormA) with a combobox on it and two unbound subforms (subfrmA & subfrmB). (Both forms are attached to a table however I want them to load onto the main form where I placed an unbound subform as a placeholder.)
The combobox has two values “a” and “b.” When a is selected I want subfrm A to load onto Form A. When b is selected I want subfrmB to load onto Form A. I think have this part working.
However when I select a record on the main form the associated subforms don’t appear. When I try to link the subforms to the main form an error message appears saying I can’t build a link between unbound forms.
The packageID is the link between the main form and subform and is a hidden field on all forms. Whenever the packageID is automatically updated the psckageID in the subform fields are also updated.
Case”A”
Me.subfrmAB.SourceObject=“FormA
Me.packageDetailsID=Me.subfrmAB.packageDetailsID
Case “B”
Me.subfrmAB.SourceObject=“FormB”
Me.packageDetailsID=Me.subfrmAB.packageDetailsID
EDIT: I ended up creating two subforms subfrmA (Form A) and subfrmB (Form B). Then I linked both to the parent form via the master and child links.
I make one of the subforms visible and the other invisible depending on what the user selects in the combobox of the main form.
Everything works except Form B won’t load, but the container loads. I tried loading Form B separately by itself it still won’t load. I also deleted subfrmA and Form B still doesn’t load.
Here is my edited code:
Select Case Me.Authorization.Text
Case “A”
Me.subfrmA.Visible = True
Me.subfrmB.Visible = False
Me.subfrmA.SourceObject = “Form.A”
Case “B”
Me.subfrmB.Visible = True
Me.subfrmA.Visible = False
Me.subfrmB.SourceObject = “Form.B”
End Select
The only line that doesn’t work is the Me.subfrmB.SourceObject=“Form.B” and really there’s something that’s preventing the form specifically loading. I wrote the same code for Form A and Form B but can’t figure out what’s wrong with Form B.
Can certainly be done. Here is a simple example that works for me.
Main form is bound to table Games. Forms used as subform are Umpires and Teams.
Combobox properties:
ControlSource: UNBOUND
RowSource: Umpires;Plate;UmpID;Teams;HomeTeam;TeamID
RowSourceType: ValueList
BoundColumn: 1
ColumnCount: 3
ColumnWidths: 1.0";0";0"
Code:
Private Sub Combo108_AfterUpdate()
With Me
.ctrAB.SourceObject = .Combo108
.ctrAB.LinkMasterFields = .Combo108.Column(1)
.ctrAB.LinkChildFields = .Combo108.Column(2)
End With
End Sub
You could have "A", "B" for the form names in combobox RowSource and then if both forms have same name key fields, don't need them in RowSource, just hard coded. Not entirely clear what the key field names are. Then code like:
.subfrmAB.SourceObject = "subfrm" & .Combo108
.subfrmAB.LinkMasterFields ="packageDetailsID"
.subfrmAB.LinkChildFields = "packageDetailsID"
If you want to save "A" and "B" to main form record, then bind the combobox to field. Then for subforms to change for each record while navigating main form, also have code in form OnCurrent event.
Something to be aware of when coding interaction between form/subform: subforms load before main form - seems odd but is true.
One option is to create a dummy table with one record and bind the subform to that. But you'd have to read and write all the values with code.

MS Access - FIlter subform based off listbx

I am trying to filter a subform based off a value selected in a listbox.
My list box is called cboCurrentListName and this is populated using a select query
The subform is called Form_subform_ListContents
For the change event for the list box I have the following code, however no filters are applied when its executed.
Me.Form_subform_ListContents.Form.Filter = "[ListName]=" & Me.cboCurrentListName.Value
Me.Form_subform_ListContents.Form.Filter = True
I have also tried using Master/Child links to execute this using the following steps, but this caused an input box to pop up when the fom loaded:
first I set the 'link child fields' option in the properties of the subform to 'ListName' (this is the field name that populates the list box and is present in the subform
then I set the 'link master fields' option in the properties of the subform to 'cboCurrentListName' (the listbox name)
then in the listbox properties I set the control source to 'cboCurrentListName'
The above steps cased an input box to pop up on opening the form (the value typed in here does filter the subform). However I don't want an input box in order to do this, I want to use the listbox.
I have googled this and fried different methods but have had no luck. I am pretty new to access and of the control source sections aren't a strong point of mine, which is why I tried to use the VBA on the event change instead
Any help would be appricaited, I tried a few things and had no luck.
EDIT: When trying the master/hild option I also get an error 'Can'y build a link between unbound forms' if I click the 3 dots in the poperties. SO for the above steps I had to manually type in the otpions.

Access: Execute Code when a value in dropdown is selected

I have a subform, which consists of a table.
In this table, one columns values are a dropdown field (with 3 types to choose from: Var1, Var2, Var3).
If a user now selects a value from this dropdown I want to execute a specific VBA code in the background.
For my understanding, that means I have to set up an event like "run code module xy if dropdown value changes".
I am a beginner and have no idea how to implement that. Hopefully you are able to help.
Thanks so much in advance!
My Code:
Private Sub runMacro()
''MyCodeHere
End Sub
My form with the table as a subform. You can select 3 different values for "solution". If one is selected, I want to run VBA code:
You have to open the form, which contains the Combo Box (the Drop Down) control, in Design View.
Then select this Combo Box control and open the Property Sheet if not already shown (you can swap its visibility with the key F4).
In the Property Sheet select the tab named Event and click into the event After Update.
Using the ...-button you can select the Code Builder to open the VBE.
It already prepared the event handler for you, where you now can place your code.

How to reference events on objects in a subform from inside the main form

Imagine a form that, for demontration purposes, contains only a subform and a textbox that need to mirror each other.
I tried to accomplish this by setting the Control Source on the textbox to the value of the field in the subform and this worked to make them mirror each other, but the textbox isn't editable so this is an unsuitable solution.
The next thing I decided to try was using the AfterUpdate event on both controls to run code that sets the value of the other control.
This is easy for the textbox:
'Set value of Notes field on subform whenever value of the corresponding textbox
Private Sub Notes_Textbox_AfterUpdate()
Me.subform.Form![Notes] = Me.Notes_Textbox.Text
End Sub
However, this isn't as simple for the subform field. I don't know how to reference an event on a field in a subform in a way that will still allow me to reference controls outside that subform.
As a demonstration, I need a way to do this:
Private Sub subform_Notes_AfterUpdate()
Me.Notes_Textbox.Text = Me.subform.Form![Notes]
End Sub
I can access the subform fields AfterUpdate event within the scope of the subform but if I do that, then I can't access the Textbox because it is in the main form, not the subform.
So I either need a way to define an event function on a field in a subform from within the scope of the main form, or a way to make the textbox part of the subform while maintaining the subforms ability to open in datasheet view.
Not sure I get the whole problem (how does datasheet view come into play?), but this should do what you want:
(in the subform)
Private Sub Notes_AfterUpdate()
Me.Parent!Notes_Textbox.Value = Me![Notes].Value
End Sub
Edit from comment: By far the easiest solution would be a continuous form instead of a datasheet in a subform.
You can have two textboxes, a one-liner in the Details section, and a large one in the form header or footer. Both have the same control source and are automatically updated when the other one is edited. No code required.
If it must be a subform-datasheet, you can use a similar approach: bind the main form to the same recordsource, and in On Current of the subform, move the main form to the same current record.

sql show listbox values only when combobox is populated

Firstly, I'm pretty new to access so I apologise if this is basic but I can't find the right answer either.
I've had some help setting this up so there are parts that have been created that I don't fully comprehend.
I have created a form that is to be used for data entry which consists of the following fields:
TEAMS - Combobox the rowsource of which is a table TEAMS; CALLREASONS - Multi-Select Listbox the records of which currently exist in a table CALLREASONS; ACTIONS - Multi-Select Listbox the records of which currently exist in a table CALLACTIONS; SUBMIT - Button with VBA which sends selected data to a number of tables.
The database works fine and things a saving where they should be and in the format they should be, the problem I'm having is with the display of the form.
I would like for the form to display blank when opened and after each time a record is submitted. However, if I set the rowsource of the listboxes to the tables all options show when the form is loaded and after each submission the options and the previous selections show.
How can I make it so that the first listbox CALLREASONS only displays possible options once a selection has been made in the combobox TEAMS and the second listbox CALLACTIONS only displays possible options once at least one selection is made in the listbox CALLREASONS ?
Thanks in advance for any help.
You can either change the controls row sources on the Update or Enter events, or you can simply enable/disable them on the events.
Lets use 3 nested comboboxes for an example.
Combo1 Has rowsource set permanently as it is the top level.
Combo2 and Combo3 Have no rowsource set.
On the Enter event of Combo2, you can set the rowsource to
SELECT Pkey, DisplayText FROM tblOptions WHERE OptionGroupField = " & Forms!Form1!Combo1BoundField
On the Enter event of Combo3, you can set the rowsource to
SELECT Pkey, DisplayText FROM tblOptions WHERE OptionGroupField = " & Forms!Form1!Combo2BoundField
So on so forth.