Results 1 to 10 of 10
  1. #1
    vojinb is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    18

    import txt file with multiple headers

    Hi,


    I have txt file which have header and footer occurring every 70-80 lines. how to import file avoiding this header and footer, just to get data into table?
    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,896
    The Access import utility might not handle this cleanly. The values that do not match datatype defined for respective fields will error. Errors are kicked to an ImportErrors table. So your real data might pass through fine. Try it and find out. If the header/footer rows do come in you can delete those.

    Otherwise, would have to use VBA code to read the file and write to table only those lines that are actual data.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    vojinb is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    18
    I know that, I'm looking of example of VBA code for reading lines of txt file, and for example in code :
    If "line" starts with "some text" ignore next five lines.

    I need of example of code in VBA which reads line by line

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    dim f
    dim fs
    dim sSourceFile
    dim sline
    
    set fs = createobject("scripting.filesystemobject")
    sSourceFile = "<path and name of your file>"
    f = fs.opentextfile(ssourcefile)
    
    do while f.atendofstream <> true
        sline = f.readline
        msgbox sline
        f.readline
    loop

  5. #5
    vojinb is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    18
    Hi ,
    this code is not working.
    What I need is that for txt file like:

    Header


    Class 1
    Value 1 Value 2 Value 3
    Value 1 Value 2 Value 3
    Value 1 Value 2 Value 3
    .
    .
    Class 2
    Value 1 Value 2 Value 3
    Value 1 Value 2 Value 3

    to have code which reads file line by line and when it comes to header ignore it, when it comes to class1 puts in table in different columns values 1,2,3

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    The code works, you're just being lazy

    I've given you the main thing you need (filesystemobject code). You have to figure out how to determine which lines need to be ignored, what you want to do with the 'good' lines etc, I can't do that for you.

  7. #7
    SteveF is offline Generally AccessAble
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    Here's another possible solution:

    1. Import the "raw" data into a temporary table, with no effort made to distinguish between header and detail records.

    2. Now use a query to find & append the detail records from the temporary table to the final table.

    Steve

  8. #8
    vojinb is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    18
    I have imported data into raw table and now I have different problem. When I try to delete all those which I don't need via query, access gives me a message "You are about to delete "number of records", but after delete those data are still in table, and after repeating delete query it gives me message "You are about to delete 0 records", but data are still there and after refresh and after waiting of 10-15 minutes.
    here is query:
    DELETE *
    FROM temp
    WHERE field1 Like '*RE000010*' And '* AREA* ' And '* TO:*' And '*---- ------------*';

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    WHERE field1 Like '*RE000010*' And '* AREA* ' And '* TO:*' And '*---- ------------*';

    what field does *area* refer to?

    what field does " to :* refer to

    what field does *---------------* refer to?

    you've only told the query which field *RE000010* refers to. If they all refer to field 1 then you have to have

    WHERE field1 Like '*RE000010*' And field1 like '* AREA* ' And field 1 like '* TO:*' And field1 like '*---- ------------*';

    but since you are using AND I suspect they are different fields so you'd need:

    WHERE field1 Like '*RE000010*' And field2 like '* AREA* ' And field3 like '* TO:*' And field4 like '*---- ------------*';

    However you really only need one of these like statements if nothing else in your database is going to match them. but not being familiar with the entire possible dataset I couldn't say.

  10. #10
    SteveF is offline Generally AccessAble
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    I'll also point out that you're including a needless extra step: deleting the unwanted records from the temporary table.

    I haven't seen an actual sample of your data, so I'm not totally certain, but I'd be willing to bet that if you can identify the unwanted records, you can also positively identify only the records to be transferred.

    Then it just becomes a matter of using a query along the lines of:

    INSERT INTO [FinalTableName] ( Fieldname1, Fieldname2, ... ) SELECT (Fieldname1, Fieldname2, ... ) FROM [TemporaryTable] WHERE (criteria which identify records to be transferred);

    Steve

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Import excel worksheet by range with non identical headers
    By snoopy2003 in forum Import/Export Data
    Replies: 1
    Last Post: 03-14-2011, 01:04 AM
  2. Skipping headers when Importing a text file.
    By bullwinkle55423 in forum Import/Export Data
    Replies: 1
    Last Post: 09-01-2010, 03:25 PM
  3. Replies: 4
    Last Post: 07-15-2010, 05:58 PM
  4. Splitting an Excel File into Multiple Access Tables on Import
    By TheWolfster in forum Import/Export Data
    Replies: 4
    Last Post: 04-29-2010, 04:52 AM
  5. Import a .sql file?
    By Alan in forum Import/Export Data
    Replies: 1
    Last Post: 06-03-2009, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums