GrumP
GrumP

Reputation: 1203

Concatenate strings with vb6

I am trying to concatenate in vb6. The operator += is not supported, and I want to do something like the code below. I want to add more strings to a textbox as the program works down this code. Can anyone advise what to change += to? I know & can be used when adding one string to another, but it the example I am working on here, that doesn't seem suitable.

Thanks.

    If (strHomeNo <> "") Then
        txtPhoneNums = "Home: " + strHomeNo
    End If
    If (strMobileNo <> "") Then
        txtPhoneNums += "Mobile: " + strMobileNo
    End If
    If (strWorkNo <> "") Then
        txtPhoneNums += "Work: " + strWorkNo
    End If
    If (txtPhoneNums <> "") Then
        txtPhoneNums.ForeColor = vbBlack
        txtPhoneNums.FontBold = False
    End If
Else
     txtPhoneNums.Text = "NO CONTACT DETAILS"
     txtPhoneNums.ForeColor = vbRed
     txtPhoneNums.FontBold = True

Upvotes: 2

Views: 23699

Answers (5)

David
David

Reputation: 1

There are some more stylish ways of cocatenating multiple strings

"Multiline" Strings (I usually use for SQL):

Dim str as String
str = _
   " SELECT * " & _
   " FROM table " & _
   iif(flagActive, _
       " WHERE Status = 1 ", _
       " WHERE Status = 0 ") & _
   " ORDER BY Name DESC "

Or maybe when concatenating a greater and variable amount, use a Collection Object:

Dim result as String, fieldsCollection as New Collection, a as Integer
With fieldsCollection
    If (strHomeNo <> "") Then
        .Add("Home: " & strHomeNo)
    End If
    If (strMobileNo <> "") Then
        .Add("Mobile: " & strMobileNo)
    End If
    For a = 0 To .Count - 1
        result = result & .Item(a) 
    Next a
End With

Upvotes: 0

Mr . Pvg
Mr . Pvg

Reputation: 15

VB6 uses & to concatenate strings

Upvotes: 0

Tom Collins
Tom Collins

Reputation: 4069

@David's & @Brant's answers are correct. However, if you find yourself doing a lot of concatenations, then you can build a class to make things easier for you. Something like: txtPhoneNums.Add("Mobile: ", strMobileNo). I use one to build my SQL statements.

Upvotes: 2

Brandt Solovij
Brandt Solovij

Reputation: 2134

would :

txtPhoneNums = txtPhoneNums & "Work: " & strWorkNo

not work?

Upvotes: 8

David M
David M

Reputation: 72850

In VB6, you concatenate strings with the & operator as you say. I don't remember there being a shorthand &= (it's been a while), so you'd need:

txtPhoneNums = txtPhoneNums & "Mobile: " & strMobileNo

Don't think there's a better way.

Upvotes: 2

Related Questions