Results 1 to 14 of 14

Thread: Help Text To Columns

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Help Text To Columns

    Hello ,
    I wanted to make a Program With VB that makes the text file to Columns (Comma)
    I have added 1 Button to open the file, 1 Button to Save the File.
    Now i want to make a 3rd Button to makes the File i open (.txt or .csv) to Columns.
    I have Searched All the google but couldn't find anything for it.

    Thanks.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Help Text To Columns

    You can read data from a delimited text file using TextFieldParser class.

    Example
    Code:
    Public Class Form1
        Private Sub Demo()
            Dim Errors As New List(Of String)
            Dim FileName As String = "Path and file name go here"
    
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn With {.ColumnName = "ID", .DataType = GetType(Int32), .AutoIncrement = True})
            dt.Columns.Add(New DataColumn With {.ColumnName = "FirstName", .DataType = GetType(String)})
            dt.Columns.Add(New DataColumn With {.ColumnName = "LastName", .DataType = GetType(String)})
    
            Dim Index As Int32 = 1
    
            If Not IO.File.Exists(FileName) Then
                Errors.Add("Failed to locate '" & FileName & "'")
            End If
    
            Using Reader As New FileIO.TextFieldParser(FileName)
    
                Reader.TextFieldType = FileIO.FieldType.Delimited
                Reader.Delimiters = New String() {","}
    
                Dim Line As String()
    
                While Not Reader.EndOfData
                    Try
                        Line = Reader.ReadFields()
                        If Line.Count = 3 Then
                            dt.Rows.Add(New Object() {CInt(Line(0)), Line(1), Line(2)})
                        Else
                            Errors.Add(
                                String.Format("Line {0}", Reader.LineNumber - 1))
                        End If
                        Index += 1
                    Catch ex As FileIO.MalformedLineException
                        Errors.Add(ex.Message)
                    Catch ex As Exception
                        Errors.Add(ex.Message)
                    End Try
                End While
    
            End Using
    
            If Errors.Count > 0 Then
                MessageBox.Show("See IDE output window for errors")
                For Each item In Errors
                    Console.WriteLine(item)
                Next
            End If
        End Sub
    End Class
    People.txt
    Code:
    160,Bob,Baloyi
    161,Esi,Baloyi
    162,Nomble,Ajumogoboa
    163,Matwa,Toe
    165,Abioye,Gaum
    166,Akwokwo,Ebeid
    167,Gakere,Hirse
    168,Kanoni,Kuti
    169,Mtawa,Maina
    170,Bimbaya,Maina
    171,Kadiri,Aliero
    172,Batuuli,Okafor
    173,Jaafar,Guéï
    174,Falala,Ndanusa
    175,Tarishi,Abimbola
    176,Eidi,Yeboah
    177,Masamba,Guéï
    178,Ukarimu,Adu
    Last edited by kareninstructor; May 11th, 2013 at 01:12 PM. Reason: Revised code block

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: Help Text To Columns

    Thanks for your answer
    I have tried the code u posted but i get too many errors
    That's what i get when i paste the code to the button (http://tinypic.com/view.php?pic=az9921&s=5)
    And then i fixed the code but an error showed up when i click the button (http://tinypic.com/view.php?pic=2q8d4li&s=5)

    Thanks.
    And sorry im little Newbie with VB

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Help Text To Columns

    I have updated the code. Any time you allow the IDE to generate items it is prudent to look at them, especially "FileName" as the core thing here is working on a file :-)

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: Help Text To Columns

    Thanks for reply,
    I have tried the updated code, I pasted the code to the Button, no errors poped up, but it did nothing.
    I mean i click the button and nothing happend

    Thanks.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Help Text To Columns

    Quote Originally Posted by DekaHax123 View Post
    Thanks for reply,
    I have tried the updated code, I pasted the code to the Button, no errors poped up, but it did nothing.
    I mean i click the button and nothing happend

    Thanks.
    That is right, nothing visibly happened, instead you have a DataTable populated with data that you need to select something like a DataGridView to display the data in.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: Help Text To Columns

    Thanks for reply,
    I have added DataGridView and make some columns but it still does not work :|
    I don't know what's the problem..
    Thanks.

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Help Text To Columns

    Quote Originally Posted by DekaHax123 View Post
    Thanks for reply,
    I have added DataGridView and make some columns but it still does not work :|
    I don't know what's the problem..
    Thanks.
    Hard to say but here is a VS2010 example that works using code I provided already
    TextFieldParser_VB_Forum1.zip

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: Help Text To Columns

    Thanks, That worked but i have a problem with it.

    ex: It can't read the first line : TEST,TEST,TEST
    0,6000,100
    1,6000,100
    2,6000,100
    3,6000,100
    4,6000,100
    5,6000,99
    6,6000,98

    With that first line (TEST,TEST,TEST a msg pops up "See IDE output window for errors"
    But without the first line (TEST,TEST,TEST) it works fine

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Help Text To Columns

    You have to account for if the first line is column names or some other type of data other than the bulk of the data. What exactly is the structure of your file?

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: Help Text To Columns

    What do you mean man?
    By the way the file is .CSV and it opens it if the file is with numbers...
    And is there a way to add that to the button?
    I mean click the button and select the file to open
    Thanks for helping me.

  12. #12
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Help Text To Columns

    What is not clear?
    In regards to .CSV, that is simply a file extension, a .TXT file can have the exact same structure and data as a .CSV file.

    If your first row are column names try
    Code:
    Imports System.Data.OleDb
    Public Class DemoReadTxt
        Dim FileName As String = "People.txt"
        Private Sub DemoReadTxt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Builder As New OleDbConnectionStringBuilder With
                {
                    .Provider = "Microsoft.Jet.OLEDB.4.0",
                    .DataSource = Application.StartupPath & IO.Path.DirectorySeparatorChar
                }
            Builder.Add("Extended Properties", "text;HDR=Yes;FMT=Delimited(,)")
            Using cn As New OleDbConnection With
                {
                    .ConnectionString = Builder.ConnectionString
                }
                Using cmd As New OleDbCommand With
                    {
                        .Connection = cn,
                        .CommandText =
                        <SQL>
                            SELECT * 
                            FROM <%= FileName %>
                        </SQL>.Value
                    }
                    Dim dt As New DataTable
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
                End Using
            End Using
        End Sub
    End Class
    That is if there are the same amount of TEST,TEST,TEST etc as there are columns in the actual data.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    17

    Re: Help Text To Columns

    Thanks Kevininstructor for sharing the code to convert the text into columns on a single click.

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: Help Text To Columns

    Nvm man i quit it.
    It cannot be find "Event Load cannot be found"
    Thanks for all your help man

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