Calculating the Sum of values in ms-access - sql

I have created a query to calculate the sum of all of the profit values in a table, I tried to output this to a textbox on the main form of my database and I just the error #NAME?.
Has anyone tried this before and are there any major things I am missing?

We would need to see some code/design details to understand why your text box gets that #Name error.
Without those details, I'll just suggest you consider a DSum() expression, instead of a query, to load the text box. And DSum() is kind of like a SELECT query, but returns only a single value instead of a result set.
DSum("YourNumericField", "YourTable")
Examine the DSum online help topic for more details. You might find the optional Criteria parameter useful (like a WHERE clause in a SELECT statement) if you ever want to sum only a subset of rows from your table.
DSum("YourNumericField", "YourTable", "account_status = 'ACTIVE'")

If you have create query to calculate the sum called "querySum". On the form property sheet, make sure selection type you change to FORM, go to Data and select "querySum" as record source.
And then, click text box, go to the property sheet and choice Data > Control Source. So you can choice column from query to the text box.
Otherwise, if you want to use VBA. You can do like this
DSum("NumericField", "YourTable")
Or with condition
DSum("NumericField", "YourTable", "type = 'Payment'")

Related

view unique values in Combobox using VBA in MS Access

please read my question to end because I test all previous solutions in Stackoverflow and have no answer.
I am trying to display Combobox in the form of an MS Access database using VBA, this Combobox takes its values from one table the problem that I can't display just unique values, Combobox views all values even when I use DISTINCT still view all.
also, I am trying to use GROUP BY but not work.
I know it is a simple problem but how can I solve it?
SELECT DISTINCT Exports_imports_Table.ID, Exports_imports_Table.content FROM Exports_imports_Table;
Assuming the ID field is a unique value (and the content field may have duplicates) you cannot include it in a DISTINCT. Try it like this
SELECT DISTINCT Exports_imports_Table.content FROM Exports_imports_Table;
Does that give you what you expect?
Don't take the ID field in your query builder. Instead of that, select the same field twice, leaving one of them as is, and in another you can give your conditions or formats (if any).
Make sure to change unique values in general properties to "yes".
Save it, and there you are.

Combobox filter on a query not working while Null (in the form only)

