473,804 Members | 3,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code to parse out selected fields of .txt to new table

68 New Member
Context-
I have a text file (.txt) that is comma delimited and has each text field surrounded in “”. It does not contain column headers.

I need to import 50 of the 300 rows so am using code instead of linking or importing (via 'get external data') since it exceeds the 255+ limit. I am building a database in Access 2007 off this file.

I have a table that shows every field number and field name in the text file. I added a Y/N box where I marked the fields I want to import and built a query to show only those fields (QRY_IMPORT). Column [FieldID] is the location and [FieldName] is the name. There are 50 records, so I need [FieldID] = 1,2,61,115,154, 155,166,167, etc…

I would like to allow the flexibility to add fields in the future which is why I am planning on building a new table each time with the selected [FieldName] being the column headers.

Question-
What is the best way to go about this?

Step one, I would think is to build a new table with fields selected in the [FieldName] of QRY_IMPORT

Step two, would be to parse out each line, create a count for what field it’s on, see if that count matches one of the fields that I’m looking for and it so, write it to the table. I have written procedures that go though each line of a query using .MoveNext and could go through each of the 50 records in the query, then loop through the code until it gets to that field, write that field to a table, then move to the next line in the query, etc, but then it would have to cycle through 50 fields or query and text of potentially 4000 rows.

I would appreciate any pointers, examples in any level of detail for how this would be done (expertly, efficiently). If the answer involves arrays, I have never used them and don’t really know how they work (is it time I learn?) :)
May 4 '11 #1
21 3848
Rabbit
12,516 Recognized Expert Moderator MVP
It's time you learned to use arrays.

Since it's delimited by a comma, you can read the row, and then use the Split() function to get an array. Then, if you need the first column, you can access that using arrayVariable(0 ). If you need the 8th column, then arrayVariable(7 ). And so on. Plus, you can just loop through an array or recordset containing the columns you want instead of coding it out manually.
May 4 '11 #2
dk4300
68 New Member
See, when you start talking about arrays I get lost. I'm having a hard time getting the (fuzzy to me) concept into practice for what I'm trying to accomplish. I think I need more hand holding.

To practice my parsing, I had grabbed some code from the web and updated it with a count and an if/then statement to randomly pull fields 1 and 93. Instead of having the variables in the code (in this case 1 and 93) I would want to pull all the intergers in [QRY_IMPORT]![FieldID]. That's what you said in your response, but I don't know how to make it happen. Could you please help me update my code?

Expand|Select|Wrap|Line Numbers
  1. Sub ReadTxtFile()
  2.  
  3.    Dim strTxt As String, v1 As Variant, v2 As Variant, intColumnCount As Long
  4.  
  5.    Open "H:\filename.txt" For Input As #1
  6.  
  7.    Do While Not EOF(1)
  8.       Line Input #1, strTxt
  9.       v1 = Split(strTxt, ",")
  10.  
  11.       intColumnCount = 1
  12.       For Each v2 In v1
  13.  
  14.         If intColumnCount = 1 Or intColumnCount = 93 Then
  15.             Debug.Print v2;
  16.         End If
  17.  
  18.         intColumnCount = intColumnCount + 1
  19.       Next
  20.    Loop
  21.    Close #1
  22. End Sub 
