MS Access - Query - Replace value - sql

One of the fields contains ID of partner organisations. Wherever the field is populated it's one of the partner organisations. If the field is blank, it means it's the parent company. In my query, how can I populate the blank records with the ID of the parent company?
The only thing I found was to create an expression in a new field:
SELECT Valid_Learner.LearnRefNumber, Valid_Learner.FamilyName, Valid_Learner.GivenNames, Valid_Learner.DateOfBirth, Valid_Learner.Ethnicity, Valid_LearningDelivery.FundModel, Valid_LearningDelivery.PartnerUKPRN,
Replace([PartnerUKPRN],"","parentComanyID") AS UKPRN,
Valid_LearningDelivery.DelLocPostCode, Valid_LearningDelivery.LearnAimRef
FROM Valid_Learner RIGHT JOIN Valid_LearningDelivery ON Valid_Learner.LearnRefNumber = Valid_LearningDelivery.LearnRefNumber;
The new field ("UKPRN") should (for the purpose of this question) have the string "parentCompanyID" whenever it's blank in the PartnerUKPRN field.
The result is the UKPRN field displays IDs where the ID is present in the PartnerUKPRN field, but shows "#Error" where it's blank in PartnerUKPRN.

If "blank" means NULL, then you can use NZ() in MS Access:
NZ([PartnerUKPRN], "parentComanyID") AS UKPRN
This would be the "normal" way to write this logic. I don't know why REPLACE() would return an error.

Related

Using Wildcard in query criteria when referencing form input

I am attempting to use a form to initiate and return query results based on user selected criteria (via a series of 24 combo boxes). So, if the user selects from a drop down, the subform requeries and returns results from query with the selected value as a filter. I need the user to be able to select as many or as few criteria as desired. I have that piece working using the following:
In the query that populates the subform, each criteria has:
Like "*" & [Forms]![formname]![cobx_name] & "*"
Like I said, this works. The problem lies in one of the criteria. There is a field that contains a sequential numeric value (from 1 to over 11,000). When I select, say 7, the query returns records with numerical value 7, 17, 27, 37, and so on. I need for the query to use wildcard and return all records when that specific combo box is null, but return only the one record that is equal to the value (i.e. just the record with value 7 for example) that the user selected.
LIKE and wildcard is intended for text not numbers. If you must use dynamic parameterized query, consider this:
BETWEEN Nz([Forms]![formname]![cobx_name], 0) AND Nz([Forms]![formname]![cobx_name], 99999)

Calculating the number of like records in an unbound text box on a form. in MS Access 2016

I have an unbound text box [txt_AmendmentOF] on a form to count the total number of Amendments a specific record has in the database. So the user knows which amendment they are on. Just a simple you are on Amendment 3 out of 8. i have this calculation i am using in the Control Source field of the text box [txt_AmendmentOF]:
=IIf(IsNull([txt_Amendment]),0,Count([MIPR_Number]))
there is a Field on my table called [Amendment] its text box on the form is called [txt_Amendment]. I have the calculation return a zero in [txt_AmendmentOF] if [txt_Amendment] is null. I need it to count the record number [MIPR_Number] that are the same and return the total number of Amendments count in the [txt_AmendmentOF] text box. on my table i have private key field called [ID] it is an autonumber format. The problem with the code above is it is counting all the fields that have a [MIPR_Number] and returning the total row count of that. i am not an expert here so any help would be greatly appreciated.
Update to post..
I have also tried this and got an error in the text box
=IIf(IsNull([txt_Amendment]),0,Count([MIPR_Number]=[txt_MIPR_Number]))
Counting by a condition is one of the basic dataabase tasks. To get answers from a db, you have to query the database (and that is done by createing a query).
To count same[MIPR_Number]you have to group the data (a group contains same numbers), then count.
SELECT COUNT(id) as CountOfSameNr, [MIPR_Number] FROM Table GROUP BY [MIPR_Number]
Store this query as e.g CountMIPRNumber.
Now you have two options:
Use a Dlookup to get one value
=IIf(IsNull([txt_Amendment]),0,Dlookup("CountOfSameNr","CountMIPRNumber","[MIPR_Number] = " & [MIPR_Number]))
or add the query to form recordsource (join on [MIPR_Number], "SELECT * FROM TABLE" is forms query)
SELECT CountMIPRNumber.CountOfSameNr, TABLE.* FROM TABLE LEFT JOIN CountMIPRNumber ON TABLE.[MIPR_Number] = CountMIPRNumber.[MIPR_Number]
and reference the count field
=IIf(IsNull([txt_Amendment]),0,[CountOfSameNr])

