How do I select a specific column based on a variable in a MS Access query? - ms-access

I have a large table with the following fields:
Date
Product_ID
AmountEUR_Field1
AmountEUR_Field2
AmountEUR_Field3
AmountEUR_Field4
AmountEUR_Field5
where each AmountEUR field represents the sales amount for a product.
The reason for having 5 different AmountEUR fields is that they are based on different Currency Rates (in example BeginingOfMonthRate, AverageMonthRate, EndOfMonthRate etc.).
I now want to copy a specific AmountEUR field to another table, but the AmountEUR field to be copied varies over time (sometimes it is AmountEUR_Field2, other times it is AmountEUR_Field5).
Therefore I need to select a specific column based on a variable from another table. (that variable should then have value between 1 and 5).
I have been thinking about making a new field called AmountEUR_ToBeUsed that is updated with the correct AmountEUR_Field, but that brings me back to the same problem of selecting the specific column I want copied.
Can a solution be made within the Access query designer, or do I need some VBA code?

You can just make this with the Access Query designer.
Specifically you will need the function IIF.
For instance, if you want to specify that before a date you wish to use AmountEUR_Field1, and otherwise AmountEUR_Field5 you can say:
IIF(somedate<#1/1/2011#,AmountEUR_Field1,AmountEUR_Field5)
Note, depending on the settings of your PC, you may have to say:
IIF(somedate<#1/1/2011#;AmountEUR_Field1;AmountEUR_Field5)

Related

How to customize TaxReport_IT - report SSRS?

I would like to custom the Report SSRS TaxReport_IT, I need to add a new field in order to use in to SyntethicReport Design.
(for example)
The classes involved seems these:
TaxReport_IT
TaxReportDP_IT
TaxReportController_IT
TaxReportContract_IT
Tables involved:
TmpTaxReport_ITSummary
TmpTaxReport_IT
TaxReportTmp_IT
Are there others? Are there some Queries involved?'
I added the new fields in to table _TmpTaxReport_IT_, and I pupulated the related tables
I'm sure, in to the method I pupulated correctly the new Field (I have the values ​​that I expect), but when I print the Report I get a lower value. Seems like I don't take the total data set.
How can I add the new field and take the correctly total value?
I saved the data (about my custom field in a RegularTable) and the sum is correct, but I have mismatch when I print the report.
I think I skip some step to Report DataSet.
I use Dynamics AX 2012.
Thanks in advice!
I have an idea what could be wrong, but I'm making some assumptions. If they are not correct, please edit your question to clarify.
From your screenshot, it looks like you want to add your new custom field in the header section of the report design. I'm assuming the expression of that field looks similar to
=First(Fields!MyCustomFIELD.Value, "TaxReportDS_IT")
Note the First key word in that expression. This indicates that the value for that field should be taken from the first of the records of the report's dataset.
I'm assuming that you calculate the value of the field while the records in table TaxReportTmp_IT are being created so that each record has a different value. Maybe it is a sum of some other field, so the first record would have the smallest value and the last record the highest.
If all those assumptions are correct, you can fix this by changing the First keyword to Last. This indicates that you want to take the value of the last record of the report data set.
See also the documentation of the Last Function.

How can I create a table that uses an equation to average data from another table?

I have a table that contains data from repeated experiments (for example, site A has one sample, and the lab processed the sample three times obtaining slightly different values). I need to average these results in a separate table, but what I have read on the Microsoft support site is that a query that pulls data into another table with a calculated field is not possible on Access.
Can I query multiple data points from one table into a single calculated field in another table? Thank you.
UPDATE
I ended up doing a lot of manual adjustments of the file format to create a calculated field in the existing table that averages each sites data, so my problem is, for my current purposes, solved. However I would still like to understand. Following up with you both, I think the problem was that I had repeated non-unique IDs between rows when I probably should have made data columns with unique variable names so that I could query each variable name for an average.
So, instead of putting each site separately on the y axis, I formatted it by putting the sample number for each site on the x-axis:
I was able to at least create a calculated field using this second format in order to create an average value for each site.
Would have there been a way to write a query using the first method? Luckily, my data set was not at all very hefty, so I could handle a reformat manually, but if the case were with thousands of data entries, I couldn't have done that.
Also, here is the link to the site I mentioned originally https://support.office.com/en-ie/article/add-a-calculated-field-to-a-table-14a60733-2580-48c2-b402-6de54fafbde3.
Thanks all.

Lookup tables displaying number instead of text [duplicate]

I am trying to create a report putting a field called contact which has the name of a person. This name is linked directly to another table where I keep all the contacts.
For some strange reason, when I include this name (which in query view displays as the name of the contact), instead of the name appearing, the unique ID number is shown on my report.
As mentioned in the article cited in the above comment, you can use a Combo Box control on your report to do the lookup for you. To see how this can be done, create a new report based on the table containing the lookup field, then drag and drop that field onto the report. That will create a Combo Box control with properties that look something like this:
Row Source: SELECT [Clients].[ID], [Clients].[LastName] FROM Clients;
Bound Column: 1
Column Count: 2
Column Widths: 0";1"
You could use a similar Combo Box control on your actual report to display the client's name rather than their numeric ID value.
Another alternative would be to change the Control Source of the report's Text Box control to have it do a DLookUp() on the table. If the lookup field is named [client] then changing the Control Source of the Text Box to something like
=DLookUp("LastName","Clients","ID=" & [client])
would also work.
I wanted to add to the great answer by Gord:
When using a "web" database (started in Access 2007 I think), you cannot change a report's fields to ComboBox style, nor can you use DLookUp(). (web databases lack a ton of features)
The workaround for this, if you want to create a Web-Report that uses lookup fields, is to create a Web-Query first based on your Web-Table (all the Web-* stuff has a www planet icon over the logo, if you create a new Web-DB in Access 2007+ you'll see what I mean)
So, instead of Table -> Report, you'll have to do W-Table -> W-Query -> W-Report.
Then, the only thing you need to customize to get the data right is the W-Query. Start by trying to reproduce the look in the query to match what you want users to see in the report. Note that here in the query, lookups will work fine (instead of the unique ID's, you get field names like you want). However, this will not carry over to the report. To do that, you gotta get the actual text field name you want into the query:
You should already have one table in your query; start by adding the table that your first lookup field points to. For example, the table I want to print is called Stock_Boards, and it has a lookup field called PCBID_lookup that points to the table Stock_PCBs.
Since you're using lookup fields, there should already be a relationship line between the two tables when you add the second one. If there isn't, something has gone horribly wrong.
Now, see how that line connects two fields on the two different tables? For example, I've got my PCBID_lookup field on my Stock_Boards table, which connects to the ID field on my Stock_PCBs table. If I created a report from this now, PCBID_lookup would be a number, a number that correlates to the ID of a record on Stock_PCBs.
To fix it, I will add the name field I want to show up on the report. In my example, that happens to be a Part Number, rather than the ID. I add the PartNumber field from my Stock_PCBs table to the query, and remove the PCBID_lookup field of the Stock_Boards table from my query.
Since PartNumber is what I want to show up on my report, it effectively replaces the original field (PCBID_lookup)
Repeat for all lookup fields you want in your report.
I had 1 more: I removed the Status field of the Stock_Boards table (which was an ID/Lookup) and added the 'Status' field from the Status table (which was the actual text name)
When finished, your query should look exactly how you want the data to appear, without any special tricks or asking Access to do something unnatural. Save your query, and create a web-report from it. Done!

If Else Statement in Access Table Field

I'm super confused how to apply the if/else statement in an Access table field, not a query. Please help! Here is what I'm required to do based on the table I constructed:
Fill in the overall rating field, using this formula: If the is available field is false, the overall rating is “pending”, regardless of the scores. Otherwise, if each rating score is 9 or above, the rating is ''excellent"; if the interview average is 9 or above and one rating score is less than 9, the rating is ''very good"; if the interview average is less than 9 and one (or both) rating score is 8 or above, the rating is "erratic"; otherwise the rating is "reject".
click here to view the table
From Access 2010 onward you can indeed create calculated fields in a table. To do that, open the table in Design View, enter the name for the new column and choose Calculated as its data type.
Use the Expression Builder or any external text editor to build/write the expression for the calculated field. There is only a limited subset of functions available to be used in a calculated table field. So in this case the Expression Builder actually is helpful, as it shows only functions that can be used in this context.
To achieve the result as per your requirements, you will need an expression with several nested Iif-Function calls. (The Switch function is not available here.)
The actual expression will be same as you would use in a query.
This is how you can do it, if you really want to. – I agree with the commenters that in almost any case it would be better to use a query to do any calculation of this kind.

MS Lightswitch Application, add temporary fields in data table

I have a requirement to add a temporary field in a data table. There are two types of fields you can add in data table. One is DATA fields which are actual fields, and Second is COMPUTED fields which have some limitations (not discussing here).
Let me explain my scenario, I have a table with fields Qty, Rate and Amount. Now I need to add one more field like InputRate which will be just a temporary field, do not have any role in the database. Why I need this is, I need to input rate let say in US Dollar, then then I have to convert this to my own currency say SAR. So I want that this temporary field will not save in database, but the actual one (the rate converted to SAR) will be stored.
We can do this easily in .Net windows Applications or Web Applications. But how can we do this in MS Lightswitch, because it will not allow to add fields on screen untill it is part of the data table. Even if you add a custom field (as I experiment) only on the screen, then it is going to repeat the same field's value for all rows (since my this table is DETAILS table). Means if my table has 5 rows, and in 6th row if I enter anything in custom field(with scope only on screen) then it is showing the same value of all other rows also, e.g. if I entered, then all other rows start also displaying the same value 10.
Any idea how to do this?