PDA

View Full Version : Solved: Delete First 9 Characters of a Text File From Access VBA



Kathleen
02-09-2012, 10:27 AM
I have been trying to combine some of the replace code I've found on this forum to delete the first 9 characters of the contents of a text file. The application of this code is to remove UNION ALL from the beginning of a script (i.e., merge.txt), where the phrase UNION ALL is repeated throughout the script. Another way to look at this is to delete the first occurance of UNION ALL, but leave all others. I've tried incorporating right/left functions, without success. Does anyone have any thoughts? Thank you, in advance, for your time and attention regarding this matter.


Function myReplaceFileText(myFilePathInput As String, myFilePathOutput As String, myReplaceString As String, myString As String)

Dim myFileString As String

Open "C:\temp\merge.txt" For Input As #1
myFileString = Input(LOF(1), 1)
Close #1

myFileString = Replace(myFileString, myReplaceString, myString, Compare:=vbTextCompare)

Open myFilePathOutput For Output As #2
Print #2, myFileString
Close
End Function
Sub Test()

Dim myFilePathInput As String
Dim myFilePathOutput As String
Dim myReplaceString As String
Dim myString As String

myFilePathInput = "C:\temp\merge.txt"
myFilePathOutput = "C:\temp\merge2.txt"
myReplaceString = "Union All"
myString = ""

Call myReplaceFileText(myFilePathInput, myFilePathOutput, myReplaceString, myString)

End Sub

EDIT I ADDED CODE TAGS : TOMMY

Tommy
02-09-2012, 11:21 AM
Hi Kathleen

Welcome to VBAX :)

IF you are 100% positive that the first 9 characters need to be removed
we can do this.


myFileString =mid(myFileString ,10)

Kathleen
02-09-2012, 11:50 AM
Thank you, so much. You have solved a problem in minutes, that took me days to ask.