i am trying to read from a text file and store the data in an array but i don't know how to get started with my code. can someone please get me started here.

i have start with the code for locating the file, but i am having difficulty reading the file.

Dim strFileName As String
strFileName = OpenAnswerFile.FileName
MessageBox.Show(strFileName) 'message box showing the file path

Recommended Answers

All 13 Replies

See if this helps.

'Dim OpenAnswerFile As New OpenFileDialog
        Dim strFileName() As String '// String Array.
        Dim tempStr As String = "" '// temp String for result.

        If OpenAnswerFile.ShowDialog = DialogResult.OK Then '// check if OK was pressed.
            strFileName = IO.File.ReadAllLines(OpenAnswerFile.FileName) '// add each line as String Array.
            For Each myLine In strFileName '// loop thru Arrays.
                tempStr &= myLine & vbNewLine '// add Array and new line.
            MsgBox(tempStr) '// display result.
        End If


Thanks for the feedback, it seems to work but the problem is it is not displaying the right results. i am trying to display this characters TFTTTTTTFFTFTFTTTTFF
To you why this is happening..


Not so clear on your reply.
Does your File only contain one line of "TFTTTTTTFFTFTFTTTTFF"?, and are you trying to separate each character into an array?

commented: BONEKA FULL BODY ELECTRIC ini terbuat dari bahan pilihan yang lembut, fleksibel dan berkualitas sehingga tidak menimbulkan penyakit dan iritasi pada kulit. UKURAN : panjang : 165cm berat : 4kg BONEKA FULL BODY ELECTRIC dapat di pompa dan di kemas, d +0

yes my file only contain this line of code "TFTTTTTTFFTFTFTTTTFF" .
What i am trying to do is read and store it in an array of Strings and then display the result.

Dim myCoolFile As String = "C:\test.txt" '// your file location.
        Dim tempStr As String = IO.File.ReadAllText(myCoolFile) '// read file into a String.
        Dim x As Integer = tempStr.Length - 1 '// get String length, Index based, starting at 0 not 1.
        Dim myArray(x) As String '// create your String Arrays.
        Dim i As Integer = 0 '// Integer used to increase Array #'s.
        For Each myChr As Char In tempStr '// loop thru each each character in the String.
            myArray(i) = myChr '// add values to each String Array.
            i += 1 '// increase count for next Array.
        MsgBox(myArray(2)) '// display result for 3rd character.

Thanks very much, appreciate your help

I am trying to do something almost exactly like this, but i want to load multiple characters. The array is being set-up by a prompt. The variables student and addstudent are defined in this block of code:

    Dim Prompt, Title As String
    Dim i As Short
    Prompt = "Enter your students' names."
    AddStudent = InputBox("How many students are in your class?")
    If AddStudent > 0 Then ReDim Student(AddStudent)
    Student(0) = AddStudent
    For i = 1 To UBound(Student)
        Title = "Student " & (i + 1)
        Student(i) = InputBox(Prompt, Title)

How do i save this then open it so that i can use the defined variables?
Any idea on how to do this?

commented: dumb.a.ss?:D or just.because?:) -3

ng5, start your own thread and Flag.Post your own dumb.a.ss after.:D

send me p.m. w/link to thread.after, I might be able to help.:)

Hi...I had a vb question here.....now i want compare input staff id with text file store id...so how i going to compare multiple staff id in text file?

This thread has been solved. Please start a new thread with an appropriate title and more detail than you provided here.

Dim myFSO As Scripting.FileSystemObject
Dim myTextStream As Scripting.TextStream
Dim myString As String

Set myFSO = New Scripting.FileSystemObject

If myFSO.FileExists(“filepath.txt”) Then
Set myTextStream = myFSO.OpenTextFile(“filepath.txt”, ForReading)
myString = myTextStream.ReadAll() ‘ file content will be available in myString
Call myTextStream.Close
End If

Set myTextStream = Nothing
Set myFSO = Nothing

Refer this link for more details

Probably better to avoid vbscript code in vb.net and just use the builtin file I/O capabilities.

Hi Guys and thanks for your insights. I am about to have a play with 22.7 trillion digits of pi and some of your code is an excellent start. Just wanted to say thanks.

Alan R Latchford

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.