Results 1 to 7 of 7

Thread: [RESOLVED] save access table to text file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Resolved [RESOLVED] save access table to text file

    i have a comma delimitted text file that i need to add three fields and update the contents of those three fields and then save the text file. what would be the best way to do this?

    right now i bring the file into ms access and alter the table to add the fields and then update them but i don't know how to export the access table back into a comma delimitted text file. anybody know how to do this?

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Re: save access table to text file

    this is how i import the text file into an access db:

    vb.net Code:
    1. 'import text file into access db
    2. sql.CommandText = "SELECT * INTO [tblMailList] FROM [Text;DATABASE=" & FilePath & ";HDR=NO].[" & FileName & "]"
    3. sql.ExecuteNonQuery()

    can i do something like this to put it back into a text file?

  3. #3
    New Member
    Join Date
    Jun 2009
    Posts
    8

    Re: save access table to text file

    well I'm not sure how you are adding the values once you add the fields. I think the faster way would be to use Excel and save as CSV. here is a program that does it too
    Code:
    Public Sub loadcsv()
    
            Dim holdingclass As holdingclass = New holdingclass
            Dim holdingclasslist As ArrayList = New ArrayList
            ' since i dont know how many values you have in your csv I'll assume at least two 
            ' i built a class that has 4 values
            Dim filename As String = "C:\Test.csv"
            Dim fields As String()
            Dim delimiter As String = ","
            Using parser As New TextFieldParser(filename)
                parser.SetDelimiters(delimiter)
                While Not parser.EndOfData
                    'instantiate a new instance of your holding class
                    holdingclass = New holdingclass
    
                    ' Read in the fields for the current line
                    fields = parser.ReadFields()
    
                    holdingclass.Class_id = fields(0)
                    holdingclass.value_2 = fields(1)
                    holdingclass.value_3 = fields(2)
    
                    ' now that you have saved the data you have read in for your class stor it in an arraylist 
                    ' so later you can iterate through the list adding your other values.
                    holdingclasslist.Add(holdingclass)
    
                End While
            End Using
    
            ' now I'm assuming you have a way to identify each line you read in by an Id 
            ' lets say class_ID is your ID andit is the first value in  your CSV
            ' you did not specify how you wanted to add the values to the first csv so if you had them
            ' in another file you could just loop through  your list looking for the matching ID.
            ' while reading in the second file.
            filename = "C:\Test_2.csv"
            Dim fields2() As String
            delimiter = ","
            Using parser As New TextFieldParser(filename)
                parser.SetDelimiters(delimiter)
                While Not parser.EndOfData
                    ' Read in the fields for the current line
                    fields2 = parser.ReadFields()
                    For Each record As holdingclass In holdingclasslist
                        If record.Class_id = fields2(0) Then
                            record.value_4 = fields2(1)
                            record.value_5 = fields2(2)
                            record.value_6 = fields2(3)
                            Exit For
                        End If
                    Next
                End While
            End Using
            'now that you have your original file and your added fields in the same class you can iterate through the classlist 
            ' printing the CSV 
            For Each H_class As holdingclass In holdingclasslist
                Dim writeline As String = H_class.Class_id & "," & H_class.value_2 & "," & H_class.value_3 & "," & H_class.value_4 & "," & H_class.value_5 & "," & H_class.value_6 & vbCrLf
                My.Computer.FileSystem.WriteAllText("C:\Combined.csv", writeline, True)
            Next
    
        End Sub
    here is the holder class

    Code:
    Public Class holdingclass
        Private ClassId, value2, value3, value4, value5, value6 As String
    
        Public Property Class_id() As String
            Get
                Return ClassId
            End Get
            Set(ByVal value As String)
                ClassId = value
            End Set
        End Property
        Public Property value_2() As String
            Get
                Return value2
            End Get
            Set(ByVal value As String)
                value2 = value
            End Set
        End Property
        Public Property value_3() As String
            Get
                Return value3
            End Get
            Set(ByVal value As String)
                value3 = value
            End Set
        End Property
        Public Property value_4() As String
            Get
                Return value4
            End Get
            Set(ByVal value As String)
                value4 = value
            End Set
        End Property
    
        Public Property value_5() As String
            Get
                Return value5
            End Get
            Set(ByVal value As String)
                value5 = value
            End Set
        End Property
        Public Property value_6() As String
            Get
                Return value6
            End Get
            Set(ByVal value As String)
                value6 = value
            End Set
        End Property
    End Class
    hope that gives you an idea
    Last edited by mbarnett; Jun 16th, 2009 at 10:16 AM.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Re: save access table to text file

    i already know how to add the fields. i just need to know how to export the data into a comma delimitted text file. this is my code:
    vb.net Code:
    1. txtDataAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [tblMailList]", cnxn)
    2.  
    3. TextFileCommandBuilder = New System.Data.OleDb.OleDbCommandBuilder(txtDataAdapter)
    4.  
    5. 'clean up old data if necessary
    6. cnxn.Open()
    7. sql.Connection = cnxn
    8.  
    9. Dim SchemaTable = cnxn.GetSchema("TABLES", New String() {Nothing, Nothing, "tblMailList"})
    10.      If SchemaTable.Rows.Count <> 0 Then
    11.           sql.CommandText = "DROP TABLE [tblMailList]"
    12.           sql.ExecuteNonQuery()
    13.      End If
    14.  
    15. 'import text file into access db
    16. sql.CommandText = "SELECT * INTO [tblMailList] FROM [Text;DATABASE=" & FilePath & ";HDR=NO].[" & FileName & "]"
    17. sql.ExecuteNonQuery()
    18.  
    19. 'add fields
    20. sql.CommandText = "ALTER TABLE [tblMailList] ADD COLUMN [ID] AUTOINCREMENT;"
    21. sql.ExecuteNonQuery()
    22. sql.CommandText = "ALTER TABLE [tblMailList] ADD COLUMN [CLUBADDR] CHAR(100);"
    23. sql.ExecuteNonQuery()
    24. sql.CommandText = "ALTER TABLE [tblMailList] ADD COLUMN [CLUBCSZ] CHAR(100);"
    25. sql.ExecuteNonQuery()
    26. sql.CommandText = "ALTER TABLE [tblMailList] ADD COLUMN [CLUBPHONE] CHAR(100);"
    27. sql.ExecuteNonQuery()
    28. sql.CommandText = "ALTER TABLE [tblMailList] ADD PRIMARY KEY (ID);"
    29. sql.ExecuteNonQuery()
    30.  
    31. 'fill data tables
    32. txtDataAdapter.Fill(tblTextFile)
    33.  
    34. For i = 0 To tblTextFile.Rows.Count - 1
    35.      Dim ModifyRow As DataRow = tblTextFile.Rows(i)
    36.      ModifyRow("CLUBADDR") = TextBoxClubAddress.Text
    37.      ModifyRow("CLUBCSZ") = TextBoxClubCSZ.Text
    38.      ModifyRow("CLUBPHONE") = TextBoxClubPhone.Text
    39. Next i
    40. txtDataAdapter.Update(tblTextFile)

  5. #5
    New Member
    Join Date
    Jun 2009
    Posts
    8

    Re: save access table to text file

    if your dead set on using access check this out
    http://www.mredkj.com/netconversions/dth.html

    I think you could do it with excel with a lot less effort.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Re: save access table to text file

    i just think access provides an easy way to add 3 fields and then update those 3 fields with data that was input from the user. i don't have an id field so i add the autonumber field and set it as the primary key so i can do my updates.

    1. open comma delimitted text file (fields are enclosed by quotes)
    2. add three fields to the end of each line of data
    3. update each of those three fields with the data the user enters in textbox
    4. save text file in original location

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Re: save access table to text file

    This is how i ended up doing it. There is no need for access. This way is short, simple and fast.

    vb.net Code:
    1. Dim txtString() As String = IO.File.ReadAllLines(ofd.FileName)
    2. For i = 0 To txtString.Length - 1
    3.      txtString(i) = txtString(i) & ",""" & TextBoxClubAddress.Text & """,""" & TextBoxClubCSZ.Text & """,""" & TextBoxClubPhone.Text & """"
    4. Next
    5. IO.File.WriteAllLines(ofd.FileName, txtString)

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