Db Search - Multiple conditions - lotus-domino

I am trying to export Documents from a Lotus DB. I have used the Db.search functionality and arrived at below code. However, I want to include 2 conditions/functions - #Contains & #Created together. I am getting Formula error. Any help is much appreciated.
Set GlobalCollection = db.Search("#Created > [01/01/2019]" & " " & "#Contains(" & "App1" & ";" & """Approved""" & ")", Nothing, 0)

The escape symbol for LotusScript is a backslash, \. LotusScript allows you to use more than just double quotes to wrap Strings. You can use curly braces ({...}) or pipes (|...|). This may make it more readable and easier to trubleshoot. There's also no need to have separate strings for each individual piece, which will again minimise risk and help readability. There may have been a mistake with each of those, I'm pretty sure you're missing an ampersand. It's much easier to troubleshoot with fewer strings.
So this should work:
Set GlobalCollection = db.Search({#Created > [01/01/2019] & #Contains(App1;"Approved")}, Nothing, 0)

Related

Dsum function not working with Text field

I've tried just about everything i can think of on why i would get this error, but i have had no luck. I wrote a similar code that references that same table with numerical values that works fine, but when searching for text it has problems. The error code says the missing operator lies here: [ExpendetureStore] = 'Lowe's
TotalCostTextBox = DSum("[ExpendetureCost]", "ProjectExpendetures", "[ExpendetureStore] = '" & Me.StoreNameCombo & "'")
Lowe's has an apostrophe in its name. Access query engine is reading that apostrophe as a special character (text delimiter) in the compiled search string. If your data includes apostrophes, one way to deal with is to 'escape' the character - double it in the data with Replace() function. This forces Access to treat the character as normal text.
TotalCostTextBox = DSum("[ExpendetureCost]", "ProjectExpendetures", "[ExpendetureStore] = '" & Replace(Me.StoreNameCombo, "'", "''") & "'")
The same will happen with quote marks and are more challenging to deal with. Note the escaping of quote between quotes.
Replace("somevalue", """", """" & """")
Or may be easier to understand using Chr() function.
Replace("somevalue", Chr(34), Chr(34) & Chr(34))
Side note: Expendeture is a misspelling of Expenditure.

How to build proper Access SQL LIKE operator expression?

I'm attempting to have a user search through a table in Microsoft Access 2010, but the SQL command isn't working. The command that loads and refreshes the table is this:
SELECT Equipment.equipmentID, Equipment.equipmentName, Equipment.model,
Equipment.make, Equipment.equipmentLocation FROM Equipment ORDER BY Equipment.equipmentName;
This works, but when I try to use a variable (or any normal criteria):
searchItem = Me.searchBox.Value
Me.List64.RowSource = "SELECT Equipment.equipmentID, Equipment.equipmentName,
Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment
WHERE Equipment.equipmentName LIKE '%searchItem%' ORDER BY Equipment.equipmentName;"
I've also tried something like "%10%" instead of the searchItem variable, but the command has the table come up blank with no errors. I suspect the problem is with the Equipment.eqiupmentName as the column name, but I can't quite figure out what's wrong here.
Here's a quick look at what the table looks like:
Try this:
Me.List64.RowSource = & _
"SELECT Equipment.equipmentID, Equipment.equipmentName," & _
" Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment" & _
" WHERE Equipment.equipmentName LIKE '*" & searchItem & "*'" & _
" ORDER BY Equipment.equipmentName;"
User rjt011000 has a valid solution, but I recommend using & for string concatenation in VBA (and Access). For an explanation of + and & see this thread.
Access will not recognize or substitute VBA variables inside an SQL statement. Furthermore, the LIKE operator is fed an SQL string value in this case (inside single quotes... which are inside the double quotes), so even if a VBA variable could be referenced directly inside SQL, Access does not interpret any such thing inside a string value.
Regarding the Access SQL LIKE operator, the multi-character matching pattern is * rather than %. Access also recognizes the operator ALIKE which does indeed honor the ANSI pattern %. See LIKE operator docs and this thread regarding ALIKE.
To be more thorough, the string delimiters and LIKE pattern-matching character should be escaped if you don't want the user inadvertently injecting invalid characters that cause errors in the SQL. Following is an example of escaping a couple of them. There are more elegant ways to handle this for all special characters, but the code and technique are beyond the scope of this answer.
...'" & Replace(Replace(searchItem, "*", "[*]"), "'", "''") & "'...
For the record, although Access SQL will not substitute a VBA variable, it will recognize and call a public VBA function. Normally such a public function must be defined in a normal module, but in context of a form's Record Source query, a form-module method can sometimes be called.
One last technique... It is possible to reference a form control's value directly in SQL. This can be very convenient and reduce extra code, but there are a couple caveats:
The form must of course be open, otherwise Access will interpret the reference as an unknown parameter and display a prompt. This will of course not be a problem if the SQL is always in context of the same form.
Access will sometimes automatically refresh the query when such a referenced control is changed, but it is not always guaranteed. The "timing" of automatic refreshes might not be immediately intuitive. You can call the Refresh method on the control or subform from various form events to force the query to refresh after the value is changed.
Notice that in the following example, the string concatenation is inside the VBA string, so that the concatenation actually happens in context of SQL and not beforehand like in the first code snippet. There is no problem with this, just something to consider since this entire answer revolves around proper string interpretation and concatenation.
But really, the same concern exists for un-escaped pattern-matching characters in the user text. Rather than making the SQL text long and ugly with calls to Replace(), instead create a custom function (e.g. EscapePattern()) that does this for any text and then wrap the control reference with that function. The example does this, although I don't include the code for the special function. Such a function could also be used in the first VBA code snippet to simplify building the SQL text.
Me.List64.RowSource = & _
"SELECT Equipment.equipmentID, Equipment.equipmentName," & _
" Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment" & _
" WHERE Equipment.equipmentName LIKE ('*' & EscapePattern(Forms![Form Name]![Control Name]) & '*')" & _
" ORDER BY Equipment.equipmentName;"
There is always more! Did you see the VBA line continuation in my example? It makes the SQL text much easier to view within VBA editor.
I suspect you are not setting your searchItem variable correctly in the SQL string. I am not too familiar with access string concatenation but try separate the searchItem out of the SQL string and then checking if your RowSource has the value you suspect.
Me.List64.RowSource = "SELECT Equipment.equipmentID, Equipment.equipmentName,
Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment
WHERE Equipment.equipmentName LIKE '%" + searchItem + "%' ORDER BY Equipment.equipmentName;"

Is it possible to replace " (double quotes) with a white space in a string?

At some point of execusion of my project, the input to the database is in the format:
Buy Requirement of "xxxxxx & xxxxxx" through mycompany.com
It results in an incorrect SQL syntax error. I need to replace " with white space. I searched in google but no helpful suggestions were there.
How to replace " with whitespace?
Dim str As String
str="Buy Requirement of "Telemarketing & ERP Software" through IndiaMART.com"
' TODO: perform replace
' result
str = "Buy Requirement of Telemarketing & ERP Software through IndiaMART.com"
I finally understand what you are asking now...
Here, this is what you want to be using: teststring.Replace(""""c, "")
If you really want to use Linq and extension methods, then use this: New String(teststring.Where(Function(c) Char.IsLetterOrDigit(c) OrElse Char.IsWhiteSpace(c)).ToArray()).
But that's just making things complicated for no reason.

Using a Dlookup in Access VBA when the field has a single quote

I've found a lot on this topic but still can't seem to get it to work. I have the following line of code:
If isNull(DLookup("id", my_table, my_field & "='" & temp_value & "'")) Then
The problem is a value in my_field of my_table is "O'Connell" (with a single quote), and I'm not sure how to get Dlookup to find it. I've tried using:
my_field & "=" & chr(34) & temp_value & chr(34)
And a host of other multi-quote options, but I just can't seem to get it to work. Though I can use VBA to modify the temp_value to include or not include the single quote, since the single quote already exists in the table, I need to make sure it matches. I'm just not sure how to tackle it.
Though the suggestions and answers here do work and resolve many issues with quotes in text, my issue ended up being related to the character I was seeing as a single quote not really being a single quote. For what it's worth, the data I was using was exported from Siebel, and the single quote I was seeing was actually chr(146), where a regular single quote (I say "regular" for lack of a better term) is chr(39).
If having issues with quotes, I found it helpful to examine the chr values of each character in the string. There may be a better way to do this, but this loop should help:
for i=1 to len(a_string)
debug.print mid(a_string,i,1) & " - " & asc(mid(a_string,i,1)
next i
The asc function gives you the chr code for a character, so this loops through the string and shows you each character and its associated chr code in the Immediate window (using debug.print). This also helps in finding other "hidden" (or non-visible) characters that may exist in a string.
Once discovered, I used the replace function to replace chr(146) with two single quotes (two chr(39)s), as suggested by HansUp, and that worked perfectly.
In this example, my_table is the name of my table and my_field is the name of a field in that table.
Dim strCriteria As String
Dim temp_value As String
temp_value = "O'Connell"
' use double instead of single quotes to avoid a '
' problem due to the single quote in the name '
strCriteria = "my_field = """ & temp_value & """"
Debug.Print strCriteria
If IsNull(DLookup("id", "my_table", strCriteria)) Then
MsgBox "no id found"
Else
MsgBox "id found"
End If
If you prefer, you can double up the single quotes within the name. This should work, but make sure you can distinguish between which are double and which are single quotes.
strCriteria = "my_field='" & Replace(temp_value, "'", "''") & "'"

Converting characters to ASCII code

Need help with reading special characters within my VB code. ASCII code Char(34) = " works fine but Char(60) = < and Char(62) = > are not being read.
My Code
node.FirstChild.InnerText = Chr(60) & "httpRuntime executionTimeout=" & Chr(34) & "999999" & Chr(34) & " maxRequestLength=" & Chr(34) & "2097151" & Chr(34) & "/" & Chr(62)
Without ASCII Code
'node.FirstChild.InnerText = "<httpRuntime executionTimeout="999999" maxRequestLength="2097151"/>"
Are you trying to modify a Config file? Try:-
node.FirstChild.InnerXml = "<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
Note all that Chr marlarky is unnecessary, were you trying to avoid < and > being encoded as XML entities?
Maybe this doesn't answer your question, but you could use two double quotes to escape the quotes character in VB.NET:
node.FirstChild.InnerText = _
"<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
I'm just guessing: you could use the String.Format method for your purposes:
node.FirstChild.InnerText = _
String.Format( _
"<httpRuntime executionTimeout=""{0}"" maxRequestLength=""{1}"" />", _
timeoutValue.ToString(), reqLenValue.ToString())
You'll need to give more information about how you're "seeing" the results. In my experience, problems with this are as likely to be about viewing strings in the debugger as getting the right strings in the first place.
I don't really see why you need to use Chr(60) etc at all, other than for the quotes. What happens when you just use < and > in your code?
I strongly suggest you dump the string out to the console rather than using the debugger - the debugger tries to show you how you could represent the string in code, rather than showing you the contents verbatim.
Of course, if this is XML then I'd expect serializing the XML out again to end up escaping the < and > - again, more information about what you're trying to do would be helpful. The absolute ideal (IMO) would be a short but complete program demonstrating the problem - a small console app which does one thing, and a description of what you want it to do instead.