Results 1 to 4 of 4

Thread: Loading a Csv

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Location
    England, Devon, Honiton
    Posts
    18

    Cool Loading a Csv

    Dear all.

    Yesterday 4x2y very kindly gave me a solution to saving to and load from a csv.txt file

    The save worked well, I can now easily save numerous textbox.text(s) with easy code. Thanks 4x2y

    The code:-
    Bla Bla

    Dim SWriter As StreamWriter = New StreamWriter(SaveFileDialog.FileName)

    SWriter.Write(TxtBx1.Text & ",")
    SWriter.Write(TxtBx2.Text)

    SWriter.Close()

    The open though was not so easy when extended to many textbox. text loads (fills)

    Bla Bla

    Dim SReader As StreamReader = New StreamReader(OpenFileDialog.FileName)

    Dim r() As String = SReader.ReadToEnd.Split(",")
    TxtBx1.Text = r(0)
    TxtBx2.Text = r(1)

    SReader.Close()

    This load solution works, BUT it is going to be difficult to manage when one has say 1000 pieces separated by "," of information to reads from the csv.txt file.

    Would be great to see another load solution that is easier to manage, i.e does not require an incrementing variable like r in this code.

    Would be fantastic to see the code / syntax of this.

    Thanks.
    Donovan

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Loading a Csv

    There is an easier way ... the TextFieldParser class
    It great for working with delimted files, even fixed width... and it handles quoted text with commas nicely too (you just have to tell it that that is the case) ... you ca read a singe field or an entire line (which results in an array)....

    now, if the problem is getting the data out of the array into the textboxes, there's a couple ways to handle that. you could set the Tag property at design time to the index value where in the array it is (so TxtBx1.Tag would be 0; TxtBx2.Tag would be 1... and so on).

    there's a couple different ways to deal with it, depending on what you're after exactly. How ever, if you have 1,000's of text boxes... I'd question whether textboxes really are the interface you want.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Location
    England, Devon, Honiton
    Posts
    18

    Re: Loading a Csv

    Hi Tg

    I will have a look at the text field parser class though i have no idea at the moment what that is. Thank you. The information i want to save is not all from text boxes, I need to save data in a text file. Some is from textboxes but the vast majority will be from data brought in from databases (access) placed in an object with twenty or so criteria each.

    I am rewriting a programme i had built many years ago. The save in this old programme looked like this:

    globals.kitchen.Save (1)

    Public Sub Save(fileNum As Integer)


    Write #fileNum, "KD Version " & app.Major & "." & app.Minor
    Write #fileNum, Me.ApplianceFitting
    Write #fileNum, Me.applianceMarkup
    Write #fileNum, Me.CabinetType
    Write #fileNum, Me.CorniceExtra
    Write #fileNum, Me.DeliveryNetCharge
    Write #fileNum, Me.DoorDescription
    Write #fileNum, Me.doorstyle
    Write #fileNum, Me.doorsupplier
    Write #fileNum, Me.FittingNotes

    Then it moves on to saving the objects

    the text file reads loads of information all separated by ","

    This then loads



    globals.kitchen.load 1
    Public Sub load(fileNum As Integer)

    Input #fileNum, tempString
    Input #fileNum, mvarApplianceFitting
    Input #fileNum, mvarapplianceMarkup
    Input #fileNum, mvarCabinetType
    Input #fileNum, mvarCorniceExtra
    Input #fileNum, mvarDeliveryNetCharge
    Input #fileNum, mvarDoorDescription
    Input #fileNum, mvarDoorStyle
    Input #fileNum, mvarDoorSupplier
    Input #fileNum, mvarFittingNotes
    Input #fileNum, mvarOfficeNotes
    Input #fileNum, mvarFactoryNotes
    Input #fileNum, mvarClientNotes
    Input #fileNum, mvarMarkUp1
    Input #fileNum, mvarMarkUp2
    Input #fileNum, mvarMarkUp3
    Etc

    So open and close a text file basically.

    I resolved this after many hours on you tube three or so weeks ago with a format very similar to this old format, but hten accidently wrote over the project. Having spent three weeks trying to find the youtube again i am well frustrated.

    Regards

    donovan

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Loading a Csv

    Since you don't have to use comma delimited files, don't. Use a grid for the user interface then use a data table to read/write xml.
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' Be sure to create a new data set in your project name 'Container'  then create a table in the data set named 'Fields' add what ever fields you want to display here
            '  Also drag a datagridview from the tool box onto your form name it 'Grid', also drop a button named 'SaveButton' and a button named 'LoadButton'
            Me.Grid.DataSource = New Container.FieldsDataTable
        End Sub
    
        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
            Dim fields = DirectCast(Me.Grid.DataSource, Container.FieldsDataTable)
            ' static file name use only for example, replace with dialog selection
            fields.WriteXml("C:\temp\fielddata.txt")
        End Sub
    
        Private Sub LoadButton_Click(sender As Object, e As EventArgs) Handles LoadButton.Click
            Dim fields = DirectCast(Me.Grid.DataSource, Container.FieldsDataTable)
            ' static file name use only for example, replace with dialog selection
            fields.ReadXml("C:\temp\fielddata.txt")
        End Sub
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

Tags for this Thread

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