472,803 Members | 870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,803 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, 507 views)
File Type: xlsx SampleTblStructure.xlsx (9.5 KB, 331 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 6763
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, 494 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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.