How make appears a field from the field list automatically? - ms-access

I just started to use Access with vba. I have table with roles, years and its workload of each role in this specific year. Each record in the table is IDRole, IDYear,Workload (number).
I display the table in a form doing a crosstab table where the years are in the columns and the roles in the rows. The number of years can increase.
My problem is when I add new years and display in the columns, it doesn't appear in the form so I have to select from the field list manually but I would like to know if there is any way in VBA that when the form is load, automatically appears the new columns (years).

I misunderstood your situation. Your form's crosstab data source is actually a table instead of a query. And as you add columns to that table, you want the new columns to automatically appear in the form when you next open the form.
In that case, use the approach described below, but where I selected a query (Query.qryFoo2) in the Source Object dropdown, choose your table ... Table.YourTableName ... in that dropdown.
The last 2 paragraphs below apply whether your data source is a table or query.
If you have your crosstab query saved as a named query, you can use it as the Source Object for a subform control.
Then when you switch from Design to Form View, the query results will be displayed in Datasheet View within the subform control.
As the number of columns changes over time, that Datasheet View can "automagically adapt" to display them ... the same as if you opened the named query directly in Datasheet View.
As far as I'm aware, there is no other reasonably simple way to accomplish what you want. If this suggestion is not suitable, you can explore programmatically adding bound data controls to your existing form after examining the set of fields the query currently returns. But that is way too much effort IMO.

I have exactly the same issue, I have a form which I use to show a Query, And I can't anticipate the number of columns from the query.
I found the same topic on other pages, and for the moment the only way I found is to build an excel file based on the query.
Another way also is maybe to directly open the crossTab query using vba.

Related

Question on bound/unbound forms in MS Access

I want to create a form populated with data from two tables, say table A and table B. The data from table A refers to a single row in A but the data that I have to retrieve from B is several rows long. I succeeded in doing so by setting up an unbound form and running a SQL JOIN query to fetch the appropriate data in VBA.
Then with VBA I was able to distinguish the data from the multiple rows in B.
Is it possible to achieve the same with a bound form so that I save myself from excessive coding? I can't figure out a way to populate my controls in the form with data from the rows in B.
If I understand you correctly, your two tables are related, that is, you search in B based on a value on A. If that's the case, the usual solution in access is to use a main form and a subform inside the main form. Then you link the two. When you insert the subform, a wizard will appear asking for the relation. You set up the columns that are common in the two tables, and everything works, usually. You can anyway add the relation later.

Filter different query fields from different forms

I have a query in MS Access that pretty much has every data I need for different reports. I know how to filter the query from a combo box in a form, however, I can't do it from multiple forms. Let's say for one occasion I want to filter the query from one form but for another purpose, I want to filter the same query in a different field from another form. When I try it there is a window that pops up asking for the criteria in the other field. Bear me with my English is not my first language.
Respectfully

Access 2016 drop-down list appearing in Reports and Queries

In Access 2016 I have a table called 'Orders'.
I also have a Form (also called 'Orders') that we use to enter order information. On the form, one field in particular ('Company') is a drop-down list. The control source is a second table called 'Companies'. And the Row Source is a SQL Query:
SELECT [Companies].[ID], [Companies].[CompanyName] FROM Companies ORDER BY [CompanyName];
So, when the user is entering an order into the Form, he/she can select the company name from this drop-down list and it in turn updates the Orders table. All basic stuff, and it works fine.
Next, I created a query (also called 'Orders') and it is based off of the Orders table.
When I run the query in Access and view it as a Datasheet, I was surprised to notice that the Company field (IN THE QUERY datasheet) is a drop-down list. Not only that, it even lets me change the value - right here in the query! If I view the SQL for this query, I can see that it is a SELECT query. In my mind, a SELECT query is read only. So my questions are - What's a drop-down list doing in a Query, and WHY does Access let me edit the values directly in the query? Isn't this supposed to be read only?
Next question:
After verifying that all of the data I need is in the Orders query, I then created a report (called 'All Orders') and the control source for this report is the Orders query. (not the table).
When I view the Report in Design view, there's the drop-down list again. Why? I am just looking to add the Company name that the user selected when they completed the form. I realize that the form Control for entering that data (on the FORM) is a drop-down list. But here I am building a report and I just want the VALUE to appear here. Not the drop down control? (I know that when I print the report or view it on the screen, the drop-down boxes go away and all I see is the actual text). But my question is - WHY is Access showing me a drop-down list control on a report? and in a select query? The query and the report are no place for editing data. I just want the value that was selected.
That's because you've defined the lookup list in the table. If you do so, it propagates to queries and new reports and forms, and will be the default way to view the data everywhere you've placed it.
Open your table in design view, and change the display control for your field back to text box. Note that any forms and reports will need to be edited or recreated, for queries the change should propagate.

