Hi guys,
i want to add a character to the begining and the end of a multilne text box and save it into a file. e.g

1234
6789
9087

output
'1234',
'6789',
'9087'
i will appreciate the code to achieve this in vb6

Dani AI

Generated

This thread asked how to wrap each line of a multiline TextBox with single quotes and a comma and then save it to a file. gave the common Split+loop approach and pointed out adding the trailing comma. Below are a compact alternative and a few practical notes that help for real-world input (blank lines, embedded quotes, different line endings, encoding).

A one-pass way in VB6 is to use VBScript RegExp (add reference to "Microsoft VBScript Regular Expressions 5.5") to replace each non-empty line with a quoted, comma-terminated line:

' Reference Microsoft VBScript Regular Expressions 5.5
Dim re As RegExp
Set re = New RegExp
re.Pattern = "^\s*(.+)\s*$"
re.Global = True
re.MultiLine = True

Dim inputText As String
' populate inputText from textbox or file
Dim outText As String
outText = re.Replace(inputText, "'$1',")

Notes and tips:

  • To avoid a comma on the very last non-empty line, do a small post-process that trims trailing whitespace/newlines and drops a final comma if present.
  • If lines can contain single quotes and output is destined for SQL, escape them first (double the single quotes).
  • Normalize line endings before processing when input may come from Unix/Mac files.
  • To save the result: use VB6 file I/O (FreeFile + Open ... For Output + Print #) for ANSI. For UTF-8, write via ADODB.Stream.

These additions handle common edge cases while keeping the per-line transformation concise.

Recommended Answers

All 3 Replies

Hi,
U can do parse the Text first, then apply single quotes

To Parse

Dim MyArr 
   MyArr = Split (Text1.Text, vbCrLF)

Iterate through loop and add single quotes

Dim i as Integer
  Dim newString as String

  For i = 0 To UBound(MyArr)
     newString = newString & "'" & MyArr(i) & "'" & vbCrLf
  Next
  
  Text1.Text = newString

great code.
just to correct ;)

newString = newString & "'" & MyArr(i) & "'," & vbCrLf

Hi Jx_Man,
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.