Results 1 to 9 of 9

Thread: From text file to array?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    From text file to array?

    When I write to a text file, it looks like this:

    Code:
    "45674576","45674567","thedate","testing"
    When reading the text file, how am I able to make what's between the first "" in a variable, second "" and so on? (I want to load one into a text box, one into a label, drop down menu etc).

    Thanks guys.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: From text file to array?

    VB Code:
    1. Option Explicit
    2. Private Sub Command1_Click()
    3.     Dim sFileText As String
    4.     Dim sArray() As String
    5.     'Assign the fileText context to SFileText
    6.     sArray = Split(sFileText, ",") 'split the content of the file with
    7.     'Respect to '
    8.     'Add the splited text to combo box
    9.     Combo1.AddItem Mid(sArray(0), 2, length(sArray(0).Text) - 1)
    10.     'Mid(sArray(0), 2, length(sArray(0).Text) - 1= will get the text between " "
    11. End Sub
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Re: From text file to array?

    Forgive my n00bness here.

    Code:
    Dim sFileText As String
        Dim sArray() As String
        Open FileLoc & jobNum & ext For Input As #1
        'Assign the fileText context to SFileText
        sArray = Split(sFileText, ",") 'split the content of the file with
        'Respect to '
        'Add the splited text to combo box
        Combo1.AddItem Mid(sArray(0), 2, length(sArray(0).Text) - 1)
        'Mid(sArray(0), 2, length(sArray(0).Text) - 1= will get the text between " "
        Close #1
    End Sub
    I am getting sarray as invalid qualifier.

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: From text file to array?

    Replace length with Len and remove .Text:
    VB Code:
    1. Combo1.AddItem Mid(sArray(0), 2, [B]Len[/B](sArray(0)) - 1)
    @danasegarane: are you drunk from Java or something?

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: From text file to array?

    Try this
    VB Code:
    1. Private Sub Command1_Click()
    2.    Dim sFileText As String
    3.    Dim i As Long
    4.     Dim sArray()
    5.     sFileText = FileText("Filename with path") 'Get the file text
    6.     'Assign the fileText context to SFileText
    7.     sArray = Split(sFileText, ",") 'split the content of the file with
    8.     'Respect to '
    9.     'Add the splited text to combo box
    10.   If UBound(sArray) > 0 Then 'If anything found in the array
    11.     For i = LBound(sArray) To UBound(sArray) 'Loop through the array and add the text
    12.     Combo1.AddItem Mid(sArray(i), 2,[B] Len[/B](sArray(i).Text) - 1)
    13.     'Mid(sArray(0), 2, length(sArray(0).Text) - 1= will get the text between " "
    14.     Next
    15.   End If
    16.  
    17. End Sub
    18.  
    19. Function FileText(ByVal filename As String) As String
    20.     'Function to get the filecontents into a string
    21.     Dim handle As Integer
    22.      
    23.        ' ensure that the file exists
    24.     If Len(Dir$(filename)) = 0 Then
    25.         Err.Raise 53  ' File not found
    26.     End If
    27.      
    28.        ' open in binary mode
    29.     handle = FreeFile
    30.     Open filename$ For Binary As #handle
    31.        ' read the string and close the file
    32.     FileText = Space$(LOF(handle))
    33.     Get #handle, , FileText
    34.     Close #handle
    35. End Function

    Edit:Sorry Gavio.It is mistake only
    Last edited by danasegarane; Feb 17th, 2007 at 06:53 AM.
    Please mark you thread resolved using the Thread Tools as shown

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Re: From text file to array?

    I am still getting the same error. I think you're over complicating this (no offense I appreciate the help!).

    Basically I don't want to worry about what's being done with the text just yet. What I want to do is:

    open text file.
    read data into array.
    array[0] = first part of text.
    array[1] = second.
    array[2] = third.
    array[2] = forth.
    labelone.caption = array[1]

    etc.

    From there I can do whatever I want with them, print etc.

    Sorry to be a pain, but your code is just frustrating me, you're obviously at a lot higher level than I am!

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: From text file to array?

    Change the line
    VB Code:
    1. Dim sArray() As String

    to
    VB Code:
    1. Dim sArray()

    it will work
    Please mark you thread resolved using the Thread Tools as shown

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Re: From text file to array?

    More errors. End If without If. For without Next, mismatch... list goes on :/

  9. #9
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: From text file to array?

    I have edited my previous post.Pls check it
    Please mark you thread resolved using the Thread Tools as shown

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