473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Text file to access Table

171 100+
Hi All
I have a text file which has records in a multi line structure.
Each record is separated with a blank line in between.
Some records will have 9 fields (in text file it is 9 lines)
and some of them will have 12 fields (or 12 lines in the text file). If the line has only "Layers" in the start line that line and the next following lines to Tbl1 and if the line has "Groups" in the middle of the line it should be saved in the Tbl2.
There are lines end with "*" symbol should not imported to any of the tables.There could be one Layer record then several group lines then again layer then groups like that.
I am trying to import this data to an access table. Hope some of the experts can help me to resolve this. I am attaching 2 sample files. One is a txt file and another one an Excel file which shows the expected result.
Please help
Attached Files
File Type: txt Sample.txt (556 Bytes, 526 views)
File Type: xlsx SampleTblStructure.xlsx (9.5 KB, 345 views)
Mar 30 '17 #1

✓ answered by jforbes

I created a Sample for you to work with. It's written the way I mentioned in my first post. I kind of got the impression that you ignored what I had wrote in that post, so please try to understand what steps are need to be done to complete this type of Import. This is a pretty common type of import... .INI files work this way. I think there is even a class available in .NET to handle this file type.

The sample I created has only one Table to house both datatypes that are defined in the Sample Text that you provided. It was easier for me to just create one table, but if you need to have two separate tables it shouldn't be that hard for you to determine the Table when the datatypes are inserted/updated into the Tables.

Lastly, the sample has the location of the Import File hard coded, you'll need to update that to a real location...
Expand|Select|Wrap|Line Numbers
  1.     Open "C:\Users\jforbes\Desktop\Bytes\TextImport\Sample-1.txt" For Input As #1

5 6944
jforbes
1,107 Expert 1GB
I don't have an Access code sample that does what you are looking for, but it's not that difficult to write a parsing routine that can handle the file format that you have provided.

The basic steps are:
  • Create a DataStructure with all the fields that will be available in the TextFile. These can be just discrete variables or you can make a class file full of variables
  • Open the File to read.
  • Loop through the File Line by Line and populate the DataStructure as the elements are found.
  • When a specific row is found that signifies the end of a group of data, like a blank line in your file, the DataStructure is as full as it's gonna get. If it has enough information, then insert a record into the database based on the DataStructure and then clear out the Datastructure.
  • Keep doing this until the end of file is reached

This sort of seems to be a good starting place for an example on how to read a file and loop through it: Code to parse out selected fields of .txt to new table You could Split on the Equal Sign(=) instead of the Comma(,)

If you develop some code and get stuck, post it and we might be able to help you.
Mar 31 '17 #2
rajeevs
171 100+
Thank you jforbes for the kind reply.
I have found a code which is actually importing the txt to one table, but not what I expect. It splits and import data to 2 fields. The code is as below:
Expand|Select|Wrap|Line Numbers
  1. Sub ReadandImport()
  2. Dim DB As DAO.Database, RS As DAO.Recordset
  3. Dim str1 As String, str2() As String
  4. Dim i As Integer
  5.  
  6. Set DB = CurrentDb
  7. Set RS = DB.OpenRecordset("Layers")
  8.  
  9. Open "path & filename.txt" For Input As #1
  10. Do While Not EOF(1)
  11. Line Input #1, str1
  12.  
  13. str2 = Split(str1, "=")
  14. RS.AddNew
  15. For i = 0 To UBound(str2)
  16. RS(i) = str2(i)
  17. Next
  18. RS.Update
  19. Loop
  20. Close #1
  21. RS.Close
  22. MsgBox "Finished", vbInformation + vbOKOnly, "Import"
  23. End Sub
I dont know how to process each line and get the expected result as shown in my attached excel file. Can you please guide me
Apr 1 '17 #3
jforbes
1,107 Expert 1GB
I created a Sample for you to work with. It's written the way I mentioned in my first post. I kind of got the impression that you ignored what I had wrote in that post, so please try to understand what steps are need to be done to complete this type of Import. This is a pretty common type of import... .INI files work this way. I think there is even a class available in .NET to handle this file type.

The sample I created has only one Table to house both datatypes that are defined in the Sample Text that you provided. It was easier for me to just create one table, but if you need to have two separate tables it shouldn't be that hard for you to determine the Table when the datatypes are inserted/updated into the Tables.

Lastly, the sample has the location of the Import File hard coded, you'll need to update that to a real location...
Expand|Select|Wrap|Line Numbers
  1.     Open "C:\Users\jforbes\Desktop\Bytes\TextImport\Sample-1.txt" For Input As #1
Attached Files
File Type: zip TextImport.zip (45.7 KB, 520 views)
Apr 1 '17 #4
rajeevs
171 100+
Dear jforbes
It was amazing. I don't know how to thank you. I have tested with the actual data and it is perfectly imported. I don't know much about class modules but I will try to understand. I dont need 2 tables, this is more than enough for me. Thank you so much
Apr 2 '17 #5
DavidHP
1 Bit
@jforbes
Awesome file upload.
:)
Feb 3 '21 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tomo | last post by:
I'm trying to import text file into some oracle table. I created control file, and text file, create table ..and run sqlldr80 from command promt.. Imort was not succesfull..error...
2
by: Abhishek | last post by:
All, I am trying to export an access table to a text file. This table has a numeric field (Double) and when I export it to a text file, I see the number in the scientific format. MS-Access...
9
by: carl.barrett | last post by:
Hi, Let me explain. I have been given a file that I need to bring into an Access table. Here is a snippet of the file. 100833983 1 MRS M I BATTY 1 000001 00833983
4
by: Michael A. Covington | last post by:
One of Microsoft's pages says it's easier to read and write text files in ..NET 2.0 than 1.1. It doesn't say what exactly was done (to System.IO or what class?). Can someone tell me?
3
by: Dave G | last post by:
I will shortly be receiving data in the form of a text file, like this: id: 123456 first name: Fred surname: Bloggs age: 26 and so on, for about 60 fields. Each line ends with a carriage...
2
by: Brad Williams | last post by:
Greetings, I have a Access 2007 App and I want to have a Config File. I have several unbound Text Boxes that are in small Date format that will be used as a Filter for several forms. I would like...
1
by: phpmel | last post by:
Hi guys, I am new to this stuff and i have NO IDEA what to do. I have a text file where each line is a value for the name of a department.There are 500 departments.how do I insert that into a...
1
by: ehcy | last post by:
hello guys.. how can i display the <DIR> in my opened files from my text file with table?.. htmlspecialchars is useless.. it always "Warning: file_get_contents() expects parameter 1 to be string,...
1
by: murlikrishna | last post by:
in my data.txt file having data that is to be inserted in the data base. i my data.txt file if having more than one row of data then how to do that. does there is any functions to do that...
1
by: MichaelHooker | last post by:
At this topic "https://bytes.com/topic/visual-basic-net/answers/530817-convert-access-table-text-file-using-vb" is the following Q&A: ====================================== "I have a MS access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.