Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit existing text file from VBA??

Status
Not open for further replies.

mkov

Programmer
Sep 10, 2003
203
US
Is it possible to edit an existing text file from VBA??

We receive a file daily from a vendor that contains informaiton that we pull into our database and then pass the file onto another company which uses it for processing. The problem is that occassionally, there is data missing from the text file that must be there before we pass it on.

Currently, when the file is imported, I test to see if the data is in the correct position of the text file and if not, give the user enough details about the record that they can go into the file and fix it manually.

Well, not I have to automate that. I have it prompt the user for the missing information, and set the value of the input box equal to a string variable.

But once that is done, is there any way to write that information back to the text file and save the changes to the file??

Thanks for your help.

Mark
 
You could do the translation interactively: read the record, parse it, [prompt for missing data], write output from input/[response].

I won't profess to be all that experienced, but I haven't found a way to update a text file yet - without rewriting the whole thing to change one piece of data in one record.

HTH

Dennis
 
That is the way I was planning on doing it, but the thought of having to recreate the file for one or two pieces of missing data is not sitting well.

Thanks for the response.
 
That's why I'd do it interactively: find out what's needed, and get it, while you're building the output.

Have fun...
 
you could also read the txt file, place it in a editable text box, allow the user to make any edits he/she wants, and then resave the information in the text box over the file

Durible Outer Casing to Prevent Fall-Apart
 
here is some code that will load the text into a textbox called hometxt, and list the file name and location in a table called webpages. I am trying to do something similar to your question, but i want to make an easy updater for a web page. Here is the code, hope it might help.

Private Sub loadtext_Click()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
Dim line As String
Dim rst1 As Recordset
Dim db As Database
Dim length As Integer
Set db = CurrentDb
Set rst1 = db.OpenRecordset("webpages")
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.
With fd

'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
If .Show = -1 Then

'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems

'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example simply displays the path in a message box.
line = vrtSelectedItem
length = 1
length = InStr(length, line, "\") + 1
Do Until InStr(length, line, "\") = 0
length = InStr(length, line, "\") + 1
'MsgBox length
Loop
line = Mid(line, length)
With rst1
.AddNew
!pagename = line
!location = vrtSelectedItem
.Update
End With

Dim FileName As String
Dim text As String
text = ""
Dim fs1, f

Set fs1 = CreateObject("Scripting.FileSystemObject")
Set f = fs1.OpenTextFile(vrtSelectedItem, 1)
Do While f.AtEndOfStream <> True
text = text & f.readline
Loop
f.Close
Me.hometxt = text
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub

Durible Outer Casing to Prevent Fall-Apart
 
i made one error in that code that i would like to correct. When the text is imported into the text box it will not have any carrage returns in it. Change
Do While f.AtEndOfStream <> True
text = text & f.readline
Loop
to
Do While f.AtEndOfStream <> True
text = text & f.readline &vbcrlf
Loop
that will add a carrage return at the end of each line.
hope that all helps.

Durible Outer Casing to Prevent Fall-Apart
 
Thanks. I will give the suggestions a try and let you know how it goes.

Thanks again.
 
If the file has fixed length "records", you can edit the individual record in situ, otherwise any change ewquires re0writting.

Buried in htese (Tek-Tips) fora is a reoutine "basGrabFile" (or something similar). There are several versions, demos on its use.implementation are included in some. One (or more) of the demos shows how to get the file into an array (of lines / records) in memory in just a few lines of code. You might be able to use this to provide an easily editable set of records for use in either a manual or aut edit routine.





MichaelRed


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top