Access, lookup for data in another table if matches - ms-access

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'");

Related

Access Junction Table SQL

First and foremost, thank you for your time in reviewing my post and offering your advice. I have a database with the following schema:
tblPR:
PRID
Title
Amount
Status
tblProgram:
ProgramID
ProgramCode
ProgramTitle
tblFund:
FundCodeID
FundCode
FundTitle
There’s a many to many relationship between the previous two tables. Therefore, I setup a junction table.
tblPrgFund:
ProgramCode
ProgramTitle
FundCode
FundTitle
(These are lookup fields to their respective tables so I believe Access pulls their key as well which would be the ids)
Now main form is set to tblPR and subform is set to tblPrgFund. There are two comboboxes in subform for FundTitle and ProgramTitle respectively. Requirement is to filter ProgramTitle based upon FundTitle that is selected by user.
To do this I tried the following SQL command in VBA for gotfocus event of program title combo box. But it is only displaying ProgramID and not ProgramTitle. I don’t even think the ids are correct tbh.
"SELECT ProgramID, ProgramTitle FROM tblPrgFund WHERE FundCode = " & Me.FundCode
If I am understanding your dilemma correctly, you want something like this:
"SELECT ProgramTitle FROM tblPrgFund WHERE FundTitle = " & Me.FundCode
OR (if you wanted to use the FundCode, and not the FundTitle)
"SELECT ProgramTitle FROM tblPrgFund WHERE FundCode = " & Me.FundCode
NOTE: This assumes that your FundCode and FundTitle are unique values in your data. But you shouldn't be grabbing the ProgramId, unless your intention is to use it in some way.
That being said, I would highly suggest you use After Update instead of GotFocus as your event for this type of update. Since this is a drop down box, I would assume you don't want the updates to go just from the initial selection of the Combo Box, but rather start after a selection has been made.

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

DSUM Existing Field in Access Query

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.

Microsoft Access 2010 filtering data based on tempvar

i have a web database and im trying to filter a datasheet, based on the contents of a tempvar. Im trying to use the record source property of the datasheet to do this.
I need to do this because, every employee that logs in should only be able to see a given subset of data in the products table. In the employee table, i have an extra column with a string value which is the data that particular employee should see.
I have a login form that on clicking login, adds this string to the tempvars collection.I can see the tempvar has been added in the immediate window as shown below:
?tempvars!tmpgrpdsc -> "IAMS"
i use the query builder option to complete the record source property as shown below.
The problem is, nothing is returned !
But when i enter the string "IAMS", i get records returned.
However, i have done this with another datasheet and it has worked, the tempvar here held a number ! See below:
What am i missing or is there a better way to filter records based on the login. Thanks
What you showed should work.
However, have you tried to change the criteria to ="""" & [Tempvars]![tmpGrdsc] & """"
Also, to make sure that your tempvar is actually containing the data during the query, you could show it as a field, just to check exactly what data is being returned during the query:
SELECT Orders.*,
[Tempvars]![tmpGrdsc] AS TmpGrdsc
FROM Orders

Access 2010 VBA Form Pulling In Table Data

I have an "Invoices" table, which I run an "Invoices From off of to create invoices for my clients. All my client data; address, hourly rate, etc. is on my "Client Lists" table. The Tables are linked together by a common account/client ID.
I don't want to update general items like hourly rate, address, every time I create an invoice. I am looking to set up an after update event that will auto populate these general items from my "Client Lists" table when I enter the client ID on the "Invoices" form.
---Update---
Below is the code I am working with, and I keep getting an error "Expected: list separator or).
In normal speak; The service type field on my "Invoices" form, should look at my Service type field, on my Client Lists table, for the same Client ID as Account ID.
Private Sub Client_ID_Change()
Service_Type = DLookup("Service_Type", "Client Lists", "Account ID=" & Client ID)
End Sub
Have you tried anything?
The short answer is:
create a query to represent the data you want. In vba use a recordset to run the query's logic for the ID you've got, then display the returned data in the recordset on your form.
However there's no detail here, as you have little yourself
Syntax error on the dlookup function.
To specify a search criteria (last parameter of dlookup) based on a field that contains a blank space, you will have to enclose it in brackets. Also consider that control names can have blank spaces, but to reference them in vba you must replace write them using underscores.
"[Account ID]=" & Client_ID
I would strongly suggest you name table fields and form controls without blank spaces.