How to define quotation mark as constant in Visual Basic - visual-studio-2010

Simply I'm trying to define the quotation mark (") as a constant in Visual Basic. However, VB automatically inserts another ", hence I can't make the char equals to an ".

You can also use the ASCII character (34) in Visual Basic like
TextBox1.Text = "She said, " & Chr(34) & "You deserve a treat!" & Chr(34)
You can also define a constant for the character, and use it where needed.
Const quote As String = """"
TextBox1.Text = "She said, " & quote & "You deserve a treat!" & quote
Taken from How to: Put Quotation Marks in a String

Two quote marks make an escaped quote mark.
variable = """"

Related

The system cannot find the file if its path/name contains a space

Path = split(wscript.scriptFullName, wscript.scriptname)(0)
CreateObject("wscript.shell").run(Path & "Name.txt")
The above script works fine if both the file path and file name contain no spaces.
If either contains a space, the result will be;
Error: The system cannot find the file specified.
How can I fix the error?
The rules are fairly simple:
All strings have to start and end with double quotes to be a valid string.
Dim a
a = "Hello World" 'Valid string.
a = "Hello World 'Not valid and will produce an error.
Any use of variables must use the String Concatenation character & to combine them with strings.
Dim a: a = "Hello"
Dim b
b = a & " World" 'Valid concatenated string.
b = a " World" 'Not valid and will produce an error.
As double quotes are used to define a string, all instances of double quotes inside a string must be escaped by doubling the quotes "" but Rule 1. still applies.
Dim a: a = "Hello"
Dim b
b = """" & a & " World""" 'Valid escaped string.
b = """ & a & " World""" 'Not valid, start of string is not complete
'after escaping the double quote
'producing an error.
Follow these three rules and you won't go far wrong.
With those in mind the above line would need to be;
CreateObject("wscript.shell").run("""" & Path & "Name.txt""")
to generate a string surrounded by literal double quotes.
Useful Links
VBS with Space in File Path
Adding quotes to a string in VBScript
Breaking a String Across Multiple Lines (More on string concatenation).
CreateObject("wscript.shell").run(""""Path & "Name.txt""")
is how.

Opening chrome browser with disabled websecurity [duplicate]

I want to insert an if statement in a cell through vba which includes double quotes.
Here is my code:
Worksheets("Sheet1").Range("A1").Value = "=IF(Sheet1!B1=0,"",Sheet1!B1)"
Due to double quotes I am having issues with inserting the string. How do I handle double quotes?
I find the easiest way is to double up on the quotes to handle a quote.
Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0,"""",Sheet1!A1)"
Some people like to use CHR(34)*:
Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0," & CHR(34) & CHR(34) & ",Sheet1!A1)"
*Note: CHAR() is used as an Excel cell formula, e.g. writing "=CHAR(34)" in a cell, but for VBA code you use the CHR() function.
Another work-around is to construct a string with a temporary substitute character. Then you can use REPLACE to change each temp character to the double quote. I use tilde as the temporary substitute character.
Here is an example from a project I have been working on. This is a little utility routine to repair a very complicated formula if/when the cell gets stepped on accidentally. It is a difficult formula to enter into a cell, but this little utility fixes it instantly.
Sub RepairFormula()
Dim FormulaString As String
FormulaString = "=MID(CELL(~filename~,$A$1),FIND(~[~,CELL(~filename~,$A$1))+1,FIND(~]~, CELL(~filename~,$A$1))-FIND(~[~,CELL(~filename~,$A$1))-1)"
FormulaString = Replace(FormulaString, Chr(126), Chr(34)) 'this replaces every instance of the tilde with a double quote.
Range("WorkbookFileName").Formula = FormulaString
This is really just a simple programming trick, but it makes entering the formula in your VBA code pretty easy.
All double quotes inside double quotes which suround the string must be changed doubled. As example I had one of json file strings : "delivery": "Standard",
In Vba Editor I changed it into """delivery"": ""Standard""," and everythig works correctly. If you have to insert a lot of similar strings, my proposal first, insert them all between "" , then with VBA editor replace " inside into "". If you will do mistake, VBA editor shows this line in red and you will correct this error.
I have written a small routine which copies formula from a cell to clipboard which one can easily paste in Visual Basic Editor.
Public Sub CopyExcelFormulaInVBAFormat()
Dim strFormula As String
Dim objDataObj As Object
'\Check that single cell is selected!
If Selection.Cells.Count > 1 Then
MsgBox "Select single cell only!", vbCritical
Exit Sub
End If
'Check if we are not on a blank cell!
If Len(ActiveCell.Formula) = 0 Then
MsgBox "No Formula To Copy!", vbCritical
Exit Sub
End If
'Add quotes as required in VBE
strFormula = Chr(34) & Replace(ActiveCell.Formula, Chr(34), Chr(34) & Chr(34)) & Chr(34)
'This is ClsID of MSFORMS Data Object
Set objDataObj = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
objDataObj.SetText strFormula, 1
objDataObj.PutInClipboard
MsgBox "VBA Format formula copied to Clipboard!", vbInformation
Set objDataObj = Nothing
End Sub
It is originally posted on Chandoo.org forums' Vault Section.
In case the comment by gicalle ever dies:
I prefer creating a global variable:
Public Const vbDoubleQuote As String = """" 'represents 1 double quote (")
Public Const vbSingleQuote As String = "'" 'represents 1 single quote (')
and using it like so:
Shell "explorer.exe " & vbDoubleQuote & sPath & vbDoubleQuote, vbNormalFocus

VB6 Getting Error when inputting apostrophe on Filter, using ADODB

I'm trying to filter datagrid from a textbox it works but not if apostrophe or ' was typed on the textbox, I'm using ADODB and VB6
Public Sub pGetCustomer(Optional vSearch As String)
If vSearch = "'" Then
xRSTree.Filter = adFilterNone
xRSTree.Requery
Else
xRSTree.Filter = "description like '%" & vSearch & "%' or customercode like '%" & vSearch & "%'"
End If
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
As it says in the ADO documentation (when did people fall into this weird habit of calling ADO "ADODB" anyway???):
Note To include single quotation marks (') in the filter Value, use two single quotation marks to represent one. For example, to filter on O'Malley, the criteria string should be "col1 = 'O''Malley'". To include single quotation marks at both the beginning and the end of the filter value, enclose the string with pound signs (#). For example, to filter on '1', the criteria string should be "col1 = #'1'#".
You must also consider wildcard rules here:
If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wild cards are allowed, and they must be the last character in the string. Value cannot be null.
But a little confusingly:
In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example, LastName Like '*mit*'), or only at the end of the pattern (for example, LastName Like 'Smit*').
You need to "escape" your qoutes or single qoutes. Simple way would be to replace in vSearch all ' with '' and all " with "".

How to encapsulate a very long string in quotation marks?

I want to print a long VBScript program into a file as one string.
program_str = " long 200 line program"
However, placing quotation marks around so long a string does not work, as it stops recognizing the program as a string as soon as it hits another set of quotation marks. Short of separating each line of the program, and concatenating it, how could I take this long program as a string and paste it into a file?
Two options:
Double any quotes in your string literal:
program_str = "This program has a quote like this "" in it."
Use Chr(34) to specify a quote in your string literal (requires concatenating):
program_str = "This program has a quote like this " & Chr(34) & " in it."
If you want to include speech marks in a string, you must precede each of them with another quotation mark "
e.g.
program_str = "He said ""Hello"""

