VBS - Ignoring the \ special character properties - string

I'm struggling to call a command line correctly in VBS due to the \ escape character.
The string output I'm looking to write to the command line is,
batch_name=\"myBatch\"
Which gets passed to a .exe file. Unfortunately, due to the way the \ character works I can only write,
batch_name=\myBatch\
batch_name=\""myBatch\""
I can't get \" in the output! An altered version of my code is below,
BATCH_NAME = "myBatch"
outputString = "batch_name=\" & BATCH_NAME & "\"
I've tried lots of methods - Concatenating the string with Chr(34), using multiple double quotes, even trying to replace() "" with ", nothing seems to work.
Any ideas?

I gave it a shot and
outputString = "batch_name=\""" & BATCH_NAME & "\"""
worked for me giving the result batch_name=\"myBatch\"
Does it work for you? How you execute this command in the shell?

You can do the following;
outputString = "batch_name=" & chr(92) & chr(34) & BATCH_NAME & chr(92) & chr(34)

Related

How to use Call Shell when there are commas in a folder directory?

I am trying to use Call Shell to open up windows explorer to a pre-determined directory saved as string.
e.g. FolderDirectory = P:/Address, Postcode
Then
Call Shell("explorer.exe" & " " * FolderDirectory, vbNormalFocus)
It works when there is no comma in the directory but when there is a comma it just opens up the documents directory.
Any ideas?
I made a folder named Address, Postcode in my Temp Folder and this code worked for me:
Dim FolderDirectory As String
FolderDirectory = "C:\Temp\Address, Postcode"
Call Shell("explorer.exe " & """" & FolderDirectory & """", vbNormalFocus)
Hope you can adapt it to your needs
It looks like when there is a comma on it, you need to double quote the path. For this answer, I based on what I read here:
Open folder which contains comma in its path

Carriage return and line feed not being replaced into VBA string

I'm writing some logs entries into a csv file however when I'm trying to replace CR and LF into a specific string I'm getting the same result as before.
Replace(SQLStatement, vbTab & Chr$(13) & Chr$(10), "")
Not being able to remove CR and LF, and even Tabs, is affecting my csv and finally in the Excel file the string is outputed on more lines instead of just one.
That will only replace the specific combination of vbTab & Chr$(13) & Chr$(10).
If you want to replace them you will need to do the individually.
Replace(Replace(Replace(SQLStatement, vbTab,""), Chr$(13),""), Chr$(10), "")
Although you need to be careful that your lines do not run into one another. You might be safer with
Replace(Replace(Replace(SQLStatement, vbTab," "), Chr$(13)," "), Chr$(10), " ")

Expression.Error when dynamically passing in Teradata SQL query (with column aliases) to ODBC query

I have a macro that prompts me for a SQL query (unless it was called by another Sub, in which case it uses the argument that was passed into its optional string parameter as the query) and then executes the query against my Teradata SQL database.
It works fine, unless there's a column alias containing a space in the query.
Example query:
SELECT 2 + 2 AS "Query Result";
Error:
Run-time error '1004':
[Expression.Error] The name 'Source' wasn't recognized. Make sure it's spelled correctly.
The line of code which I believe is the culprit is as follows (my apologies for the readability-- I recorded the macro, modified it just enough to get it to work somewhat dynamically and then haven't touched it since).
ActiveWorkbook.Queries.Add Name:=queryName, formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=my-server-name"", " & Chr(34) & code & Chr(34) & ")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
I assume it has to do with the fact that the example query above has double quotes for the alias, which are confusing the syntax when trying to be interpolated. Any help would be greatly appreciated!
Here's what the string for the formula is set to in this line:
ActiveWorkbook.Queries.Add Name:=queryName, formula:=<string here>
after all the chr() and concatenation are done:
let
Source = Odbc.Query("dsn=my-server-name", "<code>")
in
Source
That token <code> is replaced by whatever is in your variable code. So I suspect you are correct in that this formula would need to have it's double quotes escaped fully.
In other words this string you are building form Formula is going to be evaluated as code itself, and even in that evaluation it will be passing more code (your SQL) onto the Teradata server to be evaluated there.
You are in code inception. VBA code writing powerquery code writing Teradata code.
Understanding that and guessing a bit here, I'm thinking your current code variable looks something like:
code="SELECT 2 + 2 AS ""Query Result"";"
Your double quotes are already escaped for VBA. BUT because you have to survive another round of eval in powerquery you need to escape once again. Instead:
code="SELECT 2 + 2 AS """"Query Result"""";"
*I think...

Query by the string containing &

I have got a string containing "&" like "Coke & Cola" How to use this string on Queries with & Please Help me.
I must Query by this sign &
Have you tried using
amp;
or
\&
?
I'm making the assumption that you're using Sharepoint given the tag - I found an article here that might be relevant to your problem:
http://social.msdn.microsoft.com/Forums/en-US/sharepointcustomizationlegacy/thread/90479143-e26e-45cc-ae56-16574056d6d4
[general SQL] There are many answers take a look at source link:
SET ESCAPE ON
SET ESCAPE ""
SELECT 'You & me' FROM DUAL;
source
[CAML SharePoint] answer:
replace & with &
& must be changed to &
Also <must be changed to < and >must be changed to >

Write comma-delimited text file in vba, elegantly

I just have to ask this for once...
Is there a more elegant way of having your output comma-delimited, than the tried and tested & "," & we always use?
print #myTextFileNumber, myValue1 & "," & myText2 & "," & myValue3
the command you are looking for is write
from the help:
the Write # statement inserts commas between items and quotation marks around strings as they are written to the file
in your example, then command would be:
write #myTextFileNumber, myValue1 , myText2 , myValue3
If you store your values in an array, you can use join.

Resources