Filter a dropdown based on the content of another dropdown in PowerApps

I need to filter a dropdown list from the results of another dropdown list above it. I'm filtering personnel based on their respective agency, so when the user selects their Agency from the first dropdown list, the second dropdown list is only populated with Personnel from that Agency.
Agency and Personnel are two separate entities (tables) in the common data service (CDS).
So far I can get the list of Agencies from the CDS with
Filter(Agency, AgencyType = 9)
where [9] = the type of agency I'm filtering for.
I just can't get the list of people assigned to that agency to populate. I have tried:
ITEM: Filter(Personnel, ddAgency.Selected.Value in Personnel.AgencyID)
where ddAgency is the name of the referenced dropdown.
I am getting a single table error from PowerApps, so I guess it's my syntax. I'm trying to filter data and draw results based on values in 2 entities (tables).
Any suggestions?
You probably want something along the lines of
ddPersonnel.Items: Filter(Personnel, AgencyID = ddAgency.Selected.Value)
The expression used to filter the data source already assumes that you are in the context of the data source being filtered, so you don't need to specify Personnel.AgencyID - AgencyID is enough.
The expression on the right of the equality sign (ddAgency.Selected.Value) may need to be updated, if the column that has the agency id is not called Value. For example, if it's called Id, the expression would be the one below.
ddPersonnel.Items: Filter(Personnel, AgencyID = ddAgency.Selected.Id)
You can read this as "Filter the data source Personnel where the value of the AgencyID field is the same as the value of the Id of the Agency element selected in the dropdown ddAgency.

Multisearch form for a query in access

I should make a query in Access that have 4 criteria. If I Run this query by the structure view of the query it works. Then I built a form to insert the criteria in 4 text boxes and get more easy the use of the query. I create the form using the tutorial on the official site of microsoft 1; i tried first the query with only one text box and one criterium and it works; when I use 4 text box, following the tutorial, it doesn't work. The criterium that I use for each field in the query is the follow:
Switch(Not IsNull([Forms]![frmRICmp]![cod]),[Forms]![frmRICmp]![cod])
I tried to use also
IIf(IsNull([Forms]![frmRICmp]![cod]), Like "*", [Forms]![frmRICmp]![cod])
but also in this case it doesn't work.
can someone tell me the right instrtuction to use in the query's criteria
tnks
So if I gather correctly you need to perform a multi search where if any of the boxes are null you would like to return all the values. and more than one text box can be used simultaneously. TO do this you have to amend do the following.
Amend the Query Field (Note i'm referring to field and not criteria)
For the first Text Box Assuming name is COD and Field Name is also COD
If the Current field name is COD insert another field with the same name and amend to
[COD]=[Forms]![frmRICmp]![cod] OR [Forms]![frmRICmp]![cod] Is NULL
then in the criteria field use the following value
TRUE
For the second Text Box Assuming name is COD2 and Field Name is also COD2
If the Current field name is COD2 insert another field with the same name and amend to
[COD2]=[Forms]![frmRICmp]![cod2] OR [Forms]![frmRICmp]![cod2] Is NULL
then in the criteria field use the following value
TRUE
and continue the same process for all 4 text boxes.

Use tekst from table depending on number in other table using Access

I feel stupid asking this, but I really need an excample on how to get a value of a field in one table (in the end in my report) depending on a value of a field from an other table in Access.
So I have (for excample) a table:
Products and in my report I do a formule using the value of price (field of Products) and adding to that I must have the value of the field VAT-Type (a nummeric var, in the table VATS) depending on what is there in the record (of the one in the table Products) in the field VAT-Sort, also a nummeric var that must meet one of the values used in the field VAT-Type).
So in the report I must have something like:
Product: X Count Price'=(price+21%)'
where 21% comes from the dependensy between the field VAT-Type and VAT-Sort.
I know I can do something like result=select 'VAT-Sort' from 'VATS' WHERE 'VAT-Sort' = or equals 'VAT-type'
But how do I use it in a report of Access to get the right result?
You can use DLookUp:
Numeric data type:
DlookUp("Value","Vats","Vat_Type=" & Vat_Sort)
Text data type:
DlookUp("Value","Vats","Vat_Type='" & Vat_Sort & "'")
Or you can base your report on a query, say:
SELECT Value, Other, Field, names FROM Products
LEFT JOIN Vats
ON Products.VAT_Sort = Vats.Vat_Type
Edit re comments