May 4 '11 #3
dk4300
68 New Member
OK, now I have the contents of [FieldID] from QRY_IMPORT saved as a dynamic array. I still don't know how to use my new array (arrFieldID) to see if the text field I am on exists within that array. Because I don't know arrays. (But I'm learning)
May 4 '11 #4
Rabbit
12,516 Recognized Expert Moderator MVP
Please use code tags when posting code.

An array is merely a bunch of variables. For example, Dim arrayVar(3) As Integer is merely 4 variables (4 because arrays start with index 0). They are accessed like this
arrayVar(0)
arrayVar(1)
arrayVar(2)
arrayVar(3)


So as you can see, there's no need for you to keep a count of which column you're at because the index number is directly related to the column number.
May 4 '11 #5
dk4300
68 New Member
I see, but I don't know how to apply.

Right now, my text parsing is still generally happening like in lines 7-12 above.
I am also looping through a query of column headers names, saving them as an array, then making a new table with those names as the field names. I'm currently stuck on writing the parsed text fields to their variable columns.
May 5 '11 #6
Rabbit
12,516 Recognized Expert Moderator MVP
Basically, if I had a recordset of numbers that specify which column positions I want from a text file, I could code it like this.
Expand|Select|Wrap|Line Numbers
  1. ' Code that opens text file for reading
  2. ' Code that returns recordset containing numbers representing the columns I want from the file.
  3.  
  4. Do Until objTextFile.AtEndOfStream
  5.    strTemp = objTextFile.ReadLine()
  6.    arrTemp = Split(strTemp, ",")
  7.    strResult = ""
  8.    rstVariable.MoveFirst
  9.  
  10.    Do Until rstVariable.EOF
  11.       strResult = strResult & arrTemp(rstVariable("FieldName")) & " | "
  12.       rstVariable.MoveNext
  13.    Loop
  14.  
  15.    Debug.Print strResult
  16. Loop
May 5 '11 #7
dk4300
68 New Member
Thanks for your help. This is going to kill me.
May 6 '11 #8
Rabbit
12,516 Recognized Expert Moderator MVP
What part are you stuck on? The code itself is fairly straightforward . All it does it read through a file line by line and get the fields it needs.
May 6 '11 #9
dk4300
68 New Member
I'm stuck on my inexperience. I get how straightforward it is. Unless you're me. New to arrays, objFSO, etc.

I have an error "Object Required" on strTemp* = *objTextFile.Re adLine()

Expand|Select|Wrap|Line Numbers
  1. Sub TestText()
  2.  
  3.     Dim strTemp, strResult As String
  4.     Dim arrTemp() As Long
  5.     Dim rstVariable As Recordset
  6.     Dim db As Database
  7.     Dim objTextFile
  8.     Dim objFSO
  9.  
  10.     myFile = "C:\text.txt"
  11.  
  12.     Const ForReading = 1
  13.     Set db = CurrentDb
  14.     Set rstVariable = db.OpenRecordset("TBL_IMPORT")
  15.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  16.     Set objTextFile = objFSO.OpenTextFile(myFile, ForReading)
  17.  
  18.     Do Until objTextFile.AtEndOfStream
  19.         strTemp* = *objTextFile.ReadLine()
  20.         arrTemp = Split(strTemp, ",")
  21.         strResult = ""
  22.         rstVariable.MoveFirst
  23.  
  24.         Do Until rstVariable.EOF
  25.             strResult = strResult & arrTemp(rstVariable("FieldName")) & " | "
  26.             rstVariable.MoveNext
  27.         Loop
  28.  
  29.         Debug.Print strResult
  30.     Loop
  31.  
  32. End Sub 
May 6 '11 #10

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

Similar topics

6
6109
by: nate | last post by:
Hello, Does anyone know where I can find an ASP server side script written in JavaScript to parse text fields from a form method='POST' using enctype='multipart/form-data'? I'd also like it to parse the filename. <form name='form1' method='POST' enctype='multipart/form-data' action='sub.asp'> <input type='text' name='title1' value='value1'> <input type='file' name='file1'>
5
2358
by: xah | last post by:
i'm just starting out on javascript. can anyone show me how to go about writing a code to sort a table column? something like the following: <table border="1"> <tr> <td>
7
3731
by: akira | last post by:
Hello, I need to parse this XML file or XML stream . Is it possible to parse it with DOM reading each DataContainer one by one. <DataContainer> <name> </name> <birthDate> </birthDate> <personnalInfos> <personnalInfo_entries> </personnalnfo_entries> <personnalInfo_elem> </personnalnfo_elem>
1
6429
by: ysn | last post by:
Hi , i have 2 tables in Acees, table A is Company and table B is Merging_Company. Company Contains the following fields CODE , DESCRIPTION , UNIT ID Table Merging_Company Contais the following fields CODE , DESCRIPTION , UNIT ID
0
1456
by: dave k | last post by:
I want to copy data from selected fields in a record on a tabular form (called 'ProductUpdate') to the next record when those two records have a common field - called 'BaseFund' . The selected fields I am updating are called 'date' 'cash' 'shares' and 'gilts' (amongst others). After completing all the fields in the first record I would like all the selected fields in the record below to be automatically updated if I double click on...
4
3961
by: not_a_commie | last post by:
I need code to peg the CPU for 10 seconds to test some background threads. Sleep obviously doesn't do that. How do I code a busy loop that won't get removed by the compiler? Thanks.
2
1438
by: laph | last post by:
I have two databases (DB 1 and DB 2) that linked together by a particular table. So database 1 could see a table in DB 2. Table1 belongs to DB1. Table2 belongs to DB2 but link to DB1. - On this DB 1, we have a form that takes user input. The form ties to table1. Is there a procedure or method to automatically copies selected fields (not record) to the linked Table2? Ex: A form has 5 fields. I want to copy field 2 and 4 as soon as the...
1
2018
by: Mark Niznik | last post by:
by using a mcaro, I need to transfer an access table into Excel and insert today's date in the filename.
1
1987
by: khadar siddi | last post by:
I have tested this in the following two browsers. 1.Internet Exolorer 2.Mozilla FireFox In Internet Explorer: ------------------------ When I right click and see the view source...the html code to display the table is presnt..But still the table is not getting displayed. In Mozilla FireFox:
4
1640
by: dileshw | last post by:
Hey guys, I have a .xls file. Lets call it "MyExcel.xls". And I have a .mdb called "MyDB.mdb" and there's tw tables 't1' and 't2'. I want to import some selected fields of Myexcel.xls into 't1' and 't2'. t1!=t2. and t1 and t2 have a particular coloumn order that i need to follow and myxls is also of a fixed unchangable column order. All tips appriciated!
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9160
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.