Results 1 to 21 of 21

Thread: Reading form text file (RESOLVED)

Hybrid View

  1. #1
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Reading form text file

    Try:
    VB Code:
    1. Private Function SplitEx(ByVal sString As String, Optional ByVal sDelim As String = " ") As Variant
    2.     Dim sItem As String
    3.     Dim sList() As String
    4.     Dim lChar As Long
    5.     Dim lCount As Long
    6.    
    7.     Do
    8.       ' Reset Split for non-Quoted Values
    9.       lChar = InStr(sString, sDelim)
    10.        
    11.       If lChar > 0 Then
    12.           ' Extract the Item from the String
    13.           sItem = Left(sString, lChar - 1)
    14.           ' Remove Item from the Remaining String
    15.           sString = Mid(sString, lChar + Len(sDelim))
    16.       Else
    17.           ' Remaining String, becomes final Item
    18.           sItem = sString
    19.           sString = ""
    20.       End If
    21.        
    22.       ' If something was extracted, add it to the Array
    23.       If Len(sItem) > 0 Then
    24.           ReDim Preserve sList(lCount)
    25.           sList(lCount) = sItem
    26.           lCount = lCount + 1
    27.       End If
    28.        
    29.     ' Continue until no more delimiters are found.
    30.     Loop While lChar > 0
    31.    
    32.     ' If there were items, return the array
    33.     If lCount > 0 Then
    34.         SplitEx = sList
    35.     Else
    36.         ' Otherwise return an empty array
    37.         SplitEx = Array()
    38.     End If
    39. End Function
    40.  
    41. Private Sub Command1_Click()
    42.    Dim intFree As Integer
    43.    Dim strReadText As String
    44.    Dim strTextLines() As String
    45.    
    46.    intFree = FreeFile
    47.    Open "C:\SomeFile.txt" For Binary As #intFree
    48.        strReadText = Space$(LOF(intFree))
    49.        Get #intFree, , strReadText
    50.    Close #intFree
    51.    
    52.    ' Create array of text lines from the file...
    53.    strTextLines = SplitEx(strReadText, vbNewLine)
    54.    
    55.    Text1.Text = strTextLines(0) ' First line...
    56.    Text2.Text = strTextLines(1) ' Second line...
    57. End Sub
    Regards,

    - Aaron.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Reading form text file

    Hey Aaron,

    I'm still getting the same error...

    VB Code:
    1. [B]strTextLines =[/B] SplitEx(strReadText, vbNewLine)

    Compile error: "Can't assign to array"

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width