0

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

Jean Camargo
  • 340
  • 3
  • 17
  • 1
    Does this answer your question? [Public Array Compile Error - Can't assign to array](https://stackoverflow.com/questions/68471511/public-array-compile-error-cant-assign-to-array) – GSerg Jul 21 '21 at 18:06
  • @GSerg unfortunately not, I am getting a Run-time error '13' Type mismatch – Jean Camargo Jul 21 '21 at 18:13
  • If you have redeclared `deliveryType` as Variant, then you should have not done that. You should have [removed the fixed size](https://stackoverflow.com/questions/68460458/is-it-possible-to-assign-values-to-array-without-looping-vba#comment120991520_68460458). – GSerg Jul 21 '21 at 18:17
  • So many errors here. This is only the beginning. – Bob77 Jul 21 '21 at 22:09
  • 1
    Don't set the size on the array... `Dim deliveryType(2) As String` should just be `Dim deliveryType() As String`. Then, the original should be fine. For the second, you could try `Dim deliveryType As Variant`, without `()`s, as Variant can already accept both arrays and non arrays. – User51 Jul 22 '21 at 13:08

1 Answers1

0
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.

Slugsie
  • 851
  • 1
  • 7
  • 17
Jean Camargo
  • 340
  • 3
  • 17