I am trying to create an expression in Access 2016 to return a value from another table when a key is matched from the current table. I am sure it is syntax related, but am at a loss....
I want to add the receipt date (in tbl_RECEIPTS) as a column in tbl_POs based on the key value from the two tables being equal. INNER JOIN doesn't work because if we have not received the item, the field returned by DLookup should be blank.
Here is what I have:
The key is a string value in both tables.
DLookUp('[DATERECEIVED]',"tbl_RECEIPTS"," '[tbl_POs].[KeyVal]' = '[tbl_RECEIPTS].[KeyVal]' ")
The query runs, but returns a blank value for every record.
Any help would be GREATLY appreciated!
It could be:
DLookUp("[DATERECEIVED]","tbl_RECEIPTS","[KeyVal] = '" & [tbl_POs].[KeyVal] & "'")
I found it!
Sorry to answer my own question but What worked was using a LEFT JOIN with the tables.
The result will return blanks in the new date field when no corresponding key value is found.
Related
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])
I have 3 tables, "persons", "per_resi" and "residence"
This three tables form a many to many relation.
Table "person" fields: id, name etc....
Table "residence" fields: id, Street etc.....
Table "per_resi" fields: person_id and residence_id (together principal index)
Well, the problem is when I design a query in the graphic Access tool it Works as it should be.
But if I do in VBA it only return 1 record.
Dim svivienda As String
Dim rvivienda As Recordset
svivienda = "SELECT tbl_persona.Id, tbl_vivienda.Calle, tbl_vivienda.Numero " _
& "FROM tbl_vivienda INNER JOIN (tbl_persona INNER JOIN tbl_perso_viv ON tbl_persona.Id = tbl_perso_viv.Id_persona) " _
& "ON tbl_vivienda.Id = tbl_perso_viv.Id_vivienda WHERE tbl_persona.Id = " & 168 & ";"
Set rvivienda = CurrentDb.OpenRecordset(svivienda, dbOpenDynaset)
I have tried LEFT JOIN and RIGHT JOIN but always the same just one record on the recordset.
Any ideas?
MS access 2013
Thanks in advance.
Thank guys,
This was a very novel question.
Here is the answer.
The RecordCount property does not report the amount of records you have.
The value of the RecordCount property equals the number of records
that have actually been accessed. For example, when you first create a
dynaset or snapshot, you have accessed (or visited) only one record.
If you check the RecordCount property immediately after creating the
dynaset or snapshot (assuming it has at least one record), the value
is 1. To visit all the records, use the MoveLast method immediately
after opening the Recordset, and then use MoveFirst to return to the
first record. This is not done automatically because it may be slow,
especially for large result sets.
Count the number of records in a DAO Recordset
Thanks!!!
Add following statement :
rvivienda.MoveNext
will return the next record of the recordset
or :
rvivienda.MoveLast
will return the last record of the recordset
You will see the result.
The goosie2018's answer is CORRECT. I just show you the simple way to understand.
SUMMARY
So, I think the recordset you get from the database will not show the result look like an Array or a List, but a cursor. And the default cursor points to the first row, so if you use :
rvivienda.RecordCount
you should receive the number of records you actually got.
Sorry for my English ! And thanks for reading.
I've had the same problem as described here: Access 2007 - Left Join to a query returns #Error instead of Null
for which, the solution seems to be; write a better calculated field column so that the error doesn't occur later down the line - described here: Access SQL - given two dates, return the dates of the previous period
However, my calculated column is really simple, only converting a text field to a number field (e.g. "003" -> "3");
RevisionNumber = CDbl([RevisionText])
Further to this, The process works in a left join further up the chain?!
I changed the calculated field to a constant as detailed in the comments on the answer. But this gave me a new error
"Each GROUP BY expression must contain at least one column that is not an outer reference"
Here's the comment:
In my test example, if I switched the calculated field for a constant (e.g. 3 or "Test") then this value did indeed appear for every chain, whether they appeared in the right hand side of the join or not. For a calculated field it returned an error. Only when it was a direct reference to a single field did it work properly. If I do solve it I'll post the workaround here. – Wilskt
So it must be in my SQL code??
SELECT QryDATATRIncompleteRequired.*,_
QryDATATRLatestCompleted.LevelTrained, CDbl([RevisionTrained]) AS [Revision Trained], _
QryDATATRLatestCompleted.TrainerSignOff, QryDATATRLatestCompleted.DateAwarded, _
QryDATATRLatestCompleted.ValidTo_
FROM QryDATATRIncompleteRequired LEFT JOIN QryDATATRLatestCompleted ON _
(QryDATATRIncompleteRequired.EmployeeID = QryDATATRLatestCompleted.EmployeeID) _
AND (QryDATATRIncompleteRequired.SubjectTitle = QryDATATRLatestCompleted.Subject);
Trying to get the RecieptNumber (autonumber) from the most recent record in the table 'Invoices' to store the value in the variable invoiceNum (integer).
Dim rstInvoices As Recordset
Set cdCurrentDatabase = CurrentDb
Set rstInvoices = cdCurrentDatabase.OpenRecordset("SELECT LAST ([RecieptNumber]) FROM Invoices;")
invoiceNum = rstInvoices("[RecieptNumber]").Value
Started VBA programming yesterday so appreciate any help that I will be able to understand.
You'll want to do something like:
SELECT TOP 1 RecieptNumber FROM Invoices ORDER BY RecieptNumber DESC
This will order them so the last record is first in the list, and then it takes the first record. Assuming, of course, RecieptNumber is created in numerical order.
And it's bugging me, so I'll add this - it should be ReceiptNumber, not RecieptNumber...
The string argument to rstInvoices has to refer to a field that is actually returned by executing the Select statement. But the query does not return a field RecieptNumber, but the Last(RecieptNumber) without a specified name. Therefore you first want to give that aggregated column a name using the AS clause:
SELECT LAST(RecieptNumber) AS LastNumber ...
Now you can refer to that field in VBA:
invoiceNum = rstInvoices("[LastNumber]").Value
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