I am currently working on a project on MS-Access 2010 and I am struggling with a issue for hours. I would like to apply a filter on a SELECT/PIVOT statement. The result of that query is displayed in a ListBox.
The issue is that no results are displayed when the Combobox is set to Null. But when I select specific values in that Combobox it works perfectly.
The Cbbox filter is declared as a Parameter
My query WHERE clause looks like this :
WHERE (Jobs.fk_group=[Formulaires]![frm_MAIN]![lst_filterGroup] AND (fk_otherCritera='XXX'))
OR ((([Formulaires]![frm_MAIN]![lst_filterGroup]) Is Null) AND (fk_otherCritera='XXX'))
The query WORKS while I enter manually the value of the parameter (=when I enter an empty string, it displays all the records = what I want)
Idk if it is important, but also the listview that I use swap dynamically its recordsource (=it runs 2 differents queries), depending from another Cbbox
I checked the parameters values into my VBA code just before MyListview.Requery calls and IsNull(myCbboxReference) returns True and my other criteria is also OK.
I have no clue of what I did wrong, I need help :-(
Best regards, LR
I would recommend to use special value in combobox for displaying all records in main table and don't rely on comparing with Null values. Your query, probably, doesn't work because empty combobox returns "" , not Null.
Also be careful with queries based on references to controls/parameters. Access has a bug: if you apply filter on data in the form, based on query with such kind references, it stops read new values from controls/parameter during Requery. It appears at least for subforms in Datasheet mode. Workaround for this bug - using function instead of reference.

MS Access: Conditional formatting - highlight duplicates

Is there an expression that I can use in MS Access ,to highlight Duplicate entries in Reports?
I tried something like Expression is : Count(*)>1 but it doesn't work.
Br,
I suspect that the original query will need to be bulked up with a sub-query that has an ID column and count of ID. The outermost query will then need to also return the ID count.
Within the report you'd then need add another field that would show the linked ID count if it was > 1.
Access reporting (and forms) allows conditional formatting to be used in a similar way to excel.
See Ribbon: Report Design Tools>Formt>ControlFormatting...
It will let you change the format of a control depending on the value it, or another control, contains.
It's a very nice feature and will also let you add bar charts to you list forms to graphically represent the values sorted in a control.
However, the data set will need to have a column that indicates whether the current row has duplicate records. The snippet from you current query that you provided (that I repeat below) will not do this:
...OR (((Object.Key) In (SELECT [Key] FROM [Object] As Tmp GROUP BY [Key] HAVING Count(*)>1 )));
Without seeing the whole query I can't really help much, but you will need to remove the use of IN and make the SELECT statement a subquery of the main SQL Statement. The main query resultset will need to be LEFT JOINED to the sub query using the Key field. Because of the LEFT JOIN you can use "isnull(Key)" in the SELECT clause and isnull(Key) will be true for non-duplicate rows.
You can then refer to thiscolumn in your conditional formatting
I hope this makes some sense.
You
For a quick and dirty way to highlight duplicate data:
Select the object you want to highlight if it's duplicated, and make
the background white (or whatever the colour of your background is).
Create a copy of the object that you want highlighted if it's
a duplicate.
Format the copy so it has a highlight, and/or add extra text
(eg: DUPLICATE)
Put the copy behind the original (so it can't be seen).
On the original object, select "Hide duplicates" in properties.
Ensure "Hide duplicates" is NOT selected on the copy.
So when the duplicate appears, Access will hide it, but then the object you've created that was originally hidden beneath now becomes visible - effectively highlighting the field.
(Unfortunately it will only highlight the field itself, not the entire section.)

Access: Workarounds for updating data in not updatable query?

tldr: Can not update records from query because of aggregate functions. What workarounds do you suggest?
I have a table containing decision criteria to which a user can assign a relative weight. I calculate the absolute weight in an SQL query using an aggregate function (as described here Divide the value of each row by the SUM of this column).
qryDecisionCriteria
name relative_weight absolute_weight (calculated)
price 2 50 %
quality 1 25 %
experience 1 25 %
I would like to present the query result in a form, where the user can update the relative weights, and then sees the absolute_weights.
However, the query results are not updatable, because the query involves an aggregate function.
What alternative methods or workarounds could I use, so that a user can edit relative_weights and view absolute_weights as a kind of visual feedback?
I read about temporary tables here http://www.fmsinc.com/MicrosoftAccess/query/non-updateable/index.html but I'm not sure, how to implement this.
Maybe I could also create an additional "edit form" based on a simple query, that is automatically invoked when the user selects a record in qryDecisionCriteria data?
Or maybe just display data from two queries (one updatable, one with the calculated field) next to each other in the form?
Which options would you recommend and why?
Make the Record Source for the form the updatable base query. In the text box which shows the calculated absolute weight set the control source to
=DSum("relative_weight","<base table name>")/Forms!<Form Name>!relative_weight
You'll need to be sure that you do two things with this
When you drag fields onto a form in Access it makes the name of the control the same as the control source column. this is really annoying and can cause a lot of headaches. Rename your control to something like txtColumnName. That way Forms!<Form Name>!relative_weight is guaranteed to reference the field and not the textbox.
in the AfterChange event for the relative_weight textbox you should add an event handler in which the following code is run
txtabsolute_weight.Requery
This will make sure the formula is recalculated whenever someone changes a weight. Otherwise they need to hit F5.

Edit Parameter Field Crystal reports with NOT value?

In Crystal Reports, I want to add a WHERE field <> date to filter out dates that have a NULL value from my database in my report.
I'm using a legacy FoxPro database on the backend which generates an SQL statement from my report, but doesn't appear to have anyway of adding a WHERE clause to the generated statement.
When accessing the FoxPro backend directly, dates with psudo-NULL values have a date of 1899-12-30, but when they are pulled from FoxPro through Crystal they appear as 12/30/99 (which is maybe the same date just displayed in MM/DD/YY format).
I noticed that the report had an existing Parameter Field that prompts the user to filter out the original query down to a specific date range. I tried to add my own in addition to the Parameter Field, but discovered that what I needed with my WHERE field <> date is not an available option since there are only 3 types of Field Parameters mainly:
Discrete
Accept single and discrete values.
Ranged
Accept a lower and upper value in order to select everything in this range.
Discrete and Ranged
A combination of the two above
None of these appear able to filter the results of the query using a WHERE NOT type of clause, is there some other way to do this?
Add this to your record-selection formula:
// remove actual nulls
AND Not(Isnull({table.date_field}))
// remove old dates
AND {table.field} <> date(1899,12,30)
// remove dates not in select parameter value
AND {table.field} IN {#date_parameter}
All I really needed to do was add some criteria to the WHERE clause of the SQL statement, simple enough in an SQL client, but when you're doing this in Crystal Reports v10 it's a bit difficult to find, unless you know what you are looking for...
So what I needed to do was:
Select the field to filter by in the report (in the Details section)
Click the Select Expert button on the Experts toolbar.
In the Select Expert dialog the name of your field should appear in a tab.
Below you can select the WHERE criteria used to filter the records.