Results 1 to 3 of 3

Thread: Creating a Tab Delimited File Using Visual Basic.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    9

    Thumbs up Creating a Tab Delimited File Using Visual Basic.NET

    Hello,

    I've created a program that will allow me to save user input into a text file on my C:\ drive. I'm trying to figure out a way where the text will be saved using a tab delimiter (I'm still new to Visual Basic.NET 2003). What I have to do is get the information to save in the form of columns and rows so that when the text file is opened using Microsoft Excel (ex. Microsoft Excel 2007), all of the information will be saved in its own columns and rows without any manual intervention (ex. no changing column widths or anything like that).

    Here is my code so far.

    HTML Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
    
        End Sub
    
        Private Sub txtPhone_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPhone.TextChanged
            lblMessage.Text = "Your phone number is " & txtPhone.Text & "."
        End Sub
    
    
        Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            lblMessage.Text = "Your name is " & txtName.Text & "." & "Your mailing address is " & txtMailing.Text & ". " & "Your email address is " & txtEmail.Text & ". " & "Your phone number is " & txtPhone.Text & ". " & "Your student ID/Social is " & txtSocialID.Text & ". " & "Your gender is " & txtGender.Text & " ." & "Your race is " & txtRacial.Text & ". " & "Your age is " & txtAge.Text & ". "
            'the above displays the information about the user'
    
    
            Dim TargetFile As StreamWriter
    
            Try
                TargetFile = New StreamWriter("C:\DataInfo.txt", True)
            Catch
                MessageBox.Show("Error opening " & "C:\DataInfo.txt")
            End Try
    
            Try
                TargetFile.WriteLine(Now())
                TargetFile.Write("Name: " & txtName.Text & vbTab)
    
                TargetFile.Write("Mailing Address: " & txtMailing.Text & vbTab)
    
                TargetFile.Write("Email: " & txtEmail.Text & vbTab)
    
                TargetFile.Write("Confirm Email: " & txtEmail2.Text & vbTab)
    
                TargetFile.Write("Phone: " & txtPhone.Text & vbTab)
    
                TargetFile.Write("Student ID: " & txtSocialID.Text & vbTab)
                TargetFile.Write("Gender: " & txtGender.Text & vbTab)
                TargetFile.Write("Ethnicity: " & txtRacial.Text & vbTab)
                TargetFile.Write("Glasses: " & txtGlasses.Text & vbTab)
                TargetFile.Write("Age: " & txtAge.Text & vbTab)
                TargetFile.Write("Returning: " & txtReturning.Text & vbTab)
                TargetFile.Write("Notes: " & txtNotes.Text & vbTab)
    
                TargetFile.WriteLine()
                TargetFile.WriteLine()
            Catch
                MessageBox.Show("Error writing file")
            End Try
    
            TargetFile.Close()
            MessageBox.Show("Text saved to " & "C:\DataInfo.txt")
    
    
    
        End Sub
    
    
    
    
        Private Sub txtGender_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGender.TextChanged
            lblMessage.Text = "You have entered " & txtGender.Text & "."
        End Sub
    
        Private Sub txtGlasses_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGlasses.TextChanged
            lblMessage.Text = "You have entered " & txtGlasses.Text & "."
        End Sub
    
        Private Sub txtAge_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAge.TextChanged
            lblMessage.Text = "You have entered " & txtAge.Text & "."
        End Sub
    
        Private Sub txtReturning_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtReturning.TextChanged
            lblMessage.Text = "You have selected " & txtReturning.Text & "."
        End Sub
    
        Private Sub txtRacial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRacial.TextChanged
            lblMessage.Text = "You have entered " & txtRacial.Text & "."
        End Sub
    
        Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
            lblMessage.Text = "Your name is " & txtName.Text & "."
        End Sub
    
        Private Sub txtMailing_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMailing.TextChanged
            lblMessage.Text = "Your mailing address is " & txtMailing.Text & "."
        End Sub
    
        Private Sub txtEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
            lblMessage.Text = "Your email address is " & txtEmail.Text & "."
        End Sub
    
        Private Sub txtEmail2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail2.TextChanged
            lblMessage.Text = "You have confirmed your email address as " & txtEmail2.Text & "."
        End Sub
    
        Private Sub txtSocialID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSocialID.TextChanged
            lblMessage.Text = "Your social/student ID is " & txtSocialID.Text & "."
        End Sub
    
        Private Sub txtNotes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNotes.TextChanged
            lblMessage.Text = "Your notes are the following: " & txtNotes.Text & "."
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            txtName.Text = ""
            txtMailing.Text = ""
            txtEmail.Text = ""
            txtEmail2.Text = ""
            txtPhone.Text = ""
            txtSocialID.Text = ""
            txtGender.Text = ""
            txtRacial.Text = ""
            txtGlasses.Text = ""
            txtAge.Text = ""
            txtReturning.Text = ""
            txtNotes.Text = ""
        End Sub
    End Class
    
    When opening the text file using Microsoft Excel, Excel should display the data like this:

    Name | Mailing Address | Age
    John | 2300 J. Street | 19


    Can someone please help me modify my code (so that the information will be using a tab delimiter) whereas the text can open up in the right rows and columns in Excel after a user has submitted the data to the text file. Any help is greatly appreciated.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,727

    Re: Creating a Tab Delimited File Using Visual Basic.NET

    A tab-delimited file is just a text file with tab characters between each data field, so you simply write a tab character after each data field, e.g.
    vb.net Code:
    1. myStreamWriter.Write(data)
    2. myStreamWriter.Write(ControlChars.Tab)
    That said, you want to write a line break after the last data field in a row rather than a tab, so you need some logic to do that. The best way depends on the circumstances but you could do something like this:
    vb.net Code:
    1. myStreamWriter.WriteLine(String.Format("{1}{0}{2}{0}{3}", ControlChars.Tab, data1, data2, data3))
    or something like this:
    vb.net Code:
    1. myStreamWriter.WriteLine(String.Join(ControlChars.Tab, dataArray))

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    9

    Re: Creating a Tab Delimited File Using Visual Basic.NET

    Thanks for the response. I hope you don't mind me asking this (I'm new to Visual Basic altogether), but could you tell me what I should delete/modify in my code.

    I understood the part where you explained about the line break and control tabs; I'm just trying to figure out where to place everything to make the code run and work. Also, should I still keep this portion of the code as well? Thanks.

    Code:
    Dim TargetFile As StreamWriter
    
            Try
                TargetFile = New StreamWriter("C:\DataInfo.txt", True)
            Catch
                MessageBox.Show("Error opening " & "C:\DataInfo.txt")
            End Try
    
            Try
    
                TargetFile.WriteLine(Now())
                TargetFile.Write(txtName.Text & vbTab)
    
                TargetFile.Write(txtMailing.Text & vbTab)
    
                TargetFile.Write(txtEmail.Text & vbTab)
    
                TargetFile.Write(txtEmail2.Text & vbTab)
    
                TargetFile.Write(txtPhone.Text & vbTab)
    
                TargetFile.Write(txtSocialID.Text & vbTab)
                TargetFile.Write(txtGender.Text & vbTab)
                TargetFile.Write(txtRacial.Text & vbTab)
                TargetFile.Write(txtGlasses.Text & vbTab)
                TargetFile.Write(txtAge.Text & vbTab)
                TargetFile.Write(txtReturning.Text & vbTab)
                TargetFile.Write(txtNotes.Text & vbTab)
    
                TargetFile.WriteLine()
                TargetFile.WriteLine()
    
    
            Catch
                MessageBox.Show("Error writing file")
            End Try
    
            TargetFile.Close()

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