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

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])

Related

Combine Rows but concatenate on a certain field in Excel Power Query or Microsoft SQL

I have brought a table from an Authority database into Excel via power query OBDC type, that includes fields like:
Date - various
Comments - mem_txt
Sequence - seq_num
The Comments field has a length restriction, and if a longer string is entered, it returns multiple rows with the Comments field being chopped into suitable lengths and the order returned in the Sequence field as per extract below. All other parts of the records are the same.
I want to collapse the rows based and concatenate the various Comments into a single entry. There is a date/time column just outside of the screen shot above that can be used to group the rows by (it is the same for the set of rows, but unique across the data set).
For example:
I did try bring the data in by a query connection, using the GROUP_CONCAT(Comments SEPARATOR ', ') and GROUP BY date, but that command isn't available in Microsoft Query.
Assuming the date/time column you refer to is named date_time, the M code would be:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
#"Grouped Rows" = Table.Group(
Source,
{"date_time"},
{{"NewCol", each Text.Combine([mem_text])}}
)
in
#"Grouped Rows"
Amend the Source line as required.

MS Access - Query - Replace value

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.

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

Display text corresponding to Combobox selection in Microsoft Access 2010

I have a contact form and one of the fields in a the form is a Contact_Type_ID. This field is a number field which also corresponds to a text field in another table (e.g. 1 = expatriate).
When I cycle through the contacts, their Contact_Type_ID is 1, 2, 3... instead of Non-profit, CEO, Vice-president, etc. This is a problem because one has no idea what number 3 means.
I would like to a combobox that only displays the corresponding text.
I can't get the two columns and 0;1 format to work. My hunch is that it's because I'm drawing information from two different tables. I can generate the correct list, but then the main entry doesn't change as I cycle through the contacts to reflect the current contact's [Contact_Type_ID].
I can't edit any of the current tables because I am supposed to apply this application to a much larger scale database.
I also tried setting the SQL for the row source:
'Populate the connection combo box '
Dim typeSQL As String
typeSQL = "SELECT DISTINCT Contacts.[ContactTypeID], Contact_Types.[ContactType] " & _
"FROM Contacts, Contact_Types " & _
"ORDER BY Contact_Types.[ContactType];"
Me.cbo_ContactType.RowSource = typeSQL
However, I then have the same problem: the combobox won't update as I cycle through the contacts. I don't understand the difference between the rowsource and the controlsource. I feel that this distinction might be key here.
Assuming I understand correctly:
In the combo box properties, go to the Data tab.
Set the Control Source to Contact_Type_ID.
This means that when you go through the records, the combo box will correspond to the Contact_Type_ID field from your data.
Set the Row Source to "SELECT Contacts.[ContactTypeID], Contact_Types.[ContactType] FROM Contacts, Contact_Types ORDER BY Contact_Types.[ContactType];"
The Row Source indicates the data that the combo box has access to, which will help it determine how to display the control source value.
Set the Bound Column to 1
Set Limit to List to Yes
Now in the Format tab. Change Column Count to 2.
Next set the column widths to the 0;1 you mentioned in your question.
Now try looking at form view, and it should behave as you expected.
If it does not work, create a new control with these instructions.
As I understand your question, you have a form with contacts based on a table Contacts and that table contains a field called either Contact_Type_ID or ContactTypeID. You wish to display the description of the contact type from a table Contact_Types.
You do not need to join tables for your combo, it should be very simple. The key, as you suspected, is the Control source, which is the property that relates one table to another.
Control source : ContactTypeID '' or Contact_Type_ID, choose from the list
Row source : SELECT ContactTypeID, ContactType FROM Contact_Types ORDER BY ContactType
Bound column : 1
Column count : 2
Column widths : 0,2
As an aside, you can join tables in a combo and still set the first column to 0 and it will still work, it is all in the correct set up.

one problem in fetching a data from backend in vb.net

i have two item in form it is---1 is textbox and 2nd is combo box.
now i have single value in textbox .
but there is a multiple value for a combobox with is totally depend on a value of textbox....
*i am already add data to the backend files ... in which 1 columns that is called column A it's value is for textbox and 2 column that is called column B it's value for combo box *
When i write in textbox it's dependent ALL value can be get in the combo box.
Can any one solve my problem... It is a row vice search.....
I used a Ms Access for my backend......
How i can create a table that can be i used for a my problem
Give me any reference or codes.
This is probably Category->Product kind of scenario, where ONE Category can have ONE or MORE Products,
Category "Softdrink" can have Products("Coke","Pepsi" etc.)
Category "Harddrink" can have Products("8 PM","BP",etc.)
So when u will put "Softdrink" in your textbox then you want to get the relevant products for it. (Assumed you are using TextChange event)
SQL - select Product.Name from Category Inner Join Product On Category.Id= Product.Category_Id where CategoryName ="Softdrink"
and table structure would be like,