Ms-Access 2010 Single quote syntax error - ms-access

I'm trying to create a plant database using MS-Access 2010. I'm trying to open a form by clicking on a button. From what I've researched online, I realize the problem lies with the single quotes, but I don't understand coding enough to figure our what I'm supposed to do to bypass the issue.
Here is the code I'm using.
="[Latin Name]=" & "'" & [Latin Name] & "'"

The last part of the Latin name ("cultivar" says a dictionary) usually is enclosed in single quotes itself. So you cannot simply concatenate the name into a query expression where it is separated by single quotes.
If you have ever heard of SQL injection, that's what is happening here.
You need to escape the single quotes with the Replace() function:
="[Latin Name]=" & "'" & Replace([Latin Name], "'", "''") & "'"

Related

MS Access 365, Using DLookup to look up info on a non combo box

helping my dad program a temp database in the meantime for our HVAC business (we used to use paradox but that mess has finally stopped working).
What he is looking for is the ability to type in a phone number and pull up the customer's name (First name, Last name) and Cust ID (Our identifying value for the customers). We have a lot of phone numbers so a combo box displaying them all would not be ideal. Any ideas?
What I have currently is 5 boxes that have
=DLookup("CustID","CustT","Phone=" & PhoneLookupF)
But they don't seem to update when I change the value on the PhoneLookupF box by typing stuff in.
If object (field, table, query, form, report) names have spaces, then enclose in [ ]. Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention. If Phone is a text field, need apostrophe delimiters, if not text then remove apostrophes from example.
=DLookup("[First name]", "CustT", "Phone='" & [PhoneLookupF] & "'")
=DLookup("[Last name]", "CustT", "Phone='" & [PhoneLookupF] & "'")
=DLookup("[Cust ID]", "CustT", "Phone='" & [PhoneLookupF] & "'")
This will not recalc until PhoneLookupF loses focus and therefore commits the entered value.
Might find the following tutorials interesting:
http://allenbrowne.com/ser-32.html
http://allenbrowne.com/AppFindAsUType.html

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

Access 2010 txt Search Box to Search multiple fields?

I have multiple combo & text boxes to search for different values in my main table on my front end. The code is as follows, just replicated for different types etc. This all works fine.
If Not IsNull(Me.strSearch) Then
strWhere = strWhere & "([tbl_Main.Description] Like ""*" & Me.strSearch & "*"") AND "
End If
My Problem is, I'm trying to create a text box which searches 2 columns simultaneously in my tbl_Main (tbl_Main.LessonsLearnt & tbl_Main.RecommendedAction) but can't figure out how to modify my current code to add another column in, searchable from the same textbox.
Disclaimer: I'm very much a beginner in MS Access - so please keep the explanation as simple as possible :D
If you need any other info - just let me know!
Thanks in advance
strWhere = strWhere & "(tbl_Main.Description Like '*" & Me.strSearch & "*' OR tbl_Main.OtherField Like '*" & Me.strSearch & "*') AND "
This will search for the strSearch being in either Desscription or OtherField. I also replaced your double double quotes with single quotes for better code readability and cross compatibility with other DBMS and removed the brackets that are only needed if you have spaces in your table/field names (something you really should never do anyway).

Errors in DLookup as a control source in access form

I have a text box with the control source of Dlookup function. But the Dlookup format makes me crazy, I tried a hundred times to refer another combo box value as criteria in Dlookup function. I got either "# name?" or "# Error".
I tried this:
=DLookUp("[Contact]","Supllier","[Company]='" & [Forms]![PurchaseOrder]![cboSupplierCompany] & "'")
and got "# Error"
when I input :
=DLookUp("[Contact]","Supllier","[Company]='" & [Me]![cboSupplierCompany] & "'")
I got "# Name?"
I finally found the solution. The right way to use Dlookup in a expression is to use the expression editor to select table field and form control.
the working expression of Dlookup in my textbox is :
DLookUp(" [Supplier]![Contact] ","Supplier"," [Supplier]![Company] ='" & [cboSupplierCompany] & "'")
Using a SQL-query window above-like versions work fine.
But they did not work in the ControlSource property setting. Here you have to use semicolons (;) instead of commas (,). At least in a German language settings environment. The control setting in German is:
Steuerelementinhalt
= DLookUp(" [Supplier]![Contact] ";"Supplier";" [Supplier]![Company] ='" & [cboSupplierCompany] & "'"

MS Access Syntax error

here is my code:
Text22 = DLookup("[Program_No]", "1_Supportive_Housing", "[Program_Name] = '" & Replace([Me.Program_Name], "'", "''") & "' And [BudgetYear] = " & [Me.BudgetYear])
I am not sure what is wrong with it, but it keeps giving me the following error:
Can't find the field | in your expression
I have been trying to get rid of this error but nothing works.
OnKeyPress, this event triggers. As the user writes, it should be able to lookup the value in the table and assign it to text22.
Looks like you want to reference to form fields [Me.Program_Name] & [Me.BudgetYear], but Access tries to find fields with exactly this name, including "Me." prefix. Try to remove brackets at all, or use Me.[Program_Name] and Me.[BudgetYear] instead.