Results 1 to 8 of 8

Thread: Importing a *.txt file to a Access database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    85

    Question Importing a *.txt file to a Access database

    I have attached a file which I exported from a access database. Now I need to find a way of importing that table again?.I am able to create a blank table with the right fields in place, for the *txt file I just need to beable to import the data. Anyone know how??
    Attached Files Attached Files

  2. #2
    Addicted Member
    Join Date
    Aug 2000
    Posts
    195
    Hi.

    I had a problem about importing a txt into Access through code that I posted here about 10 days ago. Don't know what's the thread but here are some VERY useful links I got from R. Smith. Browse through it! It's worth it. If you're still stuck w/ the code then let me know and I'll write you the code for you.

    Here are the links:
    check some of the details in www.smithvoice.com/vb5expt.htm and
    http://www.smithvoice.com/controll.htm and perhaps also you might like
    http://www.smithvoice.com/rsconv.htm
    Brandon

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    This sample shows u how u should open and read the file:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim arrData() As String
    5.     Dim s As String
    6.     Dim i As Integer
    7.    
    8.     Open "c:\c1_1801t113036_testinga45minuterun.txt" For Input As #1
    9.     Do While Not EOF(1)
    10.         Line Input #1, s
    11.         arrData = Split(s, ",")
    12.         For i = 0 To UBound(arrData)
    13.             Debug.Print arrData(i)
    14.         Next i
    15.     Loop
    16.     Close #1
    17.  
    18. End Sub

    Try the sample in a new project. step throug it and observe.

    Now, in order to import the data u would have to put in some more code

    eg:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim arrData() As String
    5.     Dim s As String
    6.     Dim i As Integer
    7.    
    8.     Open "c:\c1_1801t113036_testinga45minuterun.txt" For Input As #1
    9.     Do While Not EOF(1)
    10.         Line Input #1, s
    11.         arrData = Split(s, ",")
    12.         'For each line u will add a new record to the table.
    13.         'do this here.
    14.         For i = 0 To UBound(arrData)
    15.             'instead of debug.print, u should add the values to the
    16.             'correct fields in the record. do that here.
    17.             Debug.Print arrData(i)
    18.         Next i
    19.         'When u get here, a new record has been added. Update it
    20.         'so it is saved.
    21.     Loop
    22.     Close #1
    23.  
    24. End Sub


    Hope this is of help to u.
    -= a peet post =-

  4. #4
    Addicted Member
    Join Date
    Aug 2000
    Posts
    195
    Peet's code is OK.
    I still would use ISAM drivers though. If nothing else it is much faster (depends on your txt file size, my was 0.5GB!)
    Brandon

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by BrandonSk
    Peet's code is OK.
    I still would use ISAM drivers though. If nothing else it is much faster (depends on your txt file size, my was 0.5GB!)
    that is a big text file Brandon

    I'v never used the ISAM way, thanks for the tip... do u know how much time savings are involved?
    -= a peet post =-

  6. #6
    Addicted Member
    Join Date
    Aug 2000
    Posts
    195
    Well, that is a big txt file. If you can imagine all calls in a small country like Czech Republic that are made in one moth /and I am not talking the # to # logs, just from a transmiter to transmiter (hour and amount of calls) / you will end up with such a text file

    Anyway, to your question. I don't know the savings. Try on a few MB file and you'll see. I think if I did my txt your way, I'd be stuck for a long time. This way the import doesn't take that long (I don't know how much it takes even my way, I think I'll start measuring )

    Post me a reply if you measure it, will ya? Thanx.
    Brandon

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    85

    Smile Here isa what I got so far

    Originally posted by BrandonSk
    Peet's code is OK.
    I still would use ISAM drivers though. If nothing else it is much faster (depends on your txt file size, my was 0.5GB!)
    This is what I have got so far, I will have a look at those websites you got, but have a look at this and see if you know how to complete it. Thanks

    Private Sub cmdImport_Click()
    Dim sFName As String
    Dim sFmtsFName As String
    'On Error GoTo ErrHandler
    CommonDialog1.Filter = "Tempcontrol Files (*.*)|*.*"
    CommonDialog1.FilterIndex = 2
    CommonDialog1.ShowOpen
    OpenFile = ("CommonDialog1.FileName")
    sFName = CommonDialog1.FileTitle

    With dePenlogs.cnlogs
    If .State = 0 Then
    .Open
    End If
    .BeginTrans
    .Execute "CREATE TABLE " + sFName + "(" _
    & "LogNo CHAR (20) NULL," _
    & "Duration CHAR(20) NULL," _
    & "Time_s CHAR(20) NULL," _
    & "Value_s CHAR(20) NULL);"
    .Execute "INSERT INTO " + sFName + "(LogNo, Duration, Time_s, Value_s)" _
    & "VALUES()"
    .CommitTrans
    End With





    Exit Sub

  8. #8
    Addicted Member
    Join Date
    Aug 2000
    Posts
    195
    I guess you're using ADO. Don't know much about it but I don't think that there'll be some major diferences (this uses SQL anyway).

    So, if I got that right you first check if the file exists, if no, create a new table -> if that statement works, then it is ok.
    Now with the export there's a little trick. I don't think your statement would work. You should create something like:
    SELECT (fields) INTO [TypeOfDatabase; DATABASE=Path].[FlName] FROM Table;

    So in your example something like this:
    'st - string
    'This just gets the Path. Didn't test it but should work
    st=Left$(CommonDialog1.FileName, _
    (Instr(CommonDialog1.FileName, CommonDialog1.FileTitle)-2)

    Conn.Execute "SELECT TableName.LogNo, ...Duration, ...Time_s, ...Value_s INTO [Text; DATABASE=" & st & "].[" & sFName &"] FROM TableName;"

    I don't know about INSERT INTO. Should work that way too.
    Especialy pay attention to the Schema.ini file if you want specific output (e.g. delimiter etc.).

    post back if not clear.
    Brandon

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