Solved

Change Text File

Posted on 2004-08-19
7
126 Views
Last Modified: 2010-05-02
I have a Text file that is space delimited and want to add a digit (1) to a list (column) of fields. the field is changing from a five digit field to a six digit field.  the database only allows 5 digits on the original field. I need to save it to a different file as a text delimited file.

0
Comment
Question by:lopezt1
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:Enlade
Comment Utility

I'm not sure I can help you with this problem, but I just have to ask.  What is a "text delimited file"?  Hee hee.

I think you need to restate your question differently.  Maybe give people an example of the input file (a line or two) and then give them an example of the output file (using the line or two from your input file).  I think if you do that you will get an answer quickly as your solution is probably just a loop, a line input, a split statement, and a print statement.
0
 
LVL 12

Accepted Solution

by:
BobLamberson earned 250 total points
Comment Utility
Hi lopezt1,

Add the following code to a form code, and add a command button to the form.
Insert your path and file names where I have indicated (2 places) then run the form, click the button and open your output file to see the results.

Option Explicit
Dim fileName As String
Dim fso, ts
Dim inText As String
Dim newText As String
Dim txtArray() As String
Dim txtArrayLine() As String
Dim txtLine As String

Private Function getLine()
fileName = "C:\AAA testing\TextDocument\inputText.txt"      ' insert  your input path and filename here
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(fileName, ForReading, True)
If ts.AtEndOfStream <> True Then
   inText = ts.ReadAll
End If
ts.Close
addCharacter (inText)    'passing the file contents to addCharacter function.
Set fso = Nothing
Set ts = Nothing
End Function

Private Function addCharacter(newText As String)
Dim i As Integer
Dim outFileName As String
outFileName = "C:\AAA testing\TextDocument\outputText.txt"      ' insert  your output path and filename here
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(outFileName, ForWriting, True)
txtArray() = Split(newText, vbCrLf)       'put each Line in an Array element
For i = 0 To UBound(txtArray)
   txtArrayLine() = Split(txtArray(i), Chr(32))            'split out the columns
   txtArrayLine(3) = txtArrayLine(3) & "1"             'add the 1 here
   txtLine = Join(txtArrayLine, Chr(32))           'put the line back together
   ts.WriteLine (txtLine)               'write the line to the new file
Next i
ts.Close
Set fso = Nothing
Set ts = Nothing
End Function

Private Sub Command1_Click()
getLine
End Sub


Bob
0
 
LVL 7

Expert Comment

by:Enlade
Comment Utility

I don't know how you wrote any code from that specification, but since you have a loop, a Split, and a WriteLine, it probably will work.  Hee hee.

0
 
LVL 12

Expert Comment

by:BobLamberson
Comment Utility
lopezt1,

I think the code I posted answered your question. If not post the problem you are having with it and someone can probably help.
Bob
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now