How to enter multiple string variables into a text box - vba

I have a workbook with an userform that captures user input into string and single variables and I want to display a text consisting those variables into a text box on the same userform using new line and tab.
Example:
Dim dog as String 'rex
Dim years as Single '5
Dim owner As String 'Joe
Dim address as String '123 Sample Street
Dim value as Single '300.00
I would like to have a textbox on my form, that would display:
The dog's name is rex. He is 5 years old.
Owner: Joe
Address: 123 Sample Street
Treatment value: 300.00
I used
textbox.value = "The dog's name is " & dog & vbNewLine & vbNewLine & "Owner: " & owner & vbNewLine & "Address: " & address & vbNewLine & vbNewLine & "Treatment value: " & value
But after some time i will not be able to add another character to this line and I have plenty more variables mixed with text to come.
Can you suggest how this can be done?
Update: Resolved
Many thanks for your help.

Try
textbox.value = "The dog's name is " & dog & vbNewLine & vbNewLine
textbox.value = textbox.value & "Owner: " & owner & vbNewLine
textbox.value = textbox.value & "Treatment value: " & value & vbNewLine
Continue with other fields!

Try like this:
textbox.value = "The dog's name is " & dog & vbNewLine & vbNewLine & _
"Owner: " & owner & vbNewLine & _
"Address: " & address & vbNewLine & vbNewLine _
& "Treatment value: " & value
The " _" signs help you break the code into more lines. Note, that we have 2 signs - space and underscore there.
A small info - it is possibly good to consider passing the text as a .Text and not as .Value. Here is something good to read - Distinction between using .text and .value in VBA Access

Related

Microsoft.Office.Interop - ReplaceText (with) *NewLine* in Word.Table

I am having an application that uses Microsoft Word 2016 / the Interop interface to call the replace function. This way the application replaces some texts like 'Customername' to the actual name of the customer. Surely this is not the best way to do so, but thats how the application was build back in the days. Now I have to do some adjustments to the formatting facing a problem with the named function.
One of the Texts getting replaced is "[TELNR]" , beeing replaced with all known numbers of the customer, one number per line. Inserting New Lines as vbCr works great on a blank page as you can see top of the screenshot. The same called function will not put a new lines into a table (second page on the left) and won't even touch a textbox (second page on the right).
I have tried passing Line breaks as ^p, \n\r, \n, vbClRf / vbNewLine, vbCr. None of these will work.
I haven't found anything usefull on google or SO. Do you have ideas or a reason what might cause or even solve the problem?
#Region "Sample Strings"
Dim sample1 As String = "t:022220000000" & vbNewLine & "h:022221111111" & vbNewLine & ":0222222222222" & vbNewLine & "f:022223333333"
Dim sample2 As String = "t:022220000000" & vbCr & "h:022221111111" & vbCr & ":0222222222222" & vbCr & "f:022223333333"
Dim sample3 As String = "t:022220000000" & "^p" & "h:022221111111" & "^p" & ":0222222222222" & "^p" & "f:022223333333"
Dim sample4 As String = "t:022220000000" & "\n" & "h:022221111111" & "\n" & ":0222222222222" & "\n" & "f:022223333333"
Dim sample5 As String = "t:022220000000" & "\n\r" & "h:022221111111" & "\n\r" & ":0222222222222" & "\n\r" & "f:022223333333"
Dim sample6 As String = "t:022220000000" & vbCrLf & "h:022221111111" & vbCrLf & ":0222222222222" & vbCrLf & "f:022223333333"
#End Region
Private Sub replaceText(file As Microsoft.Office.Interop.Word.Document, find As String, replaceWith As String)
// First line was commented out for the ^p try
If replaceWith.Contains("^") Then replaceWith = replaceWith.Replace("^", "")
file.Content.Find.Execute(FindText:=find, ReplaceWith:=replaceWith, Replace:=Word.WdReplace.wdReplaceAll)
End Sub
The following was function was used to insert the textbox.

How to insert enters into email body in Outlook

What I'm trying to do is to copy a range from Excel (2 columns) and to paste in into email body in Outlook. I'd like to get also 2 columns in email body but I still receive everything in one line. Here's my code:
Tresc = "Cześć" & vbCr & vbCr & " słówko/a na dzisiaj to:" & vbCr & vbCr & Tresc & vbNewLine
'utworzenie hiperłącza
Hiperlacze = "mailto:" & Trim(LCase(Adres)) & _
"?subject=" & Tytul & _
"&body=" & Tresc
I believe you are looking for the vbCRLF built-in for "carriage return/linefeed" instead of just vbCR.
Since this is a mailto-link, there are some other rules in play. Replace the vbCr with "%0D%0A".
Example:
Tresc = "Cześć%0D%0Asłówko/a na dzisiaj to:" 'etc.

String sets its Value to "False"

So, I'm currently working on a program that should create Lua-Scripts for the Unitale-Engine. I added a "New Project" button which should create a new project file when you click it. I want this file to contain the default project-file-text. So i did this:
Dim File1 As String = "monster{" & vbNewLine & "COMMENTS=Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow." & vbNewLine & "COMMANDS=Compliment;Threaten" & vbNewLine & "DEFAULT_SPRITE=None" & vbNewLine & "NAME=Froggit" & vbNewLine & "HP=10" & vbNewLine & "ATK=1" & vbNewLine = "DEF=0" & vbNewLine & "CHECK_MESSAGE=Life is difficult for this enemy." & vbNewLine & "HandleAttack=" & vbNewLine & "OnDeath=" & vbNewLine & "HandleCustomCommand=" & vbNewLine & "}"
Dim File2 As String = vbNewLine & "encounter{" & vbNewLine & "DEFAULT_MUSIC=Fight" & vbNewLine & "ENCOUNTER_TEXT=Froggit hops close." & vbNewLine & "NEXTWAVES=1" & vbNewLine & "WAVETIMER=6" & vbNewLine & "ARENASIZE=155;130" & vbNewLine & "ENEMIES=Froggit" & vbNewLine & "ENEMYPOSITIONS=0;0" & "EncounterStarting=" & vbNewLine & "EnemyDialogueStarting=" & vbNewLine & "EnemyDialogueEnding=" & vbNewLine & "DefenseEnding=" & vbNewLine & "HandleItem=" & vbNewLine & "HandleSpare=" & vbNewLine & "EnteringState=" & vbNewLine & "Update=" & vbNewLine & "}"
Dim File3 As String = vbNewLine & "waves{" & vbNewLine & "1=Do Nothing" & vbNewLine & "}" & vbNewLine & "end"
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\UnitaleMaker\Projects\" & PJNBox.Text & ".umproj", File1 & File2 & File3, False)
Yes, that is a lot of text. The problem is, that File1 always sets its value to "False" when it is declared. I thought that is because the string is too long, so I split it to 3 parts, but it doesn't work.
Any help would be apprechiated!
~ Mika // OneByte_
Starting with VB 14 (Visual Studio 2015), you can span String literals across multiple lines. VB 14 also adds String interpolation, which makes for much cleaner code. You could write your code like the following which would have kept you from having made your mistake of & vbNewLine = "DEF=0" &and would overall make the code easier to read.
I am also assuming you forgot a newline in "ENEMYPOSITIONS=0;0" & "EncounterStarting=" and I have indented the parts between the {}. I combined File1, File2, and File3 back into a single string since you stated you broke them up in the question, I assumed you'd rather have it all in one string.
Dim File1 As String = "monster{
COMMENTS = Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow.
COMMANDS=Compliment;Threaten
DEFAULT_SPRITE=None
NAME=Froggit
HP=10
ATK=1
DEF=0
CHECK_MESSAGE=Life Is difficult For this enemy.
HandleAttack=
OnDeath=
HandleCustomCommand=
}
encounter{
DEFAULT_MUSIC = Fight
ENCOUNTER_TEXT =Froggit hops close.
NEXTWAVES=1
WAVETIMER=6
ARENASIZE=155;130
ENEMIES=Froggit
ENEMYPOSITIONS=0;0
EncounterStarting=
EnemyDialogueStarting=
EnemyDialogueEnding=
DefenseEnding=
HandleItem=
HandleSpare=
EnteringState=
Update=
}
waves{
1 =Do Nothing
}
End"
My.Computer.FileSystem.WriteAllText($"{My.Computer.FileSystem.SpecialDirectories.MyDocuments}\UnitaleMaker\Projects\{PJNBox.Text}.umproj", File1, False)
Please learn to use string.format.
Also I changed & to new line in file2:
"ENEMYPOSITIONS=0;0" & "EncounterStarting="
I just assumed you forgot to add new line there.
Alss Environment.NewLine is the best option for new lines as it is universal.
Dim File1 As String = String.Format("monster{{{0}COMMENTS=Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow.{0}COMMANDS=Compliment;Threaten{0}DEFAULT_SPRITE=None{0}NAME=Froggit{0}HP=10{0}ATK=1{0}DEF=0{0}CHECK_MESSAGE=Life is difficult for this enemy.{0}HandleAttack={0}OnDeath={0}HandleCustomCommand={0}}}", Environment.NewLine)
Dim File2 As String = String.Format("{0}encounter{{{0}DEFAULT_MUSIC=Fight{0}ENCOUNTER_TEXT=Froggit hops close.{0}NEXTWAVES=1{0}WAVETIMER=6{0}ARENASIZE=155;130{0}ENEMIES=Froggit{0}ENEMYPOSITIONS=0;0{0}EncounterStarting={0}EnemyDialogueStarting={0}EnemyDialogueEnding={0}DefenseEnding={0}HandleItem={0}HandleSpare={0}EnteringState={0}Update={0}}}", Environment.NewLine)
Dim File3 As String = String.Format("{0}waves{{{0}1=Do Nothing{0}}}{0}end", Environment.NewLine)
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\UnitaleMaker\Projects\" & PJNBox.Text & ".umproj", File1 & File2 & File3, False)

Display Text in Label like Notes

In my Application i need to display some short notes in label.
So i tried like this
lable1.Text="HELP : " & vbNewLine & vbNewLine & "1. You can Remove or
Replace existing Main Icons" & vbNewLine & vbNewLine _
& "2. If you want to remove any buttons, Right click on Icon and
select UNPIN or select the " _
& " Icon and press delete button from key borad" & vbNewLine &
vbNewLine & "3. Drag the needed button from top menu and drop it on
empty space in Icons." & vbNewLine & vbNewLine & "4. If Reports
having sub reports, You can drag and drop the sub reports only."
If the points in came to second line its start from 2 but i need all letter start same level like this
1 . aaaa
2. bbbbb
vvvvv.
and Help must have underline. How to do it. Can say which control good for this can you give some example also.
am using VB.Net 2008
I dont think you can set tabs in a label, so you might need to manually space the text using a monospaced font. But in a richtextbox, you can do this.
RichTextBox1.Text = "HELP : " & vbNewLine & vbNewLine &
"1." & vbTab & "You can Remove or Replace existing Main Icons" & vbNewLine & vbNewLine &
"2." & vbTab & "If you want to remove any buttons, Right click on Icon and" & vbNewLine _
& vbTab & "select UNPIN or select the Icon and press delete button from keyboard" & vbNewLine & vbNewLine &
"3." & vbTab & "Drag the needed button from top menu and drop it on" & vbNewLine _
& vbTab & "empty space in Icons." & vbNewLine & vbNewLine &
"4." & vbTab & "If Reports having sub reports, You can drag and drop the sub reports only."
RichTextBox1.SelectAll()
RichTextBox1.SelectionTabs = {20}
RichTextBox1.DeselectAll

evaluating strings in VBA

I have a string that is read from a cell in Excel, which is something like this:
""Horace Lai" & vbNewLine & Date"
or this
"Chr(149) & "level 1" & vbNewLine & Chr(9) & Chr(149) & "level 2" & Chr(149) & "level 1" & vbNewLine & Chr(9) & Chr(149) & "level 2" & vbNewLine & Chr(9) & Chr(9) & Chr(149) & "level 3""
I would like to be able to evaluate these strings so that ""Horace Lai" & vbNewLine & Date" becomes:
Horace Lai
2014-06-20
So far I have tried ScriptControl without success. It doesn't seem to like it when I have double quotes for the string.
Sub testing()
Dim sc As ScriptControl
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
MsgBox sc.Eval(""Horace Lai" & vbNewLine & Date")
End Sub
The MsgBox line becomes red and I cannot run it.
Any ideas? Thanks
-Horace
You specify JScript, instead:
Dim test As String, result As String
test = "Chr(149) & ""level 1"" & vbNewLine & Chr(9) & Chr(149) & ""level 2"" & Chr(149) & ""level 1"" & vbNewLine & Chr(9) & Chr(149) & ""level 2"" & vbNewLine & Chr(9) & Chr(9) & Chr(149) & ""level 3"""
Dim sc As Object
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
result = sc.Eval(test)
Debug.Print result
•level 1
•level 2•level 1
•level 2
•level 3
Firstly load the name into a variable i.e. name = """Horace Lai""" (this will contain the quote marks)
Then use the replace function to search for quotes in name name = Replace(name,"""","")
Then run the msgbox of MsgBox (name & vbNewLine & Date)
This should remove the extra " marks that you face