String Variable inside Single Quotes in Another String Variable Not Expanding - VBA - vba

I'm trying to build quite a complicated URL String in VBA and I'm having troule escaping everything correctly. I managed to get the URL to work, but now I'm trying to "variablize" the importart parts and can't understand the issue (VBA doesn't exactly give detailed errors either).
Here is a working and non-working example URL:
Dim URL As STring, Client As String
Client = "AClientName"
URL = "http://URL?filter={'mode':'cli','client':'AClientName'}" ' This Works
URL = "http://URL?filter={'mode':'cli','client':' & CLIENT & '}" ' This Fails to Open

You're just missing the a close and open quote around the variable.
If you debug.print, you can see that it's interpreting your & and Client as just another part of the string instead of concatenating it as a variable.
In:
Debug.Print "http://URL?filter={'mode':'cli','client':'AClientName'}"
Out:
http://URL?filter={'mode':'cli','client':'AClientName'}
This works because there's no escaping the string or concatenation needed.
You can see here that & CLIENT & are part of the string.
In:
Debug.Print "http://URL?filter={'mode':'cli','client':' & CLIENT & '}"
Out:
http://URL?filter={'mode':'cli','client':' & CLIENT & '}
To get the proper string with the variable use this.
In:
Debug.Print "http://URL?filter={'mode':'cli','client':'" & Client & "'}"
Out:
http://URL?filter={'mode':'cli','client':'AClientName'}

Related

VBA dynamic dates inside an API URL

I have an API that I want to update dynamically so that the user can enter a start date and an end date on a spreadsheet and my macro will pull back data for that particular date range.
The issue I'm having is that within the API URL the StartDate and EndDate parameters must be in the format yyyy-mm-dd as a string.
I've tried URL = "https:// ...&StartDate = Format(Date(),"yyyy-mm-dd") & EndDate=Format(Date(),"yyyy-mm-dd")&..." (the ... is for the things before and after the URL).
An example of the type of URL I'm looking at is:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces
I've also played around with adding in extra quotes within the URL string but I can't seem to get it to work.
I keep getting told that the dates aren't being recognised and therefore I can only get the code to run if I hardcode dates.
Any suggestions?
I noticed a few issues in the code posted. The & is the concatenation operator in VBA. You need to enclose that in "" to make sure you are returning the ampersand as a string, and not joining strings together.
I've added some sample code which hopefully illustrates the idea and get's you back up and running. The code should print out True if the createdURL and testURL are equal, or False if not.
Code
Option Explicit
Public Sub FormatExample()
'This is the example provided
Dim testURL As String
testURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&" & _
"startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces"
'This is a built string example
Dim createdURL As String
createdURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345" & _
"&startdate=" & Format(#10/1/2008#, "yyyy-mm-dd") & _
"&end-date=" & Format(#10/31/2008#, "yyyy-mm-dd") & _
"&metrics=ga:sessions,ga:bounces"
'Print out if they are equal
Debug.Print createdURL = testURL
End Sub

VBA Hyperlinks.Add method prepending local folder to Address

I've been lurking here for a while but this is my first post so let me know if I need to change something. Anyways, here goes:
I'm trying to create a macro that will add hyperlinks to cells in a worksheet. The problem is that after running the macro, I notice that the folder location of my spreadsheet has been prepended to the address that I specified. Is there something I need to do in order to indicate that this is a webpage and not a local file? Excerpt from the macro is below.
Dim IGQ As Range
Dim IGQno As String
Dim IGQno1 As String
For Each IGQ In Range("A2:A10") 'Actual range is much larger
IGQno = IGQ.Value
IGQno1 = Left(IGQ, 1)
Sheets("Cameron DCDA").Hyperlinks.Add Anchor:=IGQ, _
Address:="""http://xxxx""&IGQno1&""xxx""&IGQno&""xxxxx""" 'It's a company website so they probably don't want me to share it
Next
The result is that a hyperlink is created for each cell but it links to file:///C:\Users\John.Doe\Documents\"http://xxxx"&IGQno1&"xxx"&IGQno&"xxxxx"
I've tried using fewer quotation marks in the address since it seems like overkill but I get the compile error "Expected: end of statement"
Do you guys have any suggestions?
Too many quotes
Address:="http://xxxx " & IGQno1 & "xxx" & IGQno & "xxxxx"
Also - be sure to leave a space before your & otherwise it will be interpreted as a variable type suffix:
What are possible suffixes after variable name in VBA?

combine strings containing NULL

I am attempting to use the confluence REST API to attach files to a wiki page. I generate assorted graphs in excel using VBA and save them as images (currently as png/jpeg).
I then use the example here to upload a file to the wiki as an attachment. This method works well for text files, but when I attempt to use images I run into some issues. Specifically as I understand it the code:
sPostData = "--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--"
attempts to add string boundaries to the binary representation of the file by concatenating string. My issue is that the representation for the png contains NUL
‰PNG
SUB
NULNULNUL
IHDRNULNULEOT+NULNULSTXSYNBSETXNULNULSOH>&Ǽ ...etc...
which promptly terminates the string, and so you lose the rest of the file and the final string boundary. I believe this occurs with all string functionality in excel.
My question is how should I go about attaching the string boundaries to my file so as to avoid this issue?
Thanks in advance :)
As Alex K. pointed out I have been working under an incorrect assumption. The truncation is present in my analysis of the issue and not the actual string.
The problem was actually due to a mistake on my part - from the webiste:
Another caveat is the pvToByteArray function. It turns out send
method can not handle “byref” byte arrays, so for instance passing
baBuffer will fail, as VB6 sets up VT_BYREF bit of the type of the
variant parameter.
It was my implementation of this part of the code that was incorrect - this caused the failure and the error message
500: Stream ended unexpectedly
After correcting this the issue appears resolved.
Thanks again Alex.

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.