Search code examples
arraysvb6

VB6 Split string into an array Error "Can't assign into array" compile error


I am trying to split a string into an array in VB6.

The string is stored in a database and looks like this:

"By Value : "

Sometimes it may include something more at the end after the colon, this is why I want to split it as I am comparing it in an if statement, as shown below.

Overall my code goes like this:

Dim deliveryType(2) As String

deliveryType = Split(vaGoodsInLine.FieldValue("Comment"), ":")

If deliveryType(0) = "By Value " Then
 'Do Something
End IF

I am getting the following error

enter image description here

I also tried defining the array as a variant with no size like this:

Public deliveryType() As Variant

But then I get this error deliveryType


Solution

  • Dim deliveryType() As String
    deliveryType = Split(vaGoodsInLine.FieldValue("Comment"), ":")
    Dim deliveryTypeValue As String
    
    If Not UBound(deliveryType) = 0 Then
        deliveryTypeValue = NullStr(deliveryType(0))
    End If
        
    If deliveryTypeValue = "By Value" Then
        'Do Something
    End IF
    

    This worked for me.