single quote escaping in applescript/shell

I'm trying to process this shell script in applescript, but it keeps saying error EOF, because of the single quote in the folder name.
do shell script "cp -R " & a & " " & "'" & d & "/My 'Folder/java/" & "'"
The folder /My 'Folder/ is a legitimate directory.
The variable a = '/Applications/MyProgram/' (and includes the single quotes)
The variable d = /Folders (with no single quotes)
However, shell is getting stuck processing it, im guessing because the folder is enclosed in quotes.
Is there any way to escape this single quote, so it works in applescript using shell? Ive tried multiple backslashes but its not working.
Cheers
N
Always use quoted form of when using arbitrary data as an argument to an command. This will always quote the value even if it doesn't need to be quoted but it's better safe than sorry. When you have a string single quoted, you can only unquote (turn substitution mode back on) with another quote. Unlike AppleScript strings, you can't escape characters inside single quoted strings. So you need to turn substitution mode on, escape a quote and then turn substitution mode back one. For instance "Joe's car" should be quoted as "'Joe'\\''s car'". It's a quoted string "Joe" + escaped quote character + quoted string "s car". But like I started you should use quoted form of, quoted form of "Joe's car" will return "'Joe'\\''s car'"
Your command using quoted form will look like:
do shell script "cp -R " & quoted form of a & space & quoted form of (d & "/My 'Folder/java/")
The problem arises because you have that tick in the filename of course, on the terminal commandline you would use a tick - double-tick tick - double-tick tick sequence to present it.
(From the terminal below)
729$ echo 'My '"'"'Java Folder'
My 'Java Folder
In Applescript, and your command line, it becomes even more complicated, I recommend you echo your commandline, until you get back what you expect.
set res to (do shell script "echo 'My '\"'\"'Java Folder'")
log res
--> (*My 'Java Folder*)
I think you'll have to start out with that, and reconstruct how you escape the rest of the commandline around it, if it can't be just plugged in as it is.
You should also remove single quotes for entities, that doesn't need them, (no spaces). That way your commandline will become easier to both edit and read.
Adding an answer for JavaScript for AppleScript (JAX) users based on answer from McUsr:
debugger
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var source = "/documents/John's Spreadsheet.xls";
var target = "/documents/John's Spreadsheet.csv";
source = source.replace("'", "'\"'\"'", "g");
target = target.replace("'", "'\"'\"'", "g");
var exportScript = "/excel -i";
exportScript += " '" + source + "'";
exportScript += " '" + target + "'";
exportScript += ";true";
try {
app.doShellScript(exportScript);
}
catch (error) {
console.log(error.message);
}
If you don't know what JAX is it's AppleScript but using JavaScript. Open Script Editor and select JavaScript from the dropdown beneath the record button.

Resources