Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading a text file and using openFileDialog 1

Status
Not open for further replies.

spizotfl

MIS
Aug 17, 2005
345
0
0
US
hi, i am very new to vb. i get basic syntax and the like...
what i am trying to do:
we upload text files to a state agency. any records in the file that contain errors bounce back to us in a text file with the error code at the end of the file. the error file is not delimited in any way, it is just fixed width. i have managed to hack together something using many of the various msdn help and samples that will allow me to hardcode the filename of the source and destination. this runs pretty much as a quicky console app with almost no user input. i am trying to make a form that will allow the user to select what type of error file it is (eventually we will deal with 6-8 different types), probably by using an option group with radio buttons, and the openFileDialog to browse to the location of the error file.
now the problem: in the console you can use StreamReader and StreamWriter to easily do the basics of pulling the info from the textfile, but with the openFileDialog, VB Express says I need a Stream, not a StreamReader to work with it. How do I do this??
This is what i have for the "console" version.
Code:
Imports System
Imports System.IO
Imports Microsoft.VisualBasic.Strings

Class Test
	Public Shared Sub Main()
        Try
            ' Create an instance of StreamReader to read from a file.
            Dim sr As StreamReader = New StreamReader("C:\qryDemographic_1-27-06 412.txt")
            Dim sw As StreamWriter = New StreamWriter("C:\ErrorFile.txt")
            Dim line As String
            Dim column1 As String
            Dim column2 As String
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do
                line = sr.ReadLine()
            	column1 = Mid(line, 11,9)
            	column2= mid(Line,165)
            	sw.WriteLine(column1 & " " & column2)
            Loop Until line Is Nothing
            sr.Close()
			sw.Close()
        Catch d As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(d.Message)
        End Try
        

    End Sub
End Class
If there is a better way to manage the GUI pieces, i would love any guidance.
Thanks for any help....
 
A basic example could be something like this which is the code you provided but instead of reading from the file defined in the code, it would be selected from the file open dialog.:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.OpenFileDialog1.ShowDialog()
End Sub
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        ' Create an instance of StreamReader to read from a file.
        Try

            Dim sr As StreamReader = New StreamReader(Me.OpenFileDialog1.FileName)
            Dim sw As StreamWriter = New StreamWriter("C:\ErrorFile.txt")
            Dim line As String
            Dim column1 As String
            Dim column2 As String
            ' Read and display the lines from the file until the end
            ' of the file is reached.
            Do
                line = sr.ReadLine()
                column1 = Mid(line, 11, 9)
                column2 = Mid(line, 165)
                sw.WriteLine(column1 & " " & column2)
            Loop Until line Is Nothing
            sr.Close()
            sw.Close()
        Catch d As Exception
            ' Let the user know what went wrong.
            MsgBox("The file could not be read:" & vbnewline & d.Message)
        End Try
    End Sub

does that help?
 
i think it is close. first it complained about not having the openFileDialog1 declared, so i did that just inside of the class. Now the only complaint is about the Handles at the end of the:
Private Sub openFileDialog1_FileOk...
line.
The message is "Handles clause requires a WithEvents variable defined in the containing type or one of its base types."
 
i commented out the Handles portion of the line and things compiled ok, the only problem is no output file.
 
I am not sure how you are creating this project, if you are using Visual Studio or what but I didnt display the the whole file.


If you are using visual studio you want ot create a new form and add a button to it. Also add the Fileopen dialog to it, they are both on the toolbar in VS.

Then to properly get the events to fire correctly, double click the button, this will be something like this
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub

add these line in that Sub

'Opens a File Dialog Control
Me.OpenFileDialog1.ShowDialog()

Then double click the OpenFileDialog diagram, which will allow you to put the onclick handle function


If you arent using Visual Studio - I believe the error you had before commenting out the Handles section is because you have to declare the file open dialog with event

IE
Code:
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
 
awesome, thanks. i am using the VB 2005 express edition to try and learn some of this stuff. i appreciate the help, and your help will at least make one thing a little nicer at work.
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top