Getting data from query using Dlookup - ms-access

I have a query named "TeacherDistributions" includes the following :
TeachertorName Subject Class
and I use the code below to retrieve the teacher name according to value of two text boxes. However, it retrieves no teacher name. Am I mistaken in my code please ?
=DLookUp("[TeachertorName]","[TeacherDistributions]","[Subject]='" &
[txtSubject] & "' AND [Class]='" & [txtClass] & "'")

The code is correct, so either you feed the wrong values as parameters or a record for those parameters doesn't exist.

Related

MS-Access, DLookup won't work while bound to non-ID column

I'm dealing with a Dlookup malfunction, and I've tried everything I can think of with no success.
So I have a combobox that has the ID and grade name as columns. The ID is numerical while the grade name is a mixture of numbers, letters, and a dash. It's trying to pull a decimal from the table to display on the form. I have no problem doing this IF the ID column is bound as opposed to the grade name.
I currently have
=DLookUp("[AD Max]","Grades New","[ID] =" & [Combo24])
which works if the ID column is bound and stored in the table.
However, if the grade name (e.g. B130-2380) is bound, then the field blinks and says #error.
I've tried
=DLookUp("[AD Max]","Grades New","[ID] ='" & [Combo24] & "'")
with no success, and
=DLookUp("[AD Max]","Grades New","'[ID] =" & [Combo24] & "'")
doesn't work either.
Can someone figure out what I'm doing wrong and give me some suggestions? Thank you.

How to evaluate an expression in Access like evaluating a formula in excel

Is there a way to evaluate an expression in Access like we can evaluate formulae in Excel? My Dlookup keeps resulting in the number two and I can't figure out where the hang up is.
=Nz(DLookUp("[RemanChangePoint]![ID]","[RemanChangePoint]","[NewPartNum] Like '*" & [LegacyPN1] & "*' Or [NewPartNum] Like '*" & [LegacyPN2] & "*'"),"")
There's 10 more LegacyPNs and I am expecting to get either the ID of the record that has the LegacyPN or a blank. Before, instead of doing it as above, I was doing:
Like & Chr(34) & "*" & [LegacyPN#] & "*" & Chr(34) & " Or..."
It would result in 2 every time. But now with the shortened version, it's resulting in blanks every time, even though there are records with the part number.
I have a similar expression on a different form, but only one LegacyPN to use as the lookup so it works. I assume that the problem is the numerous Or statements, but it doesn't make sense to me why it's not working, thus the desire for an expression evaluator like Excel has.
Or (and this may be a little uncouth to ask a slightly different question)
Is there a way to use an attachment's file name as the criteria for Dlookup? That may prove more effective than going by LegacyPN, I just can't figure it out via the expression builder.
Your first DLookup where clause is referring to fields on the current form since the double quotes cause the expression to break out of the where clause and into the scope of the ControlSource.
This means something like the following will be passed into the DLookup function because the [LegacyPN] portions of the expression are evaluated in the context of the form, before DLookup is called:
[NewPartNum] Like '*1*' Or [NewPartNum] Like '*2*'
...where 1 and 2 are values of [LegacyPN1] and [LegacyPN2] on the current form.
If you want the [LegacyPN] fields in the DLookup where clause to refer to the [RemanChangePoint] table being queried by the DLookup, then you need to fix your quotes:
=Nz(DLookUp("[RemanChangePoint]![ID]","[RemanChangePoint]", "[NewPartNum] Like '*' & [LegacyPN1] & '*' Or [NewPartNum] Like '*' & [LegacyPN2] & '*'"),"")
Your second syntax should have also worked because the Chr(34) added a double quote resulting in this final string being passed to the DLookup function in the where clause:
Like & "*" & [LegacyPN] & "*" & " Or..."
In which the [LegacyPN] field refers to the table within the DLookup query, not the current form. So I'm not sure why that didn't work. Note: single quotes or double quotes work okay in this context but not a mixture.
Double quotes get tricky within arguments of functions within a CountrolSource property (and other places).

Function DLookup in Ms Access 2016

gives me
when saving records in the system.
Can anyone let me know what is the problem in this?
xx = DLookup("[part_number]", "tblModelCost", "[Customer_ID]= " & me.Customer_ID & " and [Model] = '" & me.[txtModel] & "' and [Description] = 'screen'")
Looks like Me.Customer_Id should be Me!Customer_Id. However, that depends on the form control name and what you are trying to do. Assuming you have a control named "Customer_ID" and a field named "Customer_ID" Me.Customer_ID will try to use the the control value, me!Customer_ID will try to use the underlying bound field value. If you don't have a control named Customer_ID then using Me.Customer_ID will cause the error you are getting. (This is why you want to learn to use controls names like txtCustomer_ID for your controls. )

How to do a DLOOKUP

I am trying to do a DLOOKUP but not sure where I am going wrong.
I have a table header of the date, in my example: 04/04/16
I have a table called tblMasterLeagueAvailability
I have a textbox called text2 with the individuals name in it
=DLookUp("04/04/16","tblMasterLeagueAvailability","[Name] = [Text2] ")
The formula above is not working.
I want it to lookup the table header 04/04/16 in tblMasterLeagueAvailability and show the results for the persons name in text2
Thank you in advance
The textbox reference must be outside the string. Try something like
=DLookUp("04/04/16","tblMasterLeagueAvailability",
"[Name] = '" & Replace(Forms!myForm![Text2], "'", "''") & "'")
You need the single quotes ' for filtering text fields.
The Replace() is there to prevent an error when [Text2] contains a ' itself.
Note: a column name "04/04/16" indicates a problematic table design.

Microsoft Access "if" statement

A very specific question - wondering if someone would simply be able to put together the code for me! I have no experience with code... at all.
I have a table consisting completely of associate information (Name, Phone, ID, Position, etc). I'm building a report on which I only want the Name to display, but ONLY IF their Position = "Tech"
I've come to find this formula for displaying their names in the "last name, first name" format:
=[LastName] & ", " & [FirstName]
but as I said... I only want this script to run if they're a "Tech" how would I write out the "IF" statement to make this work?
The best place for that would be in the query - add "WHERE Position = 'Tech'". Assuming you only want Techs to show up on the report.
If you want everyone to show up but only want names to be visible for techs, you can use an IIf statement: = IIf(Position = "Tech", [LastName] & ", " & [FirstName], "")
That will show the name for a tech, or a blank for anyone else.