Display queries in access subform from a selected listbox - vba

Within Access 2010, I am trying to make a form display a query within a subform QueriesSubForm that when selected from listbox QueryListBox and then click on the button runbtn. It is supposed to display the query within the linked subform, and change when you do this whole steps again from a different choice selected on the listbox and click on the button.
At the moment I can only get it to work, if I want it to display the queries in a new task window, when programmed like so:
QueriesListBox Listbox - SQL View
SELECT MSysObjects.[Name]
FROM MSysObjects
WHERE (((MSysObjects.[Type])=5) AND ((Left([Name],1))<>"~"))
ORDER BY MSysObjects.[Name];
runbtn Button - VBA View
Private Sub runbtn_Click()
DoCmd.OpenQuery QueryListBox, acViewNormal
End Sub
It works and appears to just open one of the queries from the queries list and display it to me, which I do not want.
This might be an easy thing, but I can not see how it is done with either SQL, VBA or Macro, which I am guessing is need for this to work

Ok, here's one way to do it. First make sure your subform is set up with no source object. Then setup your button click like so:
Private Sub runbtn_Click()
Me.QueriesSubForm.SourceObject = "Query." & QueryListBox.Value
End Sub
That should get you the result you're looking for. Alternatively, if you want your form to open with a certain query to displayed on open, you could set up your subform's source object to that query. Using the properties window find the source object drop down and find the query you want to show when the form opens.

Related

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

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;

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.

Microsoft Access: Different Queries Based On Input

I want to create a form that includes a drop-down list of queries, and then a button that when i press it, runs the query which i have selected from that drop-down.
I don't mind manually filling in the drop-down and then adding comparison code if that is possible. What is the best way to do this?
You can populate a combo or probably better a listbox using the following query.
This would list all queries that were named starting with qryRep... and display them as Rep...
SELECT MsysObjects.Name, Mid([Name],4) AS QueryName
FROM MsysObjects
WHERE (((Left([Name],6))="qryRep"))
ORDER BY MsysObjects.Name;
By restricting the display to a named group, you don't list every query in the database.
To run the query from the double click event of the list box - assuming it was called lstQueries;
Private Sub lstQueries_DblClick(Cancel As Integer)
Dim txtQueryName As String
txtQueryName = Me.lstQueries
DoCmd.OpenQuery txtQueryName , acViewNormal, acReadOnly
End sub
This will open the query in a read only view.

Macro in form stops working after opened with vba

Please help me explain, why this happens, after that the solution should be easy :)
I have two forms, showing different data.
Form_1: there is a combo box (with names in it), where you can choose which company you wanna see, and an after-update macro searches the record (an [ID] field), and shows the information. (To be more complicated, this [ID] field is hidden, and used for subforms, where the actual infos appear.)
Form_2: this is a continuous form, each record is in connection with the companies shown in Form_1, but several record can belong to one company. There is a button for every record to open Form_1 with the information connected to it. The vba code of the button is:
Private Sub Button_Click()
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
End Sub
In the code, the same [ID] field is used, as described above: hidden and used for subforms.
Both forms are working as needed, I am happy with them.
But after Form_1 is opened from Form_2 with the button, the combo box remains empty (actually I don't need it to be filled), and if I wanna use it to search for other items, it doesn't work, as if the macro wasn't loaded. The list of names appear, I can click on any of them, but the [ID] field is not refreshed (and of course neither the subforms). I have to close the form, and open it again from the side-list.
Why does the macro stop working?
What should I change, to make it work?
Thanks for your help!
Form1 has the filter turned on to a specific key value, so attempts to find and reposition the form's current record will fail without explicitly resetting the filter.
The Where condition of the OpenForm command does not change the form's Record Source property, nor does it perform a simple search/reposition. Rather, it applies a form filter:
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
This state is indicated in several ways
On the Home ribbon (i.e. toolbar): The Toggle Filter button is active... is highlighted by a different color.
The form's navigation bar at the bottom of the form show "Filtered" highlighted with a little funnel icon.
The Access status bar shows "Filtered" on the right near other indicators.
Of course it's possible that all of those indicators are hidden, so you just need to be aware of what each command and parameter does.
Possible solutions:
Form1's ComboBox.AfterUpdate macro should turn off the filter before searching for a new ID value.
Form2's Button_Click event opens the form without applying a filter and instead runs code that does the same thing as the ComboBox.AfterUpdate method--searches and repositions the form's record rather than filtering it.
This can be achieved in multiple ways and is largely beyond the scope of this answer, but a hint is to make a Public method in the Form1 module that performs the search. Both the ComboBox.AfterUpdate method and the other button call that same public method so they have the same behavior.

Fill in a text box when selecting a combo box option on Access

Im not very advanced in Access and I am struggling with defining the VBA code/expression build/ or Control Source for an instruction that can make the following isntruction:
If i make a selection on a Combo box (with my key field "TypeService" on my table TblMain)
Then; my text box is able to return the TypeConfig rows related to the TypeService selected before (my table TblConfig contains TypeServiceFK and TypeConfig fields)
So, my objective is to get a dynamic textbox that depends of a multi option combobox.-
Thanks for the help!!
Put a listbox on your form, let's say it's called List2, and cancel the wizard if it pops up.
In the Row Source property of the listbox, put this SQL statement
SELECT tblConfig.TypeConfig FROM tblConfig WHERE (((tblConfig.TypeService)=[Forms]![frmMain]![Combo0]));
This assumes your form is called frmMain and your combobox is called Combo0, so change those as necessary.
Right click on Combo0 and choose Build Event, then Code Builder. Replace the Sub...End Sub stub with
Private Sub Combo0_Change()
Me.List2.Requery
End Sub
Now every time Combo0 changes, the SQL statement behind List2 will run and return the TypeConfigs associated with that TypeService.