VBA vbNewLine but with no extra space - vba

I'm adding a Title to a chart but I keep getting an extra space (white space) between the two strings when I use vbNewLine.
myChart.ChartTitle.Text = "Distance to Default for" & vbNewLine & compName
note compName is another string I previously define.

Most probably it's a text format issue. Try: vbCrLf, vbCr, vbLf or (digged up the doc) Environment.NewLine. One of the four should be OK.
"Cr" is "carriage return" while "Lf" is "Line Feed" (these terms come from old typewriter times). Some OS/text format/system/whatever uses both "driver characters", some use just one of these - the white space you saw was an unnecessary "cr" or "lf" if that makes sense

Try vbCrLf:
myChart.ChartTitle.Text = "Distance to Default for" & vbCrLf & compName
EDIT: Try vbCr since vbCrLf looks to be identical to vbNewline
myChart.ChartTitle.Text = "Distance to Default for" & vbCr & compName

helpful post. I have the same issue. "text" & vbnewline & "text" is two carriage returns instead of one...
vbVerticalTab worked well for me.

I don't understand what end result you're aiming for, but using vbNewLine will insert a carriage return.
If you're just trying to insert a space between the text and the value then one of the following would work:
myChart.ChartTitle.Text = "Distance to Default for " & compName
myChart.ChartTitle.Text = "Distance to Default for" & Chr(32) & compName

So here's my solution. Use chr(10) instead of vbNewLine and then do this:
compName = Replace(compName, vbCrLf, Chr(10))
It worked for me. Don't know if it'll work for everyone. Cheers.

You can also use vbVerticalTab or Chr(11). This is the same as hitting shift + enter if you were typing a Word document.
Interestingly, in the Official documentation, VBA Miscellaneous constants, it says this in the description:
Not useful in Microsoft Windows or on the Macintosh
Of which I disagree. The documentation says the same thing about vbFormFeed / Chr(12), but you can use that to easily insert page breaks.

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.

vb6 printer.print does not work in vb.net 201

Receive this error in build
Operator '&' is not defined for types 'String' and 'Microsoft.VisualBasic.TabInfo'
This is the line, what do I have to do to make this valid (which does work in vb6)
Printer.Print("ATICTS PROBLEM REPORT: " _
& txtCallId.Text & FileSystem.TAB(70) & "Hours to date:" _
& FileSystem.TAB(90) & txtHours.Text)
I would suggest that you break that up...
Printer.Print("ATICTS PROBLEM REPORT: ")
Printer.Print(txtCallId.Text)
Printer.Print(FileSystem.TAB(70))
Printer.Print("Hours to date:")
Printer.Print(FileSystem.TAB(90))
Printer.Print(txtHours.Text)
As understand it, FileSystem.Tab wasn't a simple tab character or number of spaces. I believe the above has the best chance of producing the same results.

How to incorporate a changing variable into a text Msgbox in VBA?

I am currently trying to make a simple VBA program that would multiply two numbers together and then prompt a message box that says "The value of (variable1) and (variable 2) is (The answer).
Here is my current attempt that isn't perfect:
MsgBox prompt:=intmlt, Title:="The Value of " & intFirstNum & intSecondNum
The two big issues I have is how to you put a space inbetween the intFirstNum and intSecondNum? and How do you add an is to the end of that prompt?
It should be
"The Value of " & intFirstNum & " and " & intSecondNum & " is "
Per #ScottHoltzman

Word Interop Text replace in header adding extra spaces

Morning Everyone,
using interop.word to do a find/replace in the header. It works but word, in it's great wisdom, adds spaces: 1 space to the beginning of each line after the first, 2-3 spaces at the end of each line.
So it looks like this but it should be completely left:
Sept 2, 2015
first name
last name
address
I am looking at the text of the header after replacement using headerfooter.range.text and there are no extra spaces so it has to be an auto-formatting issue that word is obsessed with.
Thanks for any help!
What I have tried with no luck:
paragraphformat.duplicate before and reset after
range.paragraphformat.spacebefore = 0
range.paragraphformat.spaceafter = 0
Dim info As String = Now.ToLongDateString & vbCrLf & Trim(PersonInformation.FullName) & vbCrLf & Trim(PersonInformation.Address)
info &= vbCrLf & Trim(PersonInformation.City & ", " & PersonInformation.State & " " & PersonInformation.Zip)
hf.Range.Find.Execute(TagToReplace, , , , , , , , , info)
UPDATED
Found the answer. It is how word/interop interprets vbcrlf when generating the doc. Using just vbcr did the trick. vblf also caused the inserted space. Incidentally, vbNewLine also caused the extra space to appear.
Found the answer. It is how word/interop interprets vbcrlf when generating the doc. Using just vbcr did the trick. vblf also caused the inserted space. Incidentally, vbNewLine also caused the extra space to appear.

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.