DSUM Existing Field in Access Query - mysql

I am trying to reference a field in my query in a DSUM Expression in the same query but I can't get it to work. Could you please help?
I want my 'Total $ Accrual' column in the below query to sum the 'Amount $' amounts in the 'Accruals Raw Data' table for each Accrual ID from the 'Accruals Master Data' table (as they are displayed in the query when the query runs).
When I run it the query opens an input box window instead.
I originally tried using the below formula but it says the field may refer to the 'Accrual ID' in more than 1 table, therefore I have tried to reference the field within the query using the screenshot instead.
Total $ Accrual: DSum("[Amount $]","Accruals Raw Data","[Accrual ID]='" & [Accrual ID] & "'")
Many Thanks

Try this:
Total $ Accrual: DSum("[Amount $]","[Accruals Raw Data]","[Accrual ID]='" & [Accruals Master Data].[Accrual ID] & "'")
You have 2 tables in query with the same column name [Accrual ID], so Access asked to clarify which table you are referencing to.

Related

How to roll up a column with a query

i have an acess "query" that consist of Drawing-title and Multiple data per ID. And i would like to roll up this data into separate columns. Thanks in advance.
My query looks like this
And would like to change to this
A CROSSTAB requires 3 fields. You need to calculate a third field as a sequence number for each group that will become RowHeader. This will require a unique identifier field in table - autonumber should serve.
TRANSFORM First(Data) AS FirstData
SELECT DWG
FROM tableORquery
GROUP BY DWG
PIVOT DCount("*","tableORquery","DWG='" & [DWG] & "' And ID<" & [ID])+1;

ms Access '16 - Continuous form - OrderBy textbox value created with expression

I have a form that shows the records from Table-A, it is a continuous form. I have a textbox that uses a DCount expression to count records from Table-B that share the same SOP-Number.
=DCount("*","[Table-B]","SOP = " & [SOP])
This works but I don't know how I would go about sorting that column based of the resulting values. The other columns are sorted thus:
" ORDER BY [FIELD NAME] ASC;"
So without a field I don't know how (or if) I can sort the form.
Could I create a RecordSet to store the values then sort by that field (I don't know if this is even possible)
Thank you
Dan
Access 2016 (365)
I'm not quite understanding why you won't know what fields you're pulling from Table B. But you can always order by the column number. So if you're looking to order by the first column, just put:
ORDER BY 1 ASC
You can't sort the recordsource but you can sort the form itself:
Me.OrderBy = "NumTasks DESC"
Me.OrderByOn = True
(assuming your textbox with the DCount control source is named NumTasks ).
You could change the datasource to
select ,DCount('''[Table-B],'SOP=' & SOP) from [Table-A]
order by DCount('*''[Table-B],'SOP=' & SOP)
This is not a good soulution i Table-A is big.

How to concatenate three fields in table into one table?

I would like to concatenate three fields in my MS Access table into one field by using VBA. How can I do this?
I have tried using query to concatenate it and it works, but I would like it to be concatenated and saved in my table instead.
My 3 fields I want to concatenate into 1 field are: CompanyCode,YearCode and PO number.
Currently my table look like this:
Company code YearCode PONumber
ABC 17/ 200
What I want:
PONumber
ABC17/200
Use a query:
Select *, [Company Code] & [YearCode] & [PONumber] As FullNumber
From YourTable
If you insist on having a calculated field in the table itself, use this expression when you - in design view - add the calculated field:
[Company Code] & [YearCode] & [PONumber]
As mentioned #Andre, highly not recommended to use calculated fields in Access tables at all, this feature is quite buggy. But in some cases it's reasonable to store combined code in separate regular field for performance improvement, despite this denormalizes the database structure. You can store combined code in form's BeforeUpdate event:
Me!PONumberFull = Me![Company code] & Me!YearCode & Me!PONumber
Also I'd recommend to do not store "/" in YearCode, just number. In this case the code will be
Me!PONumberFull = Me![Company code] & Me!YearCode & "/" & Me!PONumber

Access, lookup for data in another table if matches

I have a Union query with invoice data like invoice number, supplier and so on. This query is created for the purpose of providing credit note information.
My problem arise when I would like to provide exchange rate for invoices in different currencies. If there is for example RON currency, I need to check currency and date of invoice and then provide value from another table.
I stored currencies and their values in another database. I wanted to use Dlookup function but it works only current database. Not sure what can I do. Is VBA needed here or it can be avoided?
Edit:
Having problem with syntax:
Query:
SELECT [Faktury].InvoiceNumber, [Faktury].InvoiceDate, [Faktury].InvoiceCountry, [Faktury].Currency, DLookUp("Value","Tabela1","Currency1 =" & [Currency]) AS Wyr1
FROM [Faktury];
Dlookup syntax:
DLookUp("Value";"Tabela1";"Currency1 =" & [Currency])
Query has column with Currency used in invoice and Tabela1 has Currency1 and Value. I get error or no value is shown...
To access a a table in another database you can create a link to it:
Go to External Data > Access (although you could use any other type of data source) > choose the database file, and select Link to the data source by creating a linked table.
Then click Ok and select the table(s) you want to link (i.e. use in your database). Now you can use the table (Currency in my example) in your queries or in VBA like a normal table. For example with DLookup in VBA:
MsgBox DLookup("EuroValue", "Currency", "ID='" & InputBox("Currency?") & "'")
or in a (SQL) query:
SELECT EuroValue FROM [Currency] WHERE ID='USD';
or
SELECT DLookUp("EuroValue","Currency","ID='USD'");

Concatenate two columns in an Access table

very simple one. I have got two fields in a table called [First Name] and [Last Name]. I would like to add a new column that is just a combination of [First Name] and [Last Name] - is there a way to do this in Access? I know this can be done in Excel using the Concatenate function.
I would like to do this in the existing table and not in a new query.
Thanks!
As #paxty says, do not do this. You have a simple answer in Access that is not available in Excel, and that is a query. You can base any output that requires that the two names be concatenated on a query.
SELECT FirstName & " " & LastName FROM MyTable
In Access 2010 you can create a "calculated field" for your table; Access applies a formula to create the content of the field. You can enter a formula such as:
FirstName & " " & LastName
You could even do fancier things like have an initial then the last name, if there is a last name, or else show the complete first name, using the Access IIf() and Len() functions.
IIf (Len(LastName) > 0, Left(FirstName, 1) & ". " & LastName, FirstName)
By using a calculated field to join the two names directly in the table... It creates issues down the road. I have two fields First & last name I used a calculated field in the table to join the two together. When you try do a report with those calculated names, it will show only the employee number, not the names. Now I am trying to correct my mistake without having to rebuild the multiple tables I have used this field as a look up in.