Access form must be opened twice before combobox values will appear

I have a checklist that is used to perform QC audits. When a reviewer answers a question that requires an exception, a pop-up form will open with the correct exception detail already populated. They then have to provide some further information, which I have set-up in the format of a combobox for a field named 'Condition_Detail.' Some exceptions have only one condition that would be an option in the drop-down whereas others have multiple possible conditions, which is why I don't have that field automatically populated as well. I have a table of possible exceptions and conditions that I have used to create the combobox query. When the database was created, the field the 'Condition_Detail' combobox is dependent on was labeled 'Exception Detail' with a space and in my new table it is called 'Exception_Detail.' I did this because the old data and tables will eventually become irrelevant and I know that it is easier to write code with underscores instead of spaces. I include this information because I had to create a relationship between those two fields in the combobox query so that the database would know they are the same. When I test the database, the form opens as expected with the exceptions already populated (NOT in combobox format - the data appears as text on the form). The issue is that when I select the combobox, it is blank. If I answer the exact same question the same way to trigger the same exception to open in a new record, the combobox does have the correct data in it. I have tried to requery the field both when the form loads and after it updates. It still won't show the combobox values unless I trigger that the form open twice with the same detail populated. An additional piece of information that likely doesn't affect the problem is that the combobox is set-up as three columns so that I can populate two additional fields when the condition detail has been updated. I used the code below to populate that, which works perfectly when I can get the condition detail to appear in the combobox:
Private Sub Condition_Details_Change()
Me.Responsible_Position.Value = Me.Condition_Details.Column(2)
Me.Severity_Description.Value = Me.Condition_Details.Column(3)
End Sub
Thanks in advance for your help!
I figured it out. The relationship I created within the combo box query was unnecessary. The query was searching for values in the table that stored the actual responses/conditions instead of the table that stored the available response options. Because of this, once I had answered the question it knew what to look for, but in the wrong place. Setting up the combo box the usual way with the look-up control value in the form referenced as follows was sufficient to get the combo box to work properly.
Field: Exception_Text
Table: tblPreCloseExceptionDescEnc
Criteria: [Forms]![frmEncompassExceptions].[Form]![Exception Text]

how to create a filter form for a Microsoft Access Report

I am fairly new to Access. I am trying to create a filter pre-report form. On this form, the user will be asked for a start and end date. He will also be given a list of Item names which are found in a different form/table and will have the ability to check off which items the report should filter on.
Do I need to use a subform for this? I tried one out, but I can't see how to add checkboxes, it seems to just give me a list and I can't modify the subform.
What I really think I need to do is to populate a checkbox list with all of the items in the other table. How exactly would I do this (if its really the best solution)?
You can use a regular form for this. You do not need to bind it to a table. Just drop 2 textboxes on the form, and as many checkboxes as you feel you need.
The report will be based on a query, which in turn will be based on this form. All the fields will be brought into the query and will reference the controls on the form.
For instance, let's say you have 2 textboxes on the form; one called txtStartDate and one called txtEndDate. The form will be called frmReportFilter. In the query that's driving the report, pull in your date column and in the Criteria put >=Forms!frmReportFilter!txtStartDate. This will pull in all records where your date field is greater than or equal to whatever is in the Start Date textbox. The rest will be referenced similarly.