You are here:

Visual Basic/Hey Aaron, I'm still getting...

Advertisement


Question
Hey Aaron,

I'm still getting the same error...



strTextLines = SplitEx(strReadText, vbNewLine)



Compile error: "Can't assign to array"



I don't know...I'm about to give up.

-------------------------
Followup To
Question -
Hey Aaron,

I asked a question at VBForums, and instead of
typing it again, here is the link...

http://www.vbforums.com/showthread.php?p=2027435#post2027435

I hope you can help me, as this is driving me crazy!

Thanks,
Ron
Answer -
Try:

'-------------------------------------------------------
Private Function SplitEx(ByVal sString As String, Optional ByVal sDelim As String = " ") As Variant
   Dim sItem As String
   Dim sList() As String
   Dim lChar As Long
   Dim lCount As Long
   
   Do
     ' Reset Split for non-Quoted Values
     lChar = InStr(sString, sDelim)
       
     If lChar > 0 Then
         ' Extract the Item from the String
         sItem = Left(sString, lChar - 1)
         ' Remove Item from the Remaining String
         sString = Mid(sString, lChar + Len(sDelim))
     Else
         ' Remaining String, becomes final Item
         sItem = sString
         sString = ""
     End If
       
     ' If something was extracted, add it to the Array
     If Len(sItem) > 0 Then
         ReDim Preserve sList(lCount)
         sList(lCount) = sItem
         lCount = lCount + 1
     End If
       
   ' Continue until no more delimiters are found.
   Loop While lChar > 0
   
   ' If there were items, return the array
   If lCount > 0 Then
       SplitEx = sList
   Else
       ' Otherwise return an empty array
       SplitEx = Array()
   End If
End Function

Private Sub Command1_Click()
  Dim intFree As Integer
  Dim strReadText As String
  Dim strTextLines() As String
  
  intFree = FreeFile
  Open "C:\SomeFile.txt" For Binary As #intFree
      strReadText = Space$(LOF(intFree))
      Get #intFree, , strReadText
  Close #intFree
  
  ' Create array of text lines from the file...
  strTextLines = SplitEx(strReadText, vbNewLine)
  
  Text1.Text = strTextLines(0) ' First line...
  Text2.Text = strTextLines(1) ' Second line...
End Sub
'-------------------------------------------------------

Regards,

- Aaron.

Answer
Try changing "strTextLines" to a Variant data type instead of a String array, i.e.

'----------------------------------------------------------
Private Sub Command1_Click()
Dim intFree As Integer
Dim strReadText As String
'Dim strTextLines() As String
Dim strTextLines As Variant

intFree = FreeFile
Open "C:\SomeFile.txt" For Binary As #intFree
   strReadText = Space$(LOF(intFree))
   Get #intFree, , strReadText
Close #intFree

' Create array of text lines from the file...
strTextLines = SplitEx(strReadText, vbNewLine)

Text1.Text = strTextLines(0) ' First line...
Text2.Text = strTextLines(1) ' Second line...
End Sub
'----------------------------------------------------------

Regards,

- Aaron.

Visual Basic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Aaron Young

Expertise

I have 11 Years of Programming Experience, 4 years with Visual Basic, I currently work as an Analyst Programmer for Red Wing Business Systems, Inc. I enjoy helping others out when they get stuck and have spent a lot of time in other Forums like VB-World and CodeGuru answering a lot of questions. I have a good knowlegde of VB overall, so will attempt any Question on any part of the Language, from Database Access to ActiveX Control Creation and all the Grey Fuzzy stuff in between. (I do enjoy using the APIs for Subclassing and getting that last drop of functionality out of VB).

©2012 About.com, a part of The New York Times Company. All rights reserved.