Query the MS Access table for the id entered in textbox - ms-access

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.

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

populating field data by Dlookup in access

I'm trying to populate data from a query (SUMA PALETS) that counts the records from a field (its related table is called "LOTES PRODUCTOS") to a field in a subform (PEDIDOS PRODUCTOS). Both, query (and its related table) and subform (and its related table) have a common field with the same value. This field is called "PRODUCTOPEDIDO" in SUMA PALETS query and in LOTES PRODUCTOS table and it is called "IdPEDIDOSPRODUCTOS" in the subform.
I'm using the next formula in the control source of the subform field (it's called "QUEDAN"):
=DLookUp("[CountofIdPEDIDOSPRODUCTOS]";"[SUMA PALETS]";"[PRODUCTOPEDIDO] = " & Forms![PEDIDOS PRODUCTOS]![IdPEDIDOSPRODUCTOS])
Where:
SUMA PALETS: It's the query name
PRODUCTOPEDIDO: table field I want to count
CountofidPEDIDOSPRODUCTOS: it's the query field that counts PRODUCTOPEDIDO field in the query
PEDIDOS PRODUCTOS: subform name
IDPEDIDOSPRODUCTOS: subform field name with the same value as - PRODUCTOPEDIDO in query.
But I get #Name? error in the subform field. I don't know where is the problem. Could It be because the field's name is the same in another tables?
I would like that QUEDAN field was updated instantly when I add records in LOTES PRODUCTOS field (by introducing data in a subform). I don't know If It`s the right approach or could It be better option If I write a query to get count values directly in QUEDAN field (I don't know much about queries in SQL).
Thanks in advance
Should be:
=DLookUp("[CountofIdPEDIDOSPRODUCTOS]";"[SUMA PALETS]";"[PRODUCTOPEDIDO] = " & Forms![ParentFormName]![PEDIDOS PRODUCTOS]![IdPEDIDOSPRODUCTOS])
You need to refer to the parent form before the subform.

how to populate text field once a comboBox change in 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.

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