Results 1 to 9 of 9

Thread: import text file into access database table

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Question import text file into access database table

    if i got text file with the following data for example

    0,"first text","


    (rc)"
    1,"second text","a b c d"
    2,"",""
    3,,


    the first field inthe table is number the second field is text the third field is memo

    how can i insert into it from text file

    i need help about the code

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: import text file into access database table

    We know you're using a CSV file and we know you're using Access. You say you need help with code but what language? VB6? VB.NET? VBA? C#? Something else? Remember that we know NOTHING about what you're doing so you have to give us ALL the relevant information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: import text file into access database table

    i'm using bv.net 2010
    and i'm useing nomal text.txt file not csv
    and i save the table from the access database. but now i'm looking for away to insert it back into the table.all the ways i find so far is through datagridview..but i'm looking for direct way

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: import text file into access database table

    CSV stands for comma-separated values. You have a text file with a record per line (other than line breaks within fields) and field values separated by commas. That is a CSV file.

    You should populate a DataTable with your data. You can then use an OleDbDataAdapter to insert all the records into the database in a single batch. For reading the data in you have two choices: ADO.NET (OleDbDataAdapter or OleDbDataReader) or a TextFieldParser. For the ADO.NET examples, follow the CodeBank link in my signature and check out my Receiving & Saving Data thread. The one extra point to note is that, if you use a data adapter to load the data, set its AcceptChangesDuringFill property to False. The MSDN documentation for the TextFieldParser class has code examples if you want to go that way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: import text file into access database table

    ok i'm using this code but i get the error vshost32-clr2.exe has stopped working and then the application close

    vb Code:
    1. Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    2.               OpenFileDialog1.ShowDialog()
    3.         Dim filepath As String = OpenFileDialog1.FileName
    4.         Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath & ";user id=admin;jet oledb:database password=123")
    5.         AccessConn.Open()
    6.         Dim cmd As New OleDb.OleDbCommand("SELECT * INTO [import] FROM [Text;Database=C:\Users\BS\Desktop\;Hdr=No].[Messages.txt]", AccessConn)
    7.         cmd.ExecuteNonQuery()
    8.         AccessConn.Close()
    9.     End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: import text file into access database table

    I have already directed you to my CodeBank thread. Where in that thread do I use ExecuteNonQuery to execute a SELECT statement? In my previous post I specifically said to populate a DataTable using a data adapter or a data reader. Where are you doing that? I don't provide these instructions for fun. I provide them because they will solve your problem. If you don't follow the instructions I provide to help you then why am I here?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: import text file into access database table

    i dont know how to do DataTable from text file..
    and the problem is that the whole application close

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: import text file into access database table

    this is my final code and it still the program close when it excute
    vb Code:
    1. Imports System.IO
    2. Imports System.Data.OleDb
    3. Public Class Form1
    4.     Sub LoadData()
    5.         Dim objStreamReader As StreamReader = File.OpenText("C:\Users\BS\Desktop\Messages.txt")
    6.         While (objStreamReader.Peek() <> -1)
    7.             Dim Str1() As String = Split(objStreamReader.ReadLine(), ",")
    8.             AddRecord(Str1(0), Str1(1), Str1(2))
    9.         End While
    10.         objStreamReader.Close()
    11.     End Sub
    12.     Sub AddRecord(ByVal Field1 As String, ByVal field2 As String, ByVal field3 As String)
    13.         OpenFileDialog1.ShowDialog()
    14.         Dim filepath As String = OpenFileDialog1.FileName
    15.         Dim objConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath & ";user id=admin;jet oledb:database password=123")
    16.         Dim saveinto As New OleDb.OleDbCommand
    17.         saveinto.Connection = objConnection
    18.         saveinto.CommandType = CommandType.Text
    19.         saveinto.CommandText = " update Messages set Label= '" & field2.ToString & "' ,message='" & field3.ToString & "' WHERE Index = " & Field1.ToString()
    20.         objConnection.Open()
    21.         saveinto.ExecuteNonQuery()
    22.         objConnection.Close()
    23.     End Sub
    24.  
    25.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    26.         Me.LoadData()
    27.     End Sub
    28. End Class

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: import text file into access database table

    ok i got the code working now
    ther's just few things i think it wont be hard how can i get rid of the quotes of the text ""
    0,"first text"," "
    also if the text file contain unicode caracters like arabic or korean words it change to questions mark how to fix this

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