how to populate text field once a comboBox change in ms access - ms-access

I do have to tables that are connected with a relationship. I want to display a Name from the other table once the the combo box change. For example, I have table1 where it has table2ForeignKey and I want to populate a text field in a form using the the table2Name I create a query but I dont know to to connected with the combo Box cause I want the Id of the combobox to be pass to the query then the query to validate the combo box Id and return the value to get a clear Id the query I built:
SELECT Colleges.College_Name
FROM Users INNER JOIN Colleges ON Users.college_id = Colleges.college_id
WHERE ((([Combo37])=[Users].[User_Id]));
SO I have table users and in the users there is field of the college ID and I want to get the college name using its foreign key by validating the join method and the query by its self it works with no issue but I am not sure how to let the query read the user idea input from the combobox cause every time I open the query it read(combo37) as just a variable(knowing that combo37 is the name of the combobox) and it ask me to input the combo37 manually so my question is how to pass a value from a combobox to a query and how to let the textfield to be populated using this query.

Your query doesn't know what [Combo37] is that's why when you open the query it askf for a value.
You need to tell the query where/what the [combo37] is. Try something like
SELECT Colleges.College_Name
FROM Users INNER JOIN Colleges ON Users.college_id = Colleges.college_id
WHERE (
[Users].[User_Id] = Forms!formName![combo37]
);
so the query knows which form look for to find the combo37 object.
Make sure the form is open before you run this query.

Related

I did a query in access that calculates a date, when I enter the date in a form I get the result in the query but not able to save it to the table

Pic hereI am new to MS Access. I have a customer table with creationdate field as well as submissiondate. The submissiondate was calculated via a query since it carries multiple conditions. I created a form from the customer table where I am able to input the creationdate field and I managed to show the submissiondate (from the query) via Dlookup but the date is not recorded in the customer table. How can I record the submissiondate value from the query in the customer table without having to do an update query every time I add a new customer? I did an update query but it updates all records every time it runs, and we have more than 50K customers. Any help is appreciated.
this is the query in Access that gets the result for me.
SELECT HardDeadlineCalculationQ1.ID, HardDeadlineCalculationQ1.R AS ReferralDate, HardDeadlineCalculationQ1.[Source of Referral], HardDeadlineCalculationQ1.HD1, HardDeadlineCalculationQ1.wdhd1, HardDeadlineCalculationQ1.hd2, HardDeadlineCalculationQ1.wdhd2, HolidaysT.holidaydates, Switch([HardDeadlineCalculationQ1].hd2=holidayst.holidaydates,"Yes") AS isholiday, IIf(isholiday="Yes",([HardDeadlineCalculationQ1.hd2]-1),[HardDeadlineCalculationQ1].hd2) AS hd3, WeekdayName(Weekday(hd3)) AS wdhd3, Switch(WeekdayName(Weekday(hd3))="Monday",(hd3),WeekdayName(Weekday(hd3))="Tuesday",(hd3),WeekdayName(Weekday(hd3))="Wednesday",(hd3),WeekdayName(Weekday(hd3))="Thursday",(hd3),WeekdayName(Weekday(hd3))="Friday",(hd3),WeekdayName(Weekday(hd3))="Sunday",(hd3-2)) AS hd4, WeekdayName(Weekday(hd4)) AS wdhd4
FROM HardDeadlineCalculationQ1 LEFT JOIN HolidaysT ON HardDeadlineCalculationQ1.hd2 = HolidaysT.holidaydates;
regards,
"The submissiondate was calculated via a query since it carries multiple conditions"
By this, i presume you mean that the submission date is a calculated field(as its control source)
Do the following
Copy the calculation, i.e formula and put in vba code, under the after update event of every field that is a variable in the expression.
e.g submissiondate= place the formular here
Then on the form remove the calculation , i.e formula from the control source of the form control and make a field in the table the control source, e.g submissiondate.

Can I use DCount to populate a textbox with criteria from a different table?

I have been having much trouble with this problem. I have a table in Access called "Import" that I import records to. Each record has a facility name that corresponds to a "region" from a different table called "COID_Lookup". I'm trying to get a count of records in the "Import" table based on criteria from the "COID_Lookup" table. Is this possible? Also, I have a query that already does this perfectly but I understand a textbox value cannot be based on a query.
This is what I've tried =DCount("Facility","tblImport","tblCOID_Lookup.Region = 'Midwest'")
My output is #Error in the textbox that blinks as if it is caught in an endless loop.
The query I have, Midwest_Count, works as expected, but I don't know how to put that in the expression. I have tried to look it up but the answers don't make sense to me. I'm sorry.
The solution I used is a DLookup of the query I had that worked.
=DLookUp("CountOfFacility","qryMidwest_Count")
This is the query.
SELECT Count([tblImport].Facility) AS CountOfFacility FROM tblCOID_Lookup INNER JOIN tblImport ON tblCOID_Lookup.[Facility] = tblImport.Facility WHERE (((tblCOID_Lookup.Region)="Midwest"));
Yes, you can do that, as DCount will accept an SQL criteria value:
=DCount("*", "tblImport", "[Facility] = (Select [Facility Name] From tblCOID_Lookup Where [Region] = 'Midwest')")

Searching a database in Report Builder 3.0

I am not sure if this is possible in Report Builder or not. I want to put a text box in a Report Builder report and then search the database for a specific person and display that in a subreport. Is this possible and, if so, how would you suggest I do it? I have tried researching it online without success.
This part will produce a parameter into which your user can type whatever they want
You need to set up your report to use parameters. You can set these parameters up to require user input either from manual entry or by picking from a pre-defined list.
Assuming you are returning your data using a SQL query, you can then reference these parameters in your dataset script. If for example you had a parameter called FirstName and another called Surname, and you only wanted to return values in your data set that matched both exactly, you would reference these parameters like so:
select PersonID
,FirstName
,Surname
,OtherDetails
from PersonTable
where FirstName = #FirstName
and Surname = #Surname
If you would rather have more of a 'search' type function, you can use the SQL like operator and wildcards, though bear in mind this will have a potentially very detrimental effect on your query performance:
select PersonID
,FirstName
,Surname
,OtherDetails
from PersonTable
where FirstName like '%'+#FirstName+'%'
and Surname like '%'+#Surname+'%'
This part shows you how to change that parameter so it provides a drop down menu. This part is optional.
If you want to provide a list of available options to select from, you can create a parameter that has a list of 'Available Values'. These can either be manually typed in by yourself - hard coding them in to the report design - or you can make them data driven by basing the parameter on a second dataset.
To get that list of people, you would want to return the ID of the person you are looking for as well as the details that are end-user friendly to be visible in the report:
-- select distinct so we have no duplicates
select distinct PersonID as Value -- Value is what is used to the query. This ideally will be a uniquye identifier
,FirstName
+ ' '
+ Surname as Label -- Label is what the user sees. This can be a bit more verbose and detailed
from PersonTable
order by Surname -- Specify how you want the options ordered
,FirstName
If you set this dataset as the source by selecting Get Values From A Query in the parameter options, you will see the drop down list appear when you run the report. Users can then select one, click Run Report and have their selection impact the data that is returned.

Query the MS Access table for the id entered in textbox

I have two tables Order and Customer.
In the order form, if I enter the value for the CustId field, I need the value for CustDesc to come automatically with reference to 'Customer' table.
The remaining fields in the 'Orders' form, I have mapped to the fields in the order form directly. I am stuck only in mapping to the 'Customer.CustDesc' to Orders.CustDesc. Because I dont know how to query like below
select custdesc from Customer where [custId= the value of CustId in Orderform]
I am new to MS Access form. pls help.
You can use the lookup function:
= DLookup("[custdesc],"[Customer]", "[custId] =[Forms]![Orderform]![CustId]")
In VBA on the AfterUpdate event:
(in orderform)
CustId_AfterUpdate
[Forms]![Orderform]![CustDesc] = Nz(DLookup("[custdesc],"[Customer]", "[custId] =[Forms]![Orderform]![CustId]"))
- By setting the control source property of the desired field to the same statement:
= DLookup("[custdesc],"[Customer]", "[custId] =[Forms]![Orderform]![CustId]")
It might be easier to use a query that joins both tables as your recordsource for the form instead of your orders table. That way you don't need to come up with this kind of construct to find one other value specifically.
Query:
SELECT O.*, C.custdec as CustomerDesc
FROM Order O left join Customer C
ON O.custId = C.custId
If you also need to be able to edit/add data to those tables from this form, you'll need to set the RecordsetType property to 'dynaset'.
Another option would be the use of subforms (customer data in the main form, data from different orders in the subform), but that depends on what the form's intended purpose is.

MS Access Query using IFF to compare values

I am trying to build a query which will look at the data in two fields in two different tables and check to see if the data is the same, if it is I want it to return the number of times it is matched, if it isn't I simply want it to return the text saying "No viewings".
I have constructed this query in my access database which has the field from the first table "Property" and the second field I want it to compare the data with, "Viewings". I have build the following expression using the build tool, however I am stuck to make it work since every time I get this error message when trying to run the query: "Your query does not include the specified expression 'Property Viewed' as part of an aggregate function."
totalViewings: IIf([Viewings]![Property Viewed]=[Property]![ID],Count([Viewings]![Property Viewed]=[Property]![ID]),"No Viewings")
Any help how to overcome this error would be very appreciated.
Thanks
I would suggest doing something like this:
1) Assuming this is something you are developing yourself, make sure your data structure is all in order first. Since I dislike relatively code-hostile identifiers, I'd have the tables as so -
Properties - PropertyID (AutoNumber, primary key), HouseNumberOrName, Street, etc.
Viewings - ViewingID (AutoNumber, primary key), PropertyID (Number/Long Integer), ViewingDate, etc.
In the Relationships view, Properties.PropertyID would then be set up to point to Viewings.PropertyID in a one-to-many relation.
2) Your actual query I would then break into two, the first to compile the data and the second to format it for display. The first would go like this, saved as ViewingCounts...
SELECT Properties.PropertyID, Count(Viewings.PropertyID) As ViewingCount
FROM Properties LEFT JOIN Viewings ON Properties.PropertyID = Viewings.PropertyID
GROUP BY Properties.PropertyID;
... and the second like this, saved as ViewingCountsForDisplay:
SELECT Properties.*, IIf(ViewingCount = 0, 'No viewings', ViewingCount) AS Viewings
FROM Properties INNER JOIN ViewingCounts ON Properties.PropertyID = ViewingCounts.PropertyID
ORDER BY Properties.PropertyID;