This question already has answers here:
Escaping quotes in a string in VB6
(7 answers)
Closed 8 years ago.
I am trying to make one of my variables equal to the following:
Dim Quote as string
Quote = "Letter Size 11 x 17""
I need the single quote/inch symbol in the text but i do not know how to make VBA recognize it without giving me and error saying i have to end quote it.
Try one of these. It seems to do what you want (depending on where you want the ":
Sub test()
Dim Quote As String
Quote = "Letter Size 11 x 17"""
Debug.Print Quote
Quote = "Letter Size 11"" x 17"""
Debug.Print Quote
End Sub
Related
I have a multi-column combobox on an Access form (Access 2019 x64, if that matters) and would like to use its AddItem() method to populate it as described in this documentation. Citing from it:
For multiple-column lists, use semicolons to delimit the strings for
each column (for example, "1010;red;large" for a three-column list).
If the Item argument contains fewer strings than columns in the
control, items will be added starting with the left-most column. If
the Item argument contains more strings than columns in the control,
the extra strings are ignored.
So it is impossible to add a string for a certain column when the string itself already contains a semicolon?
Surround the column value which contains the semi-colon with double-quotes, e.g.:
AddItem """Column1;A"";Column2;Column3"
Will yield:
+-----------+---------+---------+
| Column1;A | Column2 | Column3 |
+-----------+---------+---------+
All credits go to #Lee Mac who answered my original question completely. However, the answer immediately raised the question how we should treat strings which already have quotes in them.
Therefore, I have written the following function in VBA which seems to solve the problem completely. The comments should be self-explaining. Please comment if you find an error.
I have posted this as separate answer because it's way too long for a comment, and because it seems that the community does not prefer such answers edited into the original question.
'Notes:
'
'The following function quotes an arbitrary string so that it can be
'used as part of the parameter to the ComboBox.AddItem() method.
'Writing that function was necessary because there is no easy way to
'use strings which already contain the ; character as part of that
'parameter, or which even contain the ; character as well as double
'quotes; see also Microsoft's documentation.
'
'To construct the parameter for the AddItem() method, the following
'must be done:
'
'For EACH column, the source string must be quoted using the following
'function. Then, all QUOTED strings must be concatenated, separating
'them only by one ; character.
'
'Successfully tested 2019-10-03 in Access 2019 / 64-bit / VBA with a
'standard combobox on a form.
Public Function QuoteStringForValueList(sString As String) As String
Dim sTemp As String, sDDQ As String
'String containing two double quotes to make reading easier
sDDQ = Chr(34) & Chr(34)
'Replace each double quote by four double quotes
sTemp = Replace(sString, Chr(34), sDDQ & sDDQ)
'Add two leading and two trailing double quotes
QuoteStringForValueList = sDDQ & sTemp & sDDQ
End Function
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
IndexOutOfRangeException in VB.NET
(2 answers)
What does IndexOutofRangeException mean?
(3 answers)
“IndexOutOfRangeException was unhandled” [duplicate]
(2 answers)
Closed 4 years ago.
I have this code:
Module Module11
Sub Main()
Dim mess As String
Dim out As String
Dim num, p As UInteger
Dim q As Integer = -1
Console.Write("Enter a message: ")
mess = Console.ReadLine()
While p < 9
q += 1
num = Asc(mess(p + q)) + 5
out = Chr(num)
Console.Write(out)
End While
Console.ReadKey()
End Sub
End Module
But on Line num = Asc(mess(p + q)) + 5, it is showing 'index out of the bounds of array'.
I was actually trying to create code that could change every character (even blank space) to the next sixth character (with reference to ASCII codes) of whatever character we input.
It shows error even after giving the correct output (in the black console).
Please help.
I think I have the answer (or at least AN answer). I'm not familiar with visual basic, but I am familiar with Python and this seems to be extremely similar. After doing some quick researching, I learned that the "UInteger" data type cannot be negative. I would recommend changing the while loop to read "While p > 0 And p < 9" and then the rest of the code. It looks to me like your answer is coming out negative. This would make sense considering what your error message says. However, I'm not sure how the output would still come out correctly, but it's worth a shot. I would try that, and let me know how it goes!
This question already has answers here:
How to add open and close quotes if A string has spaces
(3 answers)
Closed 6 years ago.
How would I insert a double quote (") in vb.net without signalling the end of the string?
Use Chr(34) instead of the actual character. Check out decimal codes for more like this one.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace " in vb.net
How to put data containing double-quotes in string variable?
Aug 01, 2003 02:30 AM | LINK
I need to store a string that contains words in double quotes. The complete string data is presented to the variable in double quotes, but the next double quotes within the data ends the string and the remaining text gives a syntax error. How can I place literal double-quotes in a string variable? For example:
Dim MyVar as string = "stm_bm(["menu53d0",400,"","blank2.gif",0,"","",0,0,250,0,1000,1,0,0,""],this);"
You can escape (this is how this principle is called) the double quotes by prefixing them with another double quote. You can put them in a string as follows:
Dim MyVar as string = "some text ""hello"" hello"
This will give the MyVar variable a value of some text "hello" hello.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace " in vb.net
in vb.net how can i use replace function when the replace from part has double quotes in its value ?
Dim s As String = String.Replace("some string", "some "" string with quotes")