_ works to split a long line into multiple line in VBSCript. Wondering if it works for QTP as well
Use & _ for break your long lines.
sString = "Something very long " & _
"More on that" & _
"A little more"
Reference is here
Related
I am trying to understand something about about the way VBS splits single lines of code across multiple lines.
In the below function the _ character is used in two places to split execution across two lines, ok all fine.
For Each objItem in colItems
if i=0 then
header = ""
For Each param in objItem.Properties_
header = header & param.Name & vbTab
Next
WScript.Echo header
i=1
end if
serviceData = ""
For Each param in objItem.Properties_
serviceData = serviceData & param.Value & vbTab
Next
WScript.Echo serviceData
Next
What I do not understand then is how this is supposed to look on a single line. When I modify either of the lines with any of the below I get an error.
For Each param in objItem.Propertiesheader = header & param.Name & vbTab
For Each param in objItem.Properties.header = header & param.Name & vbTab
For Each param in objItem.Properties header = header & param.Name & vbTab
Errors to the effect of:
C:\Program Files (x86)\ManageEngine\AppManager12\working\conf\application\scripts\wmiget.vbs(86,2) Microsoft VBScript runtime error: Object doesn't support this property or method: 'objItem.PropertiesserviceData'
How would the above be correctly represented on a single line?
The issue here is you are using the Line Continuation Character _ to continue a line when it isn't required.
You only use it if you need a single line of code to span multiple lines, using your example something like;
For Each _
param _
in _
objItem.Properties
serviceData = serviceData & param.Value & vbTab
Next
will work (be it a bit pointless) because the single line
For Each param in objItem.Properties
is being spanned across multiple lines.
So, because the line;
For Each param in objItem.Properties
is a single line of code no continuation is required as the next line of code is expected. However, when you provide it, it causes VBScript to error with;
Microsoft VBScript compilation error: Expected end of statement
Which is VBScript's way of telling you that the line isn't finished and still expects the end of the code statement.
If you do want to span multiple code statements across a single line use colon (:) which acts as a statement seperator, i.e;
For Each param in objItem.Properties : serviceData = serviceData & param.Value & vbTab : Next
Useful Links
Line Continuation Character (_) also known as a Statement Break.
Breaking a String Across Multiple Lines (Focuses on strings but talks about the Line Continuation Character).
VBScript, purpose of colon?
i'm trying to do a guessing game in VB6. it involves comparing a letter that the user input. i already searched for it, unfortunately, they only have for the latest version.
can anybody help me on comparing a single char in Visual Basic6.0 . because, frankly i have no idea on it.
Asc and Chr$ are the functions to look for.
Private Sub Form_Load()
Dim lSecret As Long
Dim sInput As String
Dim lAscChar As Long
'Define a secret character as an ANSI code.
lSecret = Asc("m")
Do
'Let the user input a single character.
sInput = InputBox("Enter a single character. " & _
"If more characters are entered, only the first one " & _
"will be used. To end just click OK without entering text.")
If Len(sInput) = 0 Then Exit Sub
'Obtain the first character's ANSI code.
lAscChar = Asc(sInput)
'If the user entered the correct secret character, tell her.
'Otherwise give a hint.
If lSecret = lAscChar Then
MsgBox "Great, you are a hero."
Exit Sub
ElseIf lSecret < lAscChar Then
MsgBox "Nope, in the ANSI table, the correct answer is " & _
"before this one."
Else
MsgBox "Nope, in the ANSI table, the correct answer is " & _
"after this one."
End If
Loop
End Sub
I'm trying to create an input box in VB6 where I will be able to include spaces in the PROMPT. I've tried to do the same thing I do with MSGBOX but it's not letting me do VBCRLF. here's what i'm looking to do
This company has more than 1 department.
For Accounting type 1
For Reasearch type 2
input box in here and the OK and CANCEL to the right
The following code will display a multi-line prompt string:
Dim ret As String
Dim prompt As String
prompt = "This company has more than 1 department." & vbCrLf & _
"For Accounting type 1" & vbCrLf & _
"For Reasearch type 2"
ret = InputBox$(prompt)
I Have Displayed text file in richtextbox.
and onclick on command button value of textbox1 is being replaced in text file.
but How to keep both data . previous one and another which is entered new in textbox
I HAVE USE THIS CODE BUT IT REPLACES ALL THE TEXT :
Open "D:\chat.txt" For Output As #1
a = Text1.Text
Print #1, a
Close #1
Change For Output to For Append, and it will add the new text to the end of the file instead of overwriting it.
Additional note
Since I'm not able to add a comment to Boann's answer (the one marked as accepted).
The Append access mode used with the Print statement automatically appends a new line at the end of the file. This is fine in almost all cases, but for anyone reading this that wants to avoid this behavior, just add a semicolon at the end of the Print statement (this is the only instance I've seen the semicolon used in VB6).
a = Text1.Text
intHandle = FreeFile
Open "D:\chat.txt" For Append As intHandle
Print #intHandle, a; ' Notice the semicolon; prevents a new line after this output.
Close #intHandle
I'm sure the code you posted originally was just for the sake of getting an answer and is not what your code actually looks like. Otherwise:
To you or any future readers, here's a simple AppendToFile() function which will make repeated calls easier, ensures the file gets closed even if a run-time error is encountered, and shows useful debug information upon failure (i.e. with an invalid filename):
How your original code would be written when putting my below function in your code:
AppendToFile "D:\chat.txt", Text1.Text
And here's the function:
Private Function AppendToFile( _
ByRef FilePath As String, _
ByRef Text As String, _
Optional ByVal AppendNewLine As Boolean = True _
) As Boolean
On Error GoTo ErrorHandler
Dim intHandle As Integer
' Get an available file handle to use.
intHandle = FreeFile
Open FilePath For Append As intHandle
' Only use semicolon at end if we do NOT want to append a new line.
If AppendNewLine Then
Print intHandle, Text
Else
Print intHandle, Text;
End If
Close intHandle
intHandle = 0
AppendToFile = True
Exit Function
ErrorHandler:
' Ensure that file is indeed closed.
If intHandle <> 0 Then
Close intHandle
End If
' Show error in debug window (CTRL+G)
Debug.Print _
"Error (#" & CStr(Err.Number) & ") in " & _
"TextToFile( _" & vbCrLf & _
"`" & FilePath & "`, _" & vbCrLf & _
"`" & Text & "`, _" & vbCrLf & _
IIf(AppendNewLine, "`True`", "`False`") & vbCrLf & _
"): " & Err.Description & IIf("." = Right$(Err.Description, 1), "", ".") & vbCrLf
Exit Function
End Function
strCode = "Private Sub AcclvsTime() " & vbCr _
& "Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225) "& vbCr _
& "myChtObj.Chart.ChartType = 4 " & vbCr _
& "myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")" & vbCr _
& "End Sub"
objWriteExcel.VBE.ActiveVBProject.VBComponents.Item("Sheet1").CodeModule.AddFromString(strCode)
When I executed this code i got the error “end of the statement expected in line 4” (& "myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")" & vbCr _)
Can any one help me where is the mistake?
#paxdiablo: I would comment, but don't see a comment button.
Notice, though, the second quote from Range("A1:B15")" & vbCr is started from "myChtObj.Chart.SetSourceData
Having the full block of code would help better, as we can't tell what kind of end statement you will need. I.e, your "End Sub" is in double quotes. If that's the end of the sub, you need to take them out.
Building strings by concatenation is cumbersome and errorprone. Especially, if the result is a multiline string, use Join:
strCode = Join( Array( _
"Private Sub AcclvsTime()" _
, " Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225)" _
, " myChtObj.Chart.ChartType = 4" _
, " myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets(""sheet2"").Range(""A1:B15"")" _
, "End Sub" _
), vbCrLf)
WScript.Echo strCode
output:
Private Sub AcclvsTime()
Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225)
myChtObj.Chart.ChartType = 4
myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")
End Sub
to reduce the noise caused by & and the repeating stuff vbCr(Lf). That will improve your chances to see the problems/mistakes. (Exactly two literals - "sheet2", "A1:B15" - to quote).
Addional Remark:
Given that the culprit is:
"whatever("sheet2").Range("A1:B15")"
it is obvious, that remedy
"whatever(""sheet2"").Range(""A1:B15"")"
is easier to read/check/write and less errorprone than
"whatever(" & Chr(24) & "sheet2" & Crh(34) & ").Range(" & Chr(34) + "A1:B15" & Chr(32) & ")"
Avoiding "" in literals by splicing in & Chr(34) &s is a bad strategy.
" ... Range("A1:B15")" & vbCr
Note those quotes within quotes on your fourth line (for both "sheet2" and "A1:B15") - you need to fix that.
If you want to put quotes within quotes, you can do it thus, by escaping. Two consecutive " characters within a double-quoted string will be translated to a single ".
"the word ""xyzzy"" is quoted"
Alternatively, you can also use chr(34) to get the quote:
"the word " & chr(34) & "xyzzy" & chr(34) & " is quoted"
This may be preferable in more complex cases, though I've rarely had